]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
some useful notes in code for ideas that require implementing.
[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     if request.put?
12       way = Way.from_xml(request.raw_post, true)
13
14       if way
15         unless way.preconditions_ok?
16           render :text => "", :status => :precondition_failed
17         else
18           way.version = 0
19           way.save_with_history!
20
21           render :text => way.id.to_s, :content_type => "text/plain"
22         end
23       else
24         render :nothing => true, :status => :bad_request
25       end
26     else
27       render :nothing => true, :status => :method_not_allowed
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       render ex.render_opts
62     rescue ActiveRecord::RecordNotFound
63       render :nothing => true, :status => :not_found
64     end
65   end
66
67   # This is the API call to delete a way
68   def delete
69     begin
70       way = Way.find(params[:id])
71       way.delete_with_history(@user)
72
73       # if we get here, all is fine, otherwise something will catch below.  
74       render :nothing => true
75     rescue OSM::APIError => ex
76       render ex.render_opts
77     rescue ActiveRecord::RecordNotFound
78       render :nothing => true, :status => :not_found
79     end
80   end
81
82   def full
83     begin
84       way = Way.find(params[:id])
85
86       if way.visible
87         nd_ids = way.nds + [-1]
88         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
89
90         # Render
91         doc = OSM::API.new.get_xml_doc
92         nodes.each do |node|
93           doc.root << node.to_xml_node()
94         end
95         doc.root << way.to_xml_node()
96
97         render :text => doc.to_s, :content_type => "text/xml"
98       else
99         render :text => "", :status => :gone
100       end
101     rescue ActiveRecord::RecordNotFound
102       render :nothing => true, :status => :not_found
103     end
104   end
105
106   def ways
107     begin
108       ids = params['ways'].split(',').collect { |w| w.to_i }
109     rescue
110       ids = []
111     end
112
113     if ids.length > 0
114       doc = OSM::API.new.get_xml_doc
115
116       Way.find(ids).each do |way|
117         doc.root << way.to_xml_node
118       end
119
120       render :text => doc.to_s, :content_type => "text/xml"
121     else
122       render :nothing => true, :status => :bad_request
123     end
124   end
125
126   def ways_for_node
127     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq
128
129     doc = OSM::API.new.get_xml_doc
130
131     Way.find(wayids).each do |way|
132       doc.root << way.to_xml_node
133     end
134
135     render :text => doc.to_s, :content_type => "text/xml"
136   end
137 end