]> git.openstreetmap.org Git - rails.git/blob - app/controllers/node_controller.rb
More trace tests
[rails.git] / app / controllers / node_controller.rb
1 # The NodeController is the RESTful interface to Node objects
2
3 class NodeController < ApplicationController
4   require 'xml/libxml'
5
6   session :off
7   before_filter :authorize, :only => [:create, :update, :delete]
8   before_filter :require_public_data, :only => [:create, :update, :delete]
9   before_filter :check_api_writable, :only => [:create, :update, :delete]
10   before_filter :check_api_readable, :except => [:create, :update, :delete]
11   after_filter :compress_output
12
13   # Create a node from XML.
14   def create
15     begin
16       if request.put?
17         node = Node.from_xml(request.raw_post, true)
18
19         if node
20           node.create_with_history @user
21           render :text => node.id.to_s, :content_type => "text/plain"
22         else
23           render :nothing => true, :status => :bad_request
24         end
25       else
26         render :nothing => true, :status => :method_not_allowed
27       end
28     rescue OSM::APIError => ex
29       render ex.render_opts
30     end
31   end
32
33   # Dump the details on a node given in params[:id]
34   def read
35     begin
36       node = Node.find(params[:id])
37       if node.visible?
38         response.headers['Last-Modified'] = node.timestamp.rfc822
39         render :text => node.to_xml.to_s, :content_type => "text/xml"
40        else
41         render :text => "", :status => :gone
42       end
43     rescue ActiveRecord::RecordNotFound
44       render :nothing => true, :status => :not_found
45     end
46   end
47   
48   # Update a node from given XML
49   def update
50     begin
51       node = Node.find(params[:id])
52       new_node = Node.from_xml(request.raw_post)
53
54       if new_node and new_node.id == node.id
55         node.update_from(new_node, @user)
56         render :text => node.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   # Delete a node. Doesn't actually delete it, but retains its history 
68   # in a wiki-like way. We therefore treat it like an update, so the delete
69   # method returns the new version number.
70   def delete
71     begin
72       node = Node.find(params[:id])
73       new_node = Node.from_xml(request.raw_post)
74       
75       if new_node and new_node.id == node.id
76         node.delete_with_history!(new_node, @user)
77         render :text => node.version.to_s, :content_type => "text/plain"
78       else
79         render :nothing => true, :status => :bad_request
80       end
81     rescue ActiveRecord::RecordNotFound
82       render :nothing => true, :status => :not_found
83     rescue OSM::APIError => ex
84       render ex.render_opts
85     end
86   end
87
88   # Dump the details on many nodes whose ids are given in the "nodes" parameter.
89   def nodes
90     ids = params['nodes'].split(',').collect { |n| n.to_i }
91
92     if ids.length > 0
93       doc = OSM::API.new.get_xml_doc
94
95       Node.find(ids).each do |node|
96         doc.root << node.to_xml_node
97       end 
98
99       render :text => doc.to_s, :content_type => "text/xml"
100     else
101       render :nothing => true, :status => :bad_request
102     end
103   end
104 end