1 class WayController < ApplicationController
 
   5   before_filter :authorize, :only => [:create, :update, :delete]
 
   6   before_filter :check_write_availability, :only => [:create, :update, :delete]
 
   7   before_filter :check_read_availability, :except => [:create, :update, :delete]
 
   8   after_filter :compress_output
 
  12       way = Way.from_xml(request.raw_post, true)
 
  15         if !way.preconditions_ok?
 
  16           render :text => "", :status => :precondition_failed
 
  19           way.user_id = @user.id
 
  20           way.save_with_history!
 
  22           render :text => way.id.to_s, :content_type => "text/plain"
 
  25         render :nothing => true, :status => :bad_request
 
  28       render :nothing => true, :status => :method_not_allowed
 
  34       way = Way.find(params[:id])
 
  36       response.headers['Last-Modified'] = way.timestamp.rfc822
 
  39         render :text => way.to_xml.to_s, :content_type => "text/xml"
 
  41         render :text => "", :status => :gone
 
  43     rescue ActiveRecord::RecordNotFound
 
  44       render :nothing => true, :status => :not_found
 
  50       way = Way.find(params[:id])
 
  51       new_way = Way.from_xml(request.raw_post)
 
  53       if new_way and new_way.id == way.id
 
  54         unless update_internal(way, new_way)
 
  55           render :text => "", :status => :precondition_failed
 
  57           render :nothing => true
 
  60         render :nothing => true, :status => :bad_request
 
  62     rescue ActiveRecord::RecordNotFound
 
  63       render :nothing => true, :status => :not_found
 
  67   def update_internal way, new_way
 
  68     way = Way.find(new_way.id) if way.nil?
 
  70     if !new_way.preconditions_ok?
 
  73       way.user_id = @user.id
 
  74       way.tags = new_way.tags
 
  77       way.save_with_history!
 
  83   # This is the API call to delete a way
 
  86       way = Way.find(params[:id])
 
  87       way.delete_with_relations_and_history(@user)
 
  89       # if we get here, all is fine, otherwise something will catch below.  
 
  90       render :nothing => true
 
  91     rescue OSM::APIAlreadyDeletedError
 
  92       render :text => "", :status => :gone
 
  93     rescue OSM::APIPreconditionFailedError
 
  94       render :text => "", :status => :precondition_failed
 
  95     rescue ActiveRecord::RecordNotFound
 
  96       render :nothing => true, :status => :not_found
 
 102       way = Way.find(params[:id])
 
 105         nd_ids = way.nds + [-1]
 
 106         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
 
 109         doc = OSM::API.new.get_xml_doc
 
 111           doc.root << node.to_xml_node()
 
 113         doc.root << way.to_xml_node()
 
 115         render :text => doc.to_s, :content_type => "text/xml"
 
 117         render :text => "", :status => :gone
 
 119     rescue ActiveRecord::RecordNotFound
 
 120       render :nothing => true, :status => :not_found
 
 126       ids = params['ways'].split(',').collect { |w| w.to_i }
 
 132       doc = OSM::API.new.get_xml_doc
 
 134       Way.find(ids).each do |way|
 
 135         doc.root << way.to_xml_node
 
 138       render :text => doc.to_s, :content_type => "text/xml"
 
 140       render :nothing => true, :status => :bad_request
 
 145     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq
 
 147     doc = OSM::API.new.get_xml_doc
 
 149     Way.find(wayids).each do |way|
 
 150       doc.root << way.to_xml_node
 
 153     render :text => doc.to_s, :content_type => "text/xml"