]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Added a bunch more tests on the API 0.6. Fixed node/way/relation from_xml code to...
[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 :check_write_availability, :only => [:create, :update, :delete]
7   before_filter :check_read_availability, :except => [:create, :update, :delete]
8   after_filter :compress_output
9
10   def create
11     begin
12       if request.put?
13         way = Way.from_xml(request.raw_post, true)
14
15         if way
16           way.create_with_history @user
17           render :text => way.id.to_s, :content_type => "text/plain"
18         else
19           render :nothing => true, :status => :bad_request
20         end
21       else
22         render :nothing => true, :status => :method_not_allowed
23       end
24     rescue OSM::APIError => ex
25       render ex.render_opts
26     end
27   end
28
29   def read
30     begin
31       way = Way.find(params[:id])
32
33       response.headers['Last-Modified'] = way.timestamp.rfc822
34
35       if way.visible
36         render :text => way.to_xml.to_s, :content_type => "text/xml"
37       else
38         render :text => "", :status => :gone
39       end
40     rescue OSM::APIError => ex
41       render ex.render_opts
42     rescue ActiveRecord::RecordNotFound
43       render :nothing => true, :status => :not_found
44     end
45   end
46
47   def update
48     begin
49       way = Way.find(params[:id])
50       new_way = Way.from_xml(request.raw_post)
51
52       if new_way and new_way.id == way.id
53         way.update_from(new_way, @user)
54         render :text => way.version.to_s, :content_type => "text/plain"
55       else
56         render :nothing => true, :status => :bad_request
57       end
58     rescue OSM::APIError => ex
59       render ex.render_opts
60     rescue ActiveRecord::RecordNotFound
61       render :nothing => true, :status => :not_found
62     end
63   end
64
65   # This is the API call to delete a way
66   def delete
67     begin
68       way = Way.find(params[:id])
69       new_way = Way.from_xml(request.raw_post)
70       if new_way and new_way.id == way.id
71         way.delete_with_history(new_way, @user)
72
73         # if we get here, all is fine, otherwise something will catch below.  
74         render :nothing => true
75       else
76         render :nothing => true, :status => :bad_request
77       end
78     rescue OSM::APIError => ex
79       render ex.render_opts
80     rescue ActiveRecord::RecordNotFound
81       render :nothing => true, :status => :not_found
82     end
83   end
84
85   def full
86     begin
87       way = Way.find(params[:id])
88
89       if way.visible
90         nd_ids = way.nds + [-1]
91         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
92
93         # Render
94         doc = OSM::API.new.get_xml_doc
95         nodes.each do |node|
96           doc.root << node.to_xml_node()
97         end
98         doc.root << way.to_xml_node()
99
100         render :text => doc.to_s, :content_type => "text/xml"
101       else
102         render :text => "", :status => :gone
103       end
104     rescue ActiveRecord::RecordNotFound
105       render :nothing => true, :status => :not_found
106     end
107   end
108
109   def ways
110     begin
111       ids = params['ways'].split(',').collect { |w| w.to_i }
112     rescue
113       ids = []
114     end
115
116     if ids.length > 0
117       doc = OSM::API.new.get_xml_doc
118
119       Way.find(ids).each do |way|
120         doc.root << way.to_xml_node
121       end
122
123       render :text => doc.to_s, :content_type => "text/xml"
124     else
125       render :nothing => true, :status => :bad_request
126     end
127   end
128
129   def ways_for_node
130     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq
131
132     doc = OSM::API.new.get_xml_doc
133
134     Way.find(wayids).each do |way|
135       doc.root << way.to_xml_node
136     end
137
138     render :text => doc.to_s, :content_type => "text/xml"
139   end
140 end