]> git.openstreetmap.org Git - rails.git/blob - app/controllers/api/ways_controller.rb
Fix new rubocop warnings
[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 :set_request_formats, :except => [:create, :update, :delete]
15
16     def create
17       assert_method :put
18
19       way = Way.from_xml(request.raw_post, :create => 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 the result
33         respond_to do |format|
34           format.xml
35           format.json
36         end
37       else
38         head :gone
39       end
40     end
41
42     def update
43       way = Way.find(params[:id])
44       new_way = Way.from_xml(request.raw_post)
45
46       raise OSM::APIBadUserInput, "The id in the url (#{way.id}) is not the same as provided in the xml (#{new_way.id})" unless new_way && new_way.id == way.id
47
48       way.update_from(new_way, current_user)
49       render :plain => way.version.to_s
50     end
51
52     # This is the API call to delete a way
53     def delete
54       way = Way.find(params[:id])
55       new_way = Way.from_xml(request.raw_post)
56
57       if new_way && new_way.id == way.id
58         way.delete_with_history!(new_way, current_user)
59         render :plain => way.version.to_s
60       else
61         head :bad_request
62       end
63     end
64
65     def full
66       @way = Way.includes(:nodes => :node_tags).find(params[:id])
67
68       if @way.visible
69         visible_nodes = {}
70
71         @nodes = []
72
73         @way.nodes.uniq.each do |node|
74           if node.visible
75             @nodes << node
76             visible_nodes[node.id] = node
77           end
78         end
79
80         # Render the result
81         respond_to do |format|
82           format.xml
83           format.json
84         end
85       else
86         head :gone
87       end
88     end
89
90     def index
91       raise OSM::APIBadUserInput, "The parameter ways is required, and must be of the form ways=id[,id[,id...]]" unless params["ways"]
92
93       ids = params["ways"].split(",").collect(&:to_i)
94
95       raise OSM::APIBadUserInput, "No ways were given to search for" if ids.empty?
96
97       @ways = Way.find(ids)
98
99       # Render the result
100       respond_to do |format|
101         format.xml
102         format.json
103       end
104     end
105
106     ##
107     # returns all the ways which are currently using the node given in the
108     # :id parameter. note that this used to return deleted ways as well, but
109     # this seemed not to be the expected behaviour, so it was removed.
110     def ways_for_node
111       wayids = WayNode.where(:node_id => params[:id]).collect { |ws| ws.id[0] }.uniq
112
113       @ways = Way.where(:id => wayids, :visible => true)
114
115       # Render the result
116       respond_to do |format|
117         format.xml
118         format.json
119       end
120     end
121   end
122 end