1 # The NodeController is the RESTful interface to Node objects
3 class NodeController < ApplicationController
7 before_filter :authorize, :only => [:create, :update, :delete]
8 before_filter :check_write_availability, :only => [:create, :update, :delete]
9 before_filter :check_read_availability, :except => [:create, :update, :delete]
10 after_filter :compress_output
12 # Create a node from XML.
15 node = Node.from_xml(request.raw_post, true)
17 logger.debug request.raw_post
22 #node.changeset_id = node.changeset
24 node.save_with_history!
26 render :text => node.id.to_s, :content_type => "text/plain"
28 render :nothing => true, :status => :bad_request
31 render :nothing => true, :status => :method_not_allowed
35 # Dump the details on a node given in params[:id]
38 node = Node.find(params[:id])
40 response.headers['Last-Modified'] = node.timestamp.rfc822
41 render :text => node.to_xml.to_s, :content_type => "text/xml"
43 render :text => "", :status => :gone
45 rescue ActiveRecord::RecordNotFound
46 render :nothing => true, :status => :not_found
50 # Dump a specific version of the node based on the given params[:id] and params[:version]
53 node = Node.find(:first, :conditions => { :id => params[:id], :version => params[:version] } )
55 response.headers['Last-Modified'] = node.timestamp.rfc822
56 render :text => node.to_xml.to_s, :content_type => "text/xml"
58 render :nothing => true, :status => :gone
60 rescue ActiveRecord::RecordNotFound
61 render :nothing => true, :status => :not_found
65 # Update a node from given XML
68 node = Node.find(params[:id])
69 new_node = Node.from_xml(request.raw_post)
71 if new_node and new_node.id == node.id
72 node.update_from(new_node, @user)
73 render :text => node.version.to_s, :content_type => "text/plain"
75 render :nothing => true, :status => :bad_request
77 rescue OSM::APIVersionMismatchError => ex
78 render :text => "Version mismatch: Provided " + ex.provided.to_s +
79 ", server had: " + ex.latest.to_s, :status => :bad_request
80 rescue ActiveRecord::RecordNotFound
81 render :nothing => true, :status => :not_found
85 # Delete a node. Doesn't actually delete it, but retains its history in a wiki-like way.
86 # FIXME remove all the fricking SQL
89 node = Node.find(params[:id])
90 new_node = Node.from_xml(request.raw_post)
91 # FIXME we no longer care about the user, (or maybe we want to check
92 # that the user of the changeset is the same user as is making this
93 # little change?) we really care about the
94 # changeset which must be open, and that the version that we have been
95 # given is the one that is currently stored in the database
97 if new_node and new_node.id == node.id
98 node.delete_with_history(new_node, @user)
99 render :nothing => true, :status => :success
101 render :nothing => true, :status => :bad_request
103 rescue ActiveRecord::RecordNotFound
104 render :nothing => true, :status => :not_found
105 rescue OSM::APIError => ex
106 render ex.render_opts
112 ids = params['nodes'].split(',').collect { |n| n.to_i }
115 doc = OSM::API.new.get_xml_doc
117 Node.find(ids).each do |node|
118 doc.root << node.to_xml_node
121 render :text => doc.to_s, :content_type => "text/xml"
123 render :nothing => true, :status => :bad_request