1 class Segment < ActiveRecord::Base
3 set_table_name 'current_segments'
5 validates_numericality_of :node_a
6 validates_numericality_of :node_b
7 # FIXME validate a nd b exist and are visible
9 has_many :old_segments, :foreign_key => :id
13 def self.from_xml(xml, create=false)
20 doc.find('//osm/segment').each do |pt|
22 segment.node_a = pt['from'].to_i
23 segment.node_b = pt['to'].to_i
26 segment.id = pt['id'].to_i
29 segment.visible = pt['visible'] and pt['visible'] == 'true'
32 segment.timestamp = Time.now
35 segment.timestamp = Time.parse(pt['timestamp'])
41 pt.find('tag').each do |tag|
42 tags << [tag['k'],tag['v']]
45 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
46 tags = '' if tags.nil?
56 Segment.transaction do
58 old_segment = OldSegment.from_segment(self)
62 rescue Exception => ex
68 doc = XML::Document.new
69 doc.encoding = 'UTF-8'
70 root = XML::Node.new 'osm'
71 root['version'] = API_VERSION
72 root['generator'] = 'OpenStreetMap server'
79 el1 = XML::Node.new 'segment'
80 el1['id'] = self.id.to_s
81 el1['from'] = self.node_a.to_s
82 el1['to'] = self.node_b.to_s
83 Segment.split_tags(el1, self.tags)
84 el1['visible'] = self.visible.to_s
85 el1['timestamp'] = self.timestamp.xmlschema
89 def self.split_tags(el, tags)
90 tags.split(';').each do |tag|
91 parts = tag.split('=')
94 key = parts[0].strip unless parts[0].nil?
95 val = parts[1].strip unless parts[1].nil?
96 if key != '' && val != ''
97 el2 = Segment.new('tag')