]> git.openstreetmap.org Git - rails.git/blob - app/controllers/way_controller.rb
Log unexpected exceptions raised by API calls.
[rails.git] / app / controllers / way_controller.rb
1 class WayController < ApplicationController
2   require 'xml/libxml'
3
4   before_filter :authorize, :only => [:create, :update, :delete]
5   before_filter :require_public_data, :only => [:create, :update, :delete]
6   before_filter :check_api_writable, :only => [:create, :update, :delete]
7   before_filter :check_api_readable, :except => [:create, :update, :delete]
8   after_filter :compress_output
9   around_filter :api_call_handle_error, :api_call_timeout
10
11   def create
12     assert_method :put
13
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   end
23
24   def read
25     way = Way.find(params[:id])
26     
27     response.headers['Last-Modified'] = way.timestamp.rfc822
28     
29     if way.visible
30       render :text => way.to_xml.to_s, :content_type => "text/xml"
31     else
32       render :text => "", :status => :gone
33     end
34   end
35
36   def update
37     way = Way.find(params[:id])
38     new_way = Way.from_xml(request.raw_post)
39     
40     if new_way and new_way.id == way.id
41       way.update_from(new_way, @user)
42       render :text => way.version.to_s, :content_type => "text/plain"
43     else
44       render :nothing => true, :status => :bad_request
45     end
46   end
47
48   # This is the API call to delete a way
49   def delete
50     way = Way.find(params[:id])
51     new_way = Way.from_xml(request.raw_post)
52     
53     if new_way and new_way.id == way.id
54       way.delete_with_history!(new_way, @user)
55       render :text => way.version.to_s, :content_type => "text/plain"
56     else
57       render :nothing => true, :status => :bad_request
58     end
59   end
60
61   def full
62     way = Way.find(params[:id], :include => {:nodes => :node_tags})
63     
64     if way.visible
65       changeset_cache = {}
66       user_display_name_cache = {}
67
68       doc = OSM::API.new.get_xml_doc
69       way.nodes.each do |node|
70         if node.visible
71           doc.root << node.to_xml_node(changeset_cache, user_display_name_cache)
72         end
73       end
74       doc.root << way.to_xml_node(nil, changeset_cache, user_display_name_cache)
75       
76       render :text => doc.to_s, :content_type => "text/xml"
77     else
78       render :text => "", :status => :gone
79     end
80   end
81
82   def ways
83     begin
84       ids = params['ways'].split(',').collect { |w| w.to_i }
85     rescue
86       ids = []
87     end
88
89     if ids.length > 0
90       doc = OSM::API.new.get_xml_doc
91
92       Way.find(ids).each do |way|
93         doc.root << way.to_xml_node
94       end
95
96       render :text => doc.to_s, :content_type => "text/xml"
97     else
98       render :nothing => true, :status => :bad_request
99     end
100   end
101
102   ##
103   # returns all the ways which are currently using the node given in the 
104   # :id parameter. note that this used to return deleted ways as well, but
105   # this seemed not to be the expected behaviour, so it was removed.
106   def ways_for_node
107     wayids = WayNode.find(:all, 
108                           :conditions => ['node_id = ?', params[:id]]
109                           ).collect { |ws| ws.id[0] }.uniq
110
111     doc = OSM::API.new.get_xml_doc
112
113     Way.find(wayids).each do |way|
114       doc.root << way.to_xml_node if way.visible
115     end
116
117     render :text => doc.to_s, :content_type => "text/xml"
118   end
119 end