From: Steve Coast Date: Sat, 7 Apr 2007 12:43:48 +0000 (+0000) Subject: simplify grabbing of a xml base doc X-Git-Tag: live~9149 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/668f8020ccbf544de89822415e342c1c60826dfe?ds=sidebyside simplify grabbing of a xml base doc --- diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb index 73fd9aa8c..bfe26f542 100644 --- a/app/controllers/api_controller.rb +++ b/app/controllers/api_controller.rb @@ -40,7 +40,7 @@ class ApiController < ApplicationController # get missing nodes if there are any nodes += Node.find(missing_nodes) if missing_nodes.length > 0 - doc = get_xml_doc + doc = OSM::API.get_xml_doc # get ways # find which ways are needed diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 6520e027b..9d4749a0d 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -38,16 +38,6 @@ class ApplicationController < ActionController::Base end end - def get_xml_doc - doc = XML::Document.new - doc.encoding = 'UTF-8' - root = XML::Node.new 'osm' - root['version'] = API_VERSION - root['generator'] = 'OpenStreetMap server' - doc.root = root - return doc - end - # extract authorisation credentials from headers, returns user = nil if none private def get_auth_data diff --git a/app/controllers/old_node_controller.rb b/app/controllers/old_node_controller.rb index 99e0b5675..2c0cca9bb 100644 --- a/app/controllers/old_node_controller.rb +++ b/app/controllers/old_node_controller.rb @@ -9,7 +9,7 @@ class OldNodeController < ApplicationController return end - doc = get_xml_doc + doc = OSM::API.new.get_xml_doc node.old_nodes.each do |old_node| doc.root << old_node.to_xml_node diff --git a/app/controllers/old_segment_controller.rb b/app/controllers/old_segment_controller.rb index 1971e5dca..a35a622f9 100644 --- a/app/controllers/old_segment_controller.rb +++ b/app/controllers/old_segment_controller.rb @@ -9,7 +9,7 @@ class OldSegmentController < ApplicationController return end - doc = get_xml_doc + doc = OSM::API.new.get_xml_doc segment.old_segments.each do |old_segment| doc.root << old_segment.to_xml_node @@ -17,9 +17,4 @@ class OldSegmentController < ApplicationController render :text => doc.to_s end - - - - - end diff --git a/app/controllers/segment_controller.rb b/app/controllers/segment_controller.rb index 950479ad7..76cb5cebb 100644 --- a/app/controllers/segment_controller.rb +++ b/app/controllers/segment_controller.rb @@ -85,7 +85,7 @@ class SegmentController < ApplicationController ids = params['segments'].split(',').collect {|s| s.to_i } if ids.length > 0 segmentlist = Segment.find(ids) - doc = get_xml_doc + doc = OSM::API.get_xml_doc segmentlist.each do |segment| doc.root << segment.to_xml_node end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 6561d46fb..761468b98 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,7 +1,7 @@ class UserController < ApplicationController layout 'site' - before_filter :authorize, :only => :preferences + before_filter :authorize, :only => [:preferences, :api_details] before_filter :authorize_web, :only => [:rename, :account, :go_public] before_filter :require_user, :only => [:rename, :account, :go_public] @@ -125,5 +125,9 @@ class UserController < ApplicationController render :status => 400, :nothing => true end end + + def api_details + render :text => @user.to_xml.to_s + end end diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index f9fd70af4..8daac7cc2 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -90,7 +90,7 @@ class WayController < ApplicationController ids = params['ways'].split(',').collect {|w| w.to_i } if ids.length > 0 waylist = Way.find(ids) - doc = get_xml_doc + doc = OSM::API.new.get_xml_doc waylist.each do |way| doc.root << way.to_xml_node end diff --git a/app/models/node.rb b/app/models/node.rb index 230b0f9a7..0df3689e2 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -72,13 +72,8 @@ class Node < ActiveRecord::Base end def to_xml - doc = XML::Document.new - doc.encoding = 'UTF-8' - root = XML::Node.new 'osm' - root['version'] = API_VERSION - root['generator'] = 'OpenStreetMap server' - doc.root = root - root << to_xml_node() + doc = OSM::API.new.get_xml_doc + doc.root << to_xml_node() return doc end diff --git a/app/models/segment.rb b/app/models/segment.rb index f4909c7d2..5c3c65e34 100644 --- a/app/models/segment.rb +++ b/app/models/segment.rb @@ -67,13 +67,8 @@ class Segment < ActiveRecord::Base end def to_xml - doc = XML::Document.new - doc.encoding = 'UTF-8' - root = XML::Node.new 'osm' - root['version'] = API_VERSION - root['generator'] = 'OpenStreetMap server' - doc.root = root - root << to_xml_node() + doc = OSM::API.new.get_xml_doc + doc.root << to_xml_node() return doc end diff --git a/app/models/user.rb b/app/models/user.rb index a8bc39615..780e0caaf 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,7 @@ -require 'digest/md5' - class User < ActiveRecord::Base + require 'xml/libxml' + require 'digest/md5' + has_many :traces validates_confirmation_of :pass_crypt, :message => 'Password must match the confirmation password' @@ -43,4 +44,16 @@ class User < ActiveRecord::Base return confirmstring end + def to_xml + doc = OSM::API.new.get_xml_doc + doc.root << to_xml_node() + return doc + end + + def to_xml_node + el1 = XML::Node.new 'user' + el1['display_name'] = self.display_name.to_s + el1['account_created'] = self.creation_time.xmlschema + return el1 + end end diff --git a/app/models/way.rb b/app/models/way.rb index a6e0cecf9..7dffcfef1 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -45,15 +45,8 @@ class Way < ActiveRecord::Base end def to_xml - doc = XML::Document.new - doc.encoding = 'UTF-8' - root = XML::Node.new 'osm' - root['version'] = '0.4' - root['generator'] = 'OpenStreetMap server' - - doc.root = root - - root << to_xml_node() + doc = OSM::API.new.get_xml_doc + doc.root << to_xml_node() return doc end diff --git a/config/routes.rb b/config/routes.rb index 59b8fba56..20bf02ce7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,6 +18,8 @@ ActionController::Routing::Routes.draw do |map| map.connect "api/#{API_VERSION}/map", :controller => 'api', :action => 'map' + map.connect "api/#{API_VERSION}/user/details", :controller => 'user', :action => 'api_details' + # web site map.connect '/', :controller => 'site', :action => 'index' diff --git a/lib/osm.rb b/lib/osm.rb index c223f0580..adf34684f 100644 --- a/lib/osm.rb +++ b/lib/osm.rb @@ -307,4 +307,16 @@ module OSM return @doc.to_s end end + + class API + def get_xml_doc + doc = XML::Document.new + doc.encoding = 'UTF-8' + root = XML::Node.new 'osm' + root['version'] = API_VERSION + root['generator'] = 'OpenStreetMap server' + doc.root = root + return doc + end + end end