]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
more testing of the api. Changing the generator so that it is a constant to come...
[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
71       if new_way and new_way.id == way.id
72         way.delete_with_history!(new_way, @user)
73         render :text => way.version.to_s, :content_type => "text/plain"
74       else
75         render :nothing => true, :status => :bad_request
76       end
77     rescue OSM::APIError => ex
78       render ex.render_opts
79     rescue ActiveRecord::RecordNotFound
80       render :nothing => true, :status => :not_found
81     end
82   end
83
84   def full
85     begin
86       way = Way.find(params[:id])
87
88       if way.visible
89         nd_ids = way.nds + [-1]
90         nodes = Node.find(:all, :conditions => "visible = 1 AND id IN (#{nd_ids.join(',')})")
91
92         # Render
93         doc = OSM::API.new.get_xml_doc
94         nodes.each do |node|
95           doc.root << node.to_xml_node()
96         end
97         doc.root << way.to_xml_node()
98
99         render :text => doc.to_s, :content_type => "text/xml"
100       else
101         render :text => "", :status => :gone
102       end
103     rescue ActiveRecord::RecordNotFound
104       render :nothing => true, :status => :not_found
105     end
106   end
107
108   def ways
109     begin
110       ids = params['ways'].split(',').collect { |w| w.to_i }
111     rescue
112       ids = []
113     end
114
115     if ids.length > 0
116       doc = OSM::API.new.get_xml_doc
117
118       Way.find(ids).each do |way|
119         doc.root << way.to_xml_node
120       end
121
122       render :text => doc.to_s, :content_type => "text/xml"
123     else
124       render :nothing => true, :status => :bad_request
125     end
126   end
127
128   def ways_for_node
129     wayids = WayNode.find(:all, :conditions => ['node_id = ?', params[:id]]).collect { |ws| ws.id[0] }.uniq
130
131     doc = OSM::API.new.get_xml_doc
132
133     Way.find(wayids).each do |way|
134       doc.root << way.to_xml_node
135     end
136
137     render :text => doc.to_s, :content_type => "text/xml"
138   end
139 end