rails API support for history of node/segment/way + various bugfixes and cleanups

This commit is contained in:
Steve Coast 2006-11-30 18:44:40 +00:00
parent 8ef8761fa5
commit e799022131
13 changed files with 145 additions and 69 deletions

View file

@ -27,6 +27,16 @@ class ApplicationController < ActionController::Base
end end
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
private private
def get_auth_data def get_auth_data
user, pass = '', '' user, pass = '', ''

View file

@ -87,33 +87,4 @@ class NodeController < ApplicationController
end end
def history
response.headers["Content-Type"] = 'application/xml'
node = Node.find(params[:id])
unless node
render :nothing => true, :staus => 404
return
end
doc = XML::Document.new
doc.encoding = 'UTF-8'
root = XML::Node.new 'osm'
root['version'] = '0.4'
root['generator'] = 'OpenStreetMap server'
doc.root = root
node.old_nodes.each do |old_node|
el1 = XML::Node.new 'node'
el1['id'] = old_node.id.to_s
el1['lat'] = old_node.latitude.to_s
el1['lon'] = old_node.longitude.to_s
Node.split_tags(el1, old_node.tags)
el1['visible'] = old_node.visible.to_s
el1['timestamp'] = old_node.timestamp.xmlschema
root << el1
end
render :text => doc.to_s
end
end end

View file

@ -1,2 +1,22 @@
class OldNodeController < ApplicationController class OldNodeController < ApplicationController
def history
response.headers["Content-Type"] = 'application/xml'
node = Node.find(params[:id])
unless node
render :nothing => true, :staus => 404
return
end
doc = get_xml_doc
node.old_nodes.each do |old_node|
doc.root << old_node.to_xml_node
end
render :text => doc.to_s
end
end end

View file

@ -1,2 +1,25 @@
class OldSegmentController < ApplicationController class OldSegmentController < ApplicationController
def history
response.headers["Content-Type"] = 'application/xml'
segment = Segment.find(params[:id])
unless segment
render :nothing => true, :staus => 404
return
end
doc = get_xml_doc
segment.old_segments.each do |old_segment|
doc.root << old_segment.to_xml_node
end
render :text => doc.to_s
end
end end

View file

@ -1,4 +1,21 @@
class OldWayController < ApplicationController class OldWayController < ApplicationController
def history
response.headers["Content-Type"] = 'application/xml'
way = Way.find(params[:id])
unless way
render :nothing => true, :staus => 404
return
end
doc = get_xml_doc
way.old_ways.each do |old_way|
doc.root << old_way.to_xml_node
end
render :text => doc.to_s
end
end end

View file

@ -80,35 +80,4 @@ class SegmentController < ApplicationController
end end
def history
response.headers["Content-Type"] = 'application/xml'
segment = Segment.find(params[:id])
unless segment
render :nothing => true, :staus => 404
return
end
doc = XML::Document.new
doc.encoding = 'UTF-8'
root = XML::Node.new 'osm'
root['version'] = '0.4'
root['generator'] = 'OpenStreetMap server'
doc.root = root
segment.old_segments.each do |old_segment|
el1 = XML::Node.new 'segment'
el1['id'] = old_segment.id.to_s
el1['from'] = old_segment.node_a.to_s
el1['to'] = old_segment.node_b.to_s
Segment.split_tags(el1, old_segment.tags)
el1['visible'] = old_segment.visible.to_s
el1['timestamp'] = old_segment.timestamp.xmlschema
root << el1
end
render :text => doc.to_s
end
end end

View file

@ -51,7 +51,7 @@ class WayController < ApplicationController
render :nothing => true render :nothing => true
return return
when :put when :put
way = Way.from_xml(request.raw_post, true) way = Way.from_xml(request.raw_post)
if way if way
way_in_db = Way.find(way.id) way_in_db = Way.find(way.id)
@ -76,4 +76,5 @@ class WayController < ApplicationController
end end
end end
end end
end end

View file

@ -15,4 +15,15 @@ class OldNode < ActiveRecord::Base
return old_node return old_node
end end
def to_xml_node
el1 = XML::Node.new 'node'
el1['id'] = self.id.to_s
el1['lat'] = self.latitude.to_s
el1['lon'] = self.longitude.to_s
Node.split_tags(el1, self.tags)
el1['visible'] = self.visible.to_s
el1['timestamp'] = self.timestamp.xmlschema
return el1
end
end end

View file

@ -15,4 +15,14 @@ class OldSegment < ActiveRecord::Base
return old_segment return old_segment
end end
def to_xml_node
el1 = XML::Node.new 'segment'
el1['id'] = self.id.to_s
el1['from'] = self.node_a.to_s
el1['to'] = self.node_b.to_s
Segment.split_tags(el1, self.tags)
el1['visible'] = self.visible.to_s
el1['timestamp'] = self.timestamp.xmlschema
return el1
end
end end

View file

@ -14,8 +14,19 @@ class OldWay < ActiveRecord::Base
end end
def save_with_dependencies def save_with_dependencies
# dont touch this unless you really have figured out why it's called
# (Rails doesn't deal well with the old ways table (called 'ways') because
# it doesn't have a unique key. It knows how to insert and auto_increment
# id and get it back but we have that and we want to get the 'version' back
# we could add another column but thats a lot of data. No, set_primary_key
# doesn't work either.
save() save()
self.reload() clear_aggregation_cache
clear_association_cache
@attributes.update(OldWay.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
# ok, you can touch from here on
self.tags.each do |k,v| self.tags.each do |k,v|
tag = OldWayTag.new tag = OldWayTag.new
@ -54,4 +65,35 @@ class OldWay < ActiveRecord::Base
@tags = t @tags = t
end end
# has_many :way_segments, :class_name => 'OldWaySegment', :foreign_key => 'id'
# has_many :way_tags, :class_name => 'OldWayTag', :foreign_key => 'id'
def old_segments
OldWaySegment.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])
end
def old_tags
OldWayTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])
end
def to_xml_node
el1 = XML::Node.new 'way'
el1['id'] = self.id.to_s
el1['visible'] = self.visible.to_s
el1['timestamp'] = self.timestamp.xmlschema
self.old_segments.each do |seg| # FIXME need to make sure they come back in the right order
e = XML::Node.new 'seg'
e['id'] = seg.segment_id.to_s
el1 << e
end
self.old_tags.each do |tag|
e = XML::Node.new 'tag'
e['k'] = tag.k
e['v'] = tag.v
el1 << e
end
return el1
end
end end

View file

@ -26,7 +26,7 @@ class Segment < ActiveRecord::Base
segment.id = pt['id'].to_i segment.id = pt['id'].to_i
end end
segment.visible = pt['visible'] and pt['visible'] == 'true' segment.visible = true
if create if create
segment.timestamp = Time.now segment.timestamp = Time.now
@ -94,7 +94,7 @@ class Segment < ActiveRecord::Base
key = parts[0].strip unless parts[0].nil? key = parts[0].strip unless parts[0].nil?
val = parts[1].strip unless parts[1].nil? val = parts[1].strip unless parts[1].nil?
if key != '' && val != '' if key != '' && val != ''
el2 = Segment.new('tag') el2 = XML::Node.new('tag')
el2['k'] = key.to_s el2['k'] = key.to_s
el2['v'] = val.to_s el2['v'] = val.to_s
el << el2 el << el2

View file

@ -6,6 +6,8 @@ class Way < ActiveRecord::Base
has_many :way_segments, :foreign_key => 'id' has_many :way_segments, :foreign_key => 'id'
has_many :way_tags, :foreign_key => 'id' has_many :way_tags, :foreign_key => 'id'
has_many :old_ways, :foreign_key => :id
set_table_name 'current_ways' set_table_name 'current_ways'
def self.from_xml(xml, create=false) def self.from_xml(xml, create=false)
@ -16,7 +18,7 @@ class Way < ActiveRecord::Base
way = Way.new way = Way.new
doc.find('//osm/way').each do |pt| doc.find('//osm/way').each do |pt|
unless create and pt['id'] == '0' if !create and pt['id'] != '0'
way.id = pt['id'].to_i way.id = pt['id'].to_i
end end

View file

@ -3,16 +3,16 @@ ActionController::Routing::Routes.draw do |map|
# API # API
API_VERSION = '0.4' # change this in envronment.rb too API_VERSION = '0.4' # change this in envronment.rb too
map.connect "api/#{API_VERSION}/node/create", :controller => 'node', :action => 'create' map.connect "api/#{API_VERSION}/node/create", :controller => 'node', :action => 'create'
map.connect "api/#{API_VERSION}/node/:id/history", :controller => 'node', :action => 'history', :id => nil map.connect "api/#{API_VERSION}/node/:id/history", :controller => 'old_node', :action => 'history', :id => nil
map.connect "api/#{API_VERSION}/node/:id", :controller => 'node', :action => 'rest', :id => nil map.connect "api/#{API_VERSION}/node/:id", :controller => 'node', :action => 'rest', :id => nil
map.connect "api/#{API_VERSION}/segment/create", :controller => 'segment', :action => 'create' map.connect "api/#{API_VERSION}/segment/create", :controller => 'segment', :action => 'create'
map.connect "api/#{API_VERSION}/segment/:id/history", :controller => 'segment', :action => 'history' map.connect "api/#{API_VERSION}/segment/:id/history", :controller => 'old_segment', :action => 'history'
map.connect "api/#{API_VERSION}/segment/:id", :controller => 'segment', :action => 'rest' map.connect "api/#{API_VERSION}/segment/:id", :controller => 'segment', :action => 'rest'
map.connect "api/#{API_VERSION}/way/create", :controller => 'way', :action => 'create' map.connect "api/#{API_VERSION}/way/create", :controller => 'way', :action => 'create'
map.connect "api/#{API_VERSION}/way/:id/history", :controller => 'way', :action => 'history' map.connect "api/#{API_VERSION}/way/:id/history", :controller => 'old_way', :action => 'history', :id => nil
map.connect "api/#{API_VERSION}/way/:id", :controller => 'way', :action => 'rest' map.connect "api/#{API_VERSION}/way/:id", :controller => 'way', :action => 'rest', :id => nil
map.connect "api/#{API_VERSION}/map", :controller => 'api', :action => 'map' map.connect "api/#{API_VERSION}/map", :controller => 'api', :action => 'map'