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