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