]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/ways_controller.rb
Merge remote-tracking branch 'upstream/pull/2167'
[rails.git] / app / controllers / api / ways_controller.rb
1 module Api
2   class WaysController < ApplicationController
3     require "xml/libxml"
4
5     skip_before_action :verify_authenticity_token
6     before_action :authorize, :only => [:create, :update, :delete]
7     before_action :api_deny_access_handler
8
9     authorize_resource
10
11     before_action :require_public_data, :only => [:create, :update, :delete]
12     before_action :check_api_writable, :only => [:create, :update, :delete]
13     before_action :check_api_readable, :except => [:create, :update, :delete]
14     around_action :api_call_handle_error, :api_call_timeout
15
16     def create
17       assert_method :put
18
19       way = Way.from_xml(request.raw_post, true)
20
21       # Assume that Way.from_xml has thrown an exception if there is an error parsing the xml
22       way.create_with_history current_user
23       render :plain => way.id.to_s
24     end
25
26     def show
27       way = Way.find(params[:id])
28
29       response.last_modified = way.timestamp
30
31       if way.visible
32         render :xml => way.to_xml.to_s
33       else
34         head :gone
35       end
36     end
37
38     def update
39       way = Way.find(params[:id])
40       new_way = Way.from_xml(request.raw_post)
41
42       unless new_way && new_way.id == way.id
43         raise OSM::APIBadUserInput, "The id in the url (#{way.id}) is not the same as provided in the xml (#{new_way.id})"
44       end
45
46       way.update_from(new_way, current_user)
47       render :plain => way.version.to_s
48     end
49
50     # This is the API call to delete a way
51     def delete
52       way = Way.find(params[:id])
53       new_way = Way.from_xml(request.raw_post)
54
55       if new_way && new_way.id == way.id
56         way.delete_with_history!(new_way, current_user)
57         render :plain => way.version.to_s
58       else
59         head :bad_request
60       end
61     end
62
63     def full
64       way = Way.includes(:nodes => :node_tags).find(params[:id])
65
66       if way.visible
67         visible_nodes = {}
68         changeset_cache = {}
69         user_display_name_cache = {}
70
71         doc = OSM::API.new.get_xml_doc
72         way.nodes.uniq.each do |node|
73           if node.visible
74             doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
75             visible_nodes[node.id] = node
76           end
77         end
78         doc.root << way.to_xml_node(visible_nodes, changeset_cache, user_display_name_cache)
79
80         render :xml => doc.to_s
81       else
82         head :gone
83       end
84     end
85
86     def index
87       unless params["ways"]
88         raise OSM::APIBadUserInput, "The parameter ways is required, and must be of the form ways=id[,id[,id...]]"
89       end
90
91       ids = params["ways"].split(",").collect(&:to_i)
92
93       raise OSM::APIBadUserInput, "No ways were given to search for" if ids.empty?
94
95       doc = OSM::API.new.get_xml_doc
96
97       Way.find(ids).each do |way|
98         doc.root << way.to_xml_node
99       end
100
101       render :xml => doc.to_s
102     end
103
104     ##
105     # returns all the ways which are currently using the node given in the
106     # :id parameter. note that this used to return deleted ways as well, but
107     # this seemed not to be the expected behaviour, so it was removed.
108     def ways_for_node
109       wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq
110
111       doc = OSM::API.new.get_xml_doc
112
113       Way.find(wayids).each do |way|
114         doc.root << way.to_xml_node if way.visible
115       end
116
117       render :xml => doc.to_s
118     end
119   end
120 end