Add support for accessing changesets as XML through the API via the
authorChristopher Schmidt <crschmidt@crschmidt.net>
Sun, 11 May 2008 20:03:17 +0000 (20:03 +0000)
committerChristopher Schmidt <crschmidt@crschmidt.net>
Sun, 11 May 2008 20:03:17 +0000 (20:03 +0000)
changeset controller "read" method

app/controllers/changeset_controller.rb
app/models/changeset.rb
config/routes.rb

index f91ae09bfe7bdd1ef1dddcd357c37e564c4a8f8e..3e88eeec32b0425b10ca47c92cc90b057814b8fe 100644 (file)
@@ -45,6 +45,15 @@ class ChangesetController < ApplicationController
       map { |memb| [memb[0], ids[memb[0]][memb[1].to_i] || memb[1], memb[2]] }
     return r
   end
+  
+  def read
+    begin
+      changeset = Changeset.find(params[:id])
+      render :text => changeset.to_xml.to_s, :content_type => "text/xml"
+    rescue ActiveRecord::RecordNotFound
+      render :nothing => true, :status => :not_found
+    end
+  end
 
   def upload
     if not request.put?
index 85621dce234663ea885bfcba29f8b802be47c9fa..983f7544c11710ce330c2958f192915732142352 100644 (file)
@@ -70,4 +70,37 @@ class Changeset < ActiveRecord::Base
       end
     end
   end
+  
+  def to_xml
+    doc = OSM::API.new.get_xml_doc
+    doc.root << to_xml_node()
+    return doc
+  end
+  def to_xml_node(user_display_name_cache = nil)
+    el1 = XML::Node.new 'changeset'
+    el1['id'] = self.id.to_s
+
+    user_display_name_cache = {} if user_display_name_cache.nil?
+
+    if user_display_name_cache and user_display_name_cache.key?(self.user_id)
+      # use the cache if available
+    elsif self.user.data_public?
+      user_display_name_cache[self.user_id] = self.user.display_name
+    else
+      user_display_name_cache[self.user_id] = nil
+    end
+
+    el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
+
+    self.tags.each do |k,v|
+      el2 = XML::Node.new('tag')
+      el2['k'] = k.to_s
+      el2['v'] = v.to_s
+      el1 << el2
+    end
+    
+    el1['created_at'] = self.created_at.xmlschema
+
+    return el1
+  end
 end
index 64293b34de1e095a3f9aef36d6673b2afad90438..9196fae1bb9ebad870a6223e65e9ae3dd803d935 100644 (file)
@@ -2,6 +2,7 @@ ActionController::Routing::Routes.draw do |map|
 
   # API
   map.connect "api/#{API_VERSION}/changeset/create", :controller => 'changeset', :action => 'create'
+  map.connect "api/#{API_VERSION}/changeset/:id", :controller => 'changeset', :action => 'read'
   map.connect "api/#{API_VERSION}/changeset/upload", :controller => 'changeset', :action => 'upload'
   
   map.connect "api/#{API_VERSION}/node/create", :controller => 'node', :action => 'create'