]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Expose relation version to api and fix remaining server status typo in old_relation...
[rails.git] / app / controllers / way_controller.rb
1 class WayController < ApplicationController
2   require 'xml/libxml'
3
4   session :off
5   before_filter :authorize, :only => [:create, :update, :delete]
6   before_filter :require_public_data, :only => [:create, :update, :delete]
7   before_filter :check_api_writable, :only => [:create, :update, :delete]
8   before_filter :check_api_readable, :except => [:create, :update, :delete]
9   after_filter :compress_output
10
11   def create
12     begin
13       if request.put?
14         way = Way.from_xml(request.raw_post, true)
15
16         if way
17           way.create_with_history @user
18           render :text => way.id.to_s, :content_type => "text/plain"
19         else
20           render :nothing => true, :status => :bad_request
21         end
22       else
23         render :nothing => true, :status => :method_not_allowed
24       end
25     rescue OSM::APIError => ex
26       logger.warn request.raw_post
27       render ex.render_opts
28     end
29   end
30
31   def read
32     begin
33       way = Way.find(params[:id])
34
35       response.headers['Last-Modified'] = way.timestamp.rfc822
36
37       if way.visible
38         render :text => way.to_xml.to_s, :content_type => "text/xml"
39       else
40         render :text => "", :status => :gone
41       end
42     rescue OSM::APIError => ex
43       render ex.render_opts
44     rescue ActiveRecord::RecordNotFound
45       render :nothing => true, :status => :not_found
46     end
47   end
48
49   def update
50     begin
51       way = Way.find(params[:id])
52       new_way = Way.from_xml(request.raw_post)
53
54       if new_way and new_way.id == way.id
55         way.update_from(new_way, @user)
56         render :text => way.version.to_s, :content_type => "text/plain"
57       else
58         render :nothing => true, :status => :bad_request
59       end
60     rescue OSM::APIError => ex
61       logger.warn request.raw_post
62       render ex.render_opts
63     rescue ActiveRecord::RecordNotFound
64       render :nothing => true, :status => :not_found
65     end
66   end
67
68   # This is the API call to delete a way
69   def delete
70     begin
71       way = Way.find(params[:id])
72       new_way = Way.from_xml(request.raw_post)
73
74       if new_way and new_way.id == way.id
75         way.delete_with_history!(new_way, @user)
76         render :text => way.version.to_s, :content_type => "text/plain"
77       else
78         render :nothing => true, :status => :bad_request
79       end
80     rescue OSM::APIError => ex
81       render ex.render_opts
82     rescue ActiveRecord::RecordNotFound
83       render :nothing => true, :status => :not_found
84     end
85   end
86
87   def full
88     begin
89       way = Way.find(params[:id])
90
91       if way.visible
92         nd_ids = way.nds + [-1]
93         nodes = Node.find(:all, :conditions => ["visible = ? AND id IN (#{nd_ids.join(',')})", true])
94
95         # Render
96         doc = OSM::API.new.get_xml_doc
97         nodes.each do |node|
98           doc.root << node.to_xml_node()
99         end
100         doc.root << way.to_xml_node()
101
102         render :text => doc.to_s, :content_type => "text/xml"
103       else
104         render :text => "", :status => :gone
105       end
106     rescue ActiveRecord::RecordNotFound
107       render :nothing => true, :status => :not_found
108     end
109   end
110
111   def ways
112     begin
113       ids = params['ways'].split(',').collect { |w| w.to_i }
114     rescue
115       ids = []
116     end
117
118     if ids.length > 0
119       doc = OSM::API.new.get_xml_doc
120
121       Way.find(ids).each do |way|
122         doc.root << way.to_xml_node
123       end
124
125       render :text => doc.to_s, :content_type => "text/xml"
126     else
127       render :nothing => true, :status => :bad_request
128     end
129   end
130
131   ##
132   # returns all the ways which are currently using the node given in the 
133   # :id parameter. note that this used to return deleted ways as well, but
134   # this seemed not to be the expected behaviour, so it was removed.
135   def ways_for_node
136     wayids = WayNode.find(:all, 
137                           :conditions => ['node_id = ?', params[:id]]
138                           ).collect { |ws| ws.id[0] }.uniq
139
140     doc = OSM::API.new.get_xml_doc
141
142     Way.find(wayids).each do |way|
143       doc.root << way.to_xml_node if way.visible
144     end
145
146     render :text => doc.to_s, :content_type => "text/xml"
147   end
148 end