+++ /dev/null
-class OldSegmentController < ApplicationController
- require 'xml/libxml'
-
- session :off
- after_filter :compress_output
-
- def history
- begin
- segment = Segment.find(params[:id])
-
- doc = OSM::API.new.get_xml_doc
-
- segment.old_segments.each do |old_segment|
- doc.root << old_segment.to_xml_node
- end
-
- render :text => doc.to_s, :content_type => "text/xml"
- rescue ActiveRecord::RecordNotFound
- render :nothing => true, :status => :not_found
- rescue
- render :nothing => true, :status => :internal_server_error
- end
- end
-end
+++ /dev/null
-class OldWaySegmentController < ApplicationController
-end
+++ /dev/null
-class SegmentController < ApplicationController
- require 'xml/libxml'
-
- session :off
- before_filter :authorize, :only => [:create, :update, :delete]
- before_filter :check_availability, :only => [:create, :update, :delete]
- after_filter :compress_output
-
- def create
- if request.put?
- segment = Segment.from_xml(request.raw_post, true)
-
- if segment
- if segment.node_a == segment.node_b
- render :nothing => true, :status => :expectation_failed
- elsif !segment.preconditions_ok?
- render :nothing => true, :status => :precondition_failed
- else
- segment.user_id = @user.id
- segment.from_node = Node.find(segment.node_a.to_i)
- segment.to_node = Node.find(segment.node_b.to_i)
-
- if segment.save_with_history
- render :text => segment.id.to_s, :content_type => "text/plain"
- else
- render :nothing => true, :status => :internal_server_error
- end
- end
- else
- render :nothing => true, :status => :bad_request
- end
- else
- render :nothing => true, :status => :method_not_allowed
- end
- end
-
- def read
- begin
- segment = Segment.find(params[:id])
-
- if segment.visible
- render :text => segment.to_xml.to_s, :content_type => "text/xml"
- else
- render :nothing => true, :status => :gone
- end
- rescue ActiveRecord::RecordNotFound
- render :nothing => true, :status => :not_found
- rescue
- render :nothing => true, :status => :internal_server_error
- end
- end
-
- def update
- begin
- segment = Segment.find(params[:id])
-
- if segment.visible
- new_segment = Segment.from_xml(request.raw_post)
-
- if new_segment and new_segment.id == segment.id
- if new_segment.node_a == new_segment.node_b
- render :nothing => true, :status => :expectation_failed
- elsif !new_segment.preconditions_ok?
- render :nothing => true, :status => :precondition_failed
- else
- segment.user_id = @user.id
- segment.node_a = new_segment.node_a
- segment.node_b = new_segment.node_b
- segment.tags = new_segment.tags
- segment.visible = new_segment.visible
-
- if segment.save_with_history
- render :nothing => true
- else
- render :nothing => true, :status => :internal_server_error
- end
- end
- else
- render :nothing => true, :status => :bad_request
- end
- else
- render :nothing => true, :status => :gone
- end
- rescue ActiveRecord::RecordNotFound
- render :nothing => true, :status => :not_found
- rescue
- render :nothing => true, :status => :internal_server_error
- end
- end
-
- def delete
- begin
- segment = Segment.find(params[:id])
-
- if segment.visible
- if WaySegment.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_segments.id", :conditions => [ "current_ways.visible = 1 AND current_way_segments.segment_id = ?", segment.id ])
- render :nothing => true, :status => :precondition_failed
- else
- segment.user_id = @user.id
- segment.visible = 0
-
- if segment.save_with_history
- render :nothing => true
- else
- render :nothing => true, :status => :internal_server_error
- end
- end
- else
- render :nothing => true, :status => :gone
- end
- rescue ActiveRecord::RecordNotFound
- render :nothing => true, :status => :not_found
- rescue
- render :nothing => true, :status => :internal_server_error
- end
- end
-
- def segments
- ids = params['segments'].split(',').collect { |s| s.to_i }
-
- if ids.length > 0
- doc = OSM::API.new.get_xml_doc
-
- Segment.find(ids).each do |segment|
- doc.root << segment.to_xml_node
- end
-
- render :text => doc.to_s, :content_type => "text/xml"
- else
- render :nothing => true, :status => :bad_request
- end
- end
-
- def segments_for_node
- segmentids = Segment.find(:all, :conditions => ['node_a = ? OR node_b = ?', params[:id], params[:id]]).collect { |s| s.id }.uniq
-
- if segmentids.length > 0
- doc = OSM::API.new.get_xml_doc
-
- Segment.find(segmentids).each do |segment|
- doc.root << segment.to_xml_node
- end
-
- render :text => doc.to_s, :content_type => "text/xml"
- else
- render :nothing => true, :status => :bad_request
- end
- end
-end
+++ /dev/null
-class WaySegmentController < ApplicationController
-
-
-
-end
+++ /dev/null
-module OldSegmentHelper
-end
+++ /dev/null
-module OldWaySegmentHelper
-end
+++ /dev/null
-module SegmentHelper
-end
+++ /dev/null
-module WaySegmentHelper
-end
+++ /dev/null
-class OldSegment < ActiveRecord::Base
- set_table_name 'segments'
-
- validates_presence_of :user_id, :timestamp
- validates_inclusion_of :visible, :in => [ true, false ]
- validates_numericality_of :node_a, :node_b
-
- belongs_to :user
-
- def self.from_segment(segment)
- old_segment = OldSegment.new
- old_segment.node_a = segment.node_a
- old_segment.node_b = segment.node_b
- old_segment.visible = segment.visible
- old_segment.tags = segment.tags
- old_segment.timestamp = segment.timestamp
- old_segment.user_id = segment.user_id
- old_segment.id = segment.id
- return old_segment
- 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
- el1['user'] = self.user.display_name if self.user.data_public?
- Segment.split_tags(el1, self.tags)
- el1['visible'] = self.visible.to_s
- el1['timestamp'] = self.timestamp.xmlschema
- return el1
- end
-end
+++ /dev/null
-class OldWaySegment < ActiveRecord::Base
- belongs_to :user
-
- set_table_name 'way_segments'
-
-end
+++ /dev/null
-class Segment < ActiveRecord::Base
- require 'xml/libxml'
- set_table_name 'current_segments'
-
- validates_presence_of :user_id, :timestamp
- validates_inclusion_of :visible, :in => [ true, false ]
- validates_numericality_of :node_a, :node_b
-
- has_many :old_segments, :foreign_key => :id
- belongs_to :user
-
- # using belongs_to :foreign_key = 'node_*', since if use has_one :foreign_key = 'id', segment preconditions? fails checking for segment id in node table
- belongs_to :from_node, :class_name => 'Node', :foreign_key => 'node_a'
- belongs_to :to_node, :class_name => 'Node', :foreign_key => 'node_b'
-
- def self.from_xml(xml, create=false)
- begin
- p = XML::Parser.new
- p.string = xml
- doc = p.parse
-
- segment = Segment.new
-
- doc.find('//osm/segment').each do |pt|
- segment.node_a = pt['from'].to_i
- segment.node_b = pt['to'].to_i
-
- unless create
- if pt['id'] != '0'
- segment.id = pt['id'].to_i
- end
- end
-
- segment.visible = true
-
- if create
- segment.timestamp = Time.now
- else
- if pt['timestamp']
- segment.timestamp = Time.parse(pt['timestamp'])
- end
- end
-
- tags = []
-
- pt.find('tag').each do |tag|
- tags << [tag['k'],tag['v']]
- end
-
- tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
- tags = '' if tags.nil?
-
- segment.tags = tags
- end
- rescue
- segment = nil
- end
-
- return segment
- end
-
- def save_with_history
- begin
- Segment.transaction do
- self.timestamp = Time.now
- self.save!
- old_segment = OldSegment.from_segment(self)
- old_segment.save!
- end
-
- return true
- rescue
- return nil
- 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 'segment'
- el1['id'] = self.id.to_s
- el1['from'] = self.node_a.to_s
- el1['to'] = self.node_b.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?
-
- Segment.split_tags(el1, self.tags)
- el1['visible'] = self.visible.to_s
- el1['timestamp'] = self.timestamp.xmlschema
- return el1
- end
-
- def self.split_tags(el, tags)
- tags.split(';').each do |tag|
- parts = tag.split('=')
- key = ''
- val = ''
- key = parts[0].strip unless parts[0].nil?
- val = parts[1].strip unless parts[1].nil?
- if key != '' && val != ''
- el2 = XML::Node.new('tag')
- el2['k'] = key.to_s
- el2['v'] = val.to_s
- el << el2
- end
- end
- end
-
- def preconditions_ok?
- from_node and from_node.visible and to_node and to_node.visible
- end
-
-end
+++ /dev/null
-class WaySegment < ActiveRecord::Base
- set_table_name 'current_way_segments'
-
- belongs_to :segment
-end
+++ /dev/null
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-visible_segment:
- id: 1
- node_a: 3
- node_b: 4
- user_id: 1
- visible: 1
- tags: test=yes
- timestamp: 2007-01-01 00:00:00
-
-invisible_segment:
- id: 2
- node_a: 3
- node_b: 4
- user_id: 1
- visible: 0
- tags: test=yes
- timestamp: 2007-01-01 00:00:00
-
-used_segment:
- id: 3
- node_a: 3
- node_b: 4
- user_id: 1
- visible: 1
- tags: test=yes
- timestamp: 2007-01-01 00:00:00
+++ /dev/null
-t1:
- id: 1
- segment_id: 3
- sequence_id: 1
-
-t2:
- id: 2
- segment_id: 3
- sequence_id: 1
+++ /dev/null
-# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
-visible_segment:
- id: 1
- node_a: 3
- node_b: 4
- user_id: 1
- visible: 1
- tags: test=yes
- timestamp: 2007-01-01 00:00:00
-
-invisible_segment:
- id: 2
- node_a: 3
- node_b: 4
- user_id: 1
- visible: 0
- tags: test=yes
- timestamp: 2007-01-01 00:00:00
-
-used_segment:
- id: 3
- node_a: 3
- node_b: 4
- user_id: 1
- visible: 1
- tags: test=yes
- timestamp: 2007-01-01 00:00:00
+++ /dev/null
-t1:
- id: 1
- segment_id: 3
- sequence_id: 1
- version: 1
-
-t2:
- id: 2
- segment_id: 3
- sequence_id: 1
- version: 1
+++ /dev/null
-require File.dirname(__FILE__) + '/../test_helper'
-require 'segment_controller'
-
-# Re-raise errors caught by the controller.
-class SegmentController; def rescue_action(e) raise e end; end
-
-class SegmentControllerTest < Test::Unit::TestCase
- api_fixtures
-
- def setup
- @controller = SegmentController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
- end
-
- def test_create
- # cannot read password from fixture as it is stored as MD5 digest
- basic_authorization("test@openstreetmap.org", "test");
- na = current_nodes(:used_node_1).id
- nb = current_nodes(:used_node_2).id
- content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
- put :create
- # hope for success
- assert_response :success, "segment upload did not return success status"
- # read id of created segment and search for it
- segmentid = @response.body
- checksegment = Segment.find(segmentid)
- assert_not_nil checksegment, "uploaded segment not found in data base after upload"
- # compare values
- assert_equal na, checksegment.node_a, "saved segment does not match requested from-node"
- assert_equal nb, checksegment.node_b, "saved segment does not match requested to-node"
- assert_equal users(:normal_user).id, checksegment.user_id, "saved segment does not belong to user that created it"
- assert_equal true, checksegment.visible, "saved segment is not visible"
- end
-
- def test_create_invalid
- basic_authorization("test@openstreetmap.org", "test");
- # create a segment with one invalid node
- na = current_nodes(:used_node_1).id
- nb = 0
- content("<osm><segment from='#{na}' to='#{nb}' /></osm>")
- put :create
- # expect failure
- assert_response :precondition_failed, "upload of invalid segment did not return 'precondition failed'"
- end
-
- def test_read
- # check that a visible segment is returned properly
- get :read, :id => current_segments(:visible_segment).id
- assert_response :success
- # TODO: check for <segment> tag in return data
-
- # check that an invisible segment is not returned
- get :read, :id => current_segments(:invisible_segment).id
- assert_response :gone
-
- # check chat a non-existent segment is not returned
- get :read, :id => 0
- assert_response :not_found
- end
-
- # this tests deletion restrictions - basic deletion is tested in the unit
- # tests for segment!
- def test_delete
-
- # first try to delete segment without auth
- delete :delete, :id => current_segments(:visible_segment).id
- assert_response :unauthorized
-
- # now set auth
- basic_authorization("test@openstreetmap.org", "test");
-
- # this should work
- delete :delete, :id => current_segments(:visible_segment).id
- assert_response :success
-
- # this won't work since the segment is already deleted
- delete :delete, :id => current_segments(:invisible_segment).id
- assert_response :gone
-
- # this won't work since the segment never existed
- delete :delete, :id => 0
- assert_response :not_found
-
- # this won't work since the segment is in use
- delete :delete, :id => current_segments(:used_segment).id
- assert_response :precondition_failed
- end
-
-
- def basic_authorization(user, pass)
- @request.env["HTTP_AUTHORIZATION"] = "Basic %s" % Base64.encode64("#{user}:#{pass}")
- end
-
- def content(c)
- @request.env["RAW_POST_DATA"] = c
- end
-end
+++ /dev/null
-require File.dirname(__FILE__) + '/../test_helper'
-
-class SegmentTest < Test::Unit::TestCase
- fixtures :current_nodes, :nodes, :current_segments, :segments, :users
- set_fixture_class :current_segments => :Segment
- set_fixture_class :segments => :OldSegment
- set_fixture_class :current_nodes => :Node
- set_fixture_class :nodes => :OldNode
-
- def test_create
-
- segment_template = Segment.new(:node_a => nodes(:used_node_1).id,
- :node_b => nodes(:used_node_2).id,
- :user_id => users(:normal_user).id,
- :visible => 1,
- :tags => "")
- assert segment_template.save_with_history
-
- segment = Segment.find(segment_template.id)
- assert_not_nil segment
- assert_equal segment_template.node_a, segment.node_a
- assert_equal segment_template.node_b, segment.node_b
- assert_equal segment_template.user_id, segment.user_id
- assert_equal segment_template.visible, segment.visible
- assert_equal segment_template.tags, segment.tags
- assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i
-
- assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1
- old_segment = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ])
- assert_not_nil old_segment
- assert_equal segment_template.node_a, old_segment.node_a
- assert_equal segment_template.node_b, old_segment.node_b
- assert_equal segment_template.user_id, old_segment.user_id
- assert_equal segment_template.visible, old_segment.visible
- assert_equal segment_template.tags, old_segment.tags
- assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i
- end
-
- def test_update
- segment_template = Segment.find(1)
- assert_not_nil segment_template
-
- assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1
- old_segment_template = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ])
- assert_not_nil old_segment_template
-
- segment_template.node_a = nodes(:used_node_2).id
- segment_template.node_b = nodes(:used_node_1).id
- segment_template.tags = "updated=yes"
- assert segment_template.save_with_history
-
- segment = Segment.find(segment_template.id)
- assert_not_nil segment
- assert_equal segment_template.node_a, segment.node_a
- assert_equal segment_template.node_b, segment.node_b
- assert_equal segment_template.user_id, segment.user_id
- assert_equal segment_template.visible, segment.visible
- assert_equal segment_template.tags, segment.tags
- assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i
-
- assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 2
- assert_equal OldSegment.find(:all, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]).length, 1
- old_segment = OldSegment.find(:first, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ])
- assert_not_nil old_segment
- assert_equal segment_template.node_a, old_segment.node_a
- assert_equal segment_template.node_b, old_segment.node_b
- assert_equal segment_template.user_id, old_segment.user_id
- assert_equal segment_template.visible, old_segment.visible
- assert_equal segment_template.tags, old_segment.tags
- assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i
- end
-
- def test_delete
- segment_template = Segment.find(1)
- assert_not_nil segment_template
-
- assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 1
- old_segment_template = OldSegment.find(:first, :conditions => [ "id = ?", segment_template.id ])
- assert_not_nil old_segment_template
-
- segment_template.visible = 0
- assert segment_template.save_with_history
-
- segment = Segment.find(segment_template.id)
- assert_not_nil segment
- assert_equal segment_template.node_a, segment.node_a
- assert_equal segment_template.node_b, segment.node_b
- assert_equal segment_template.user_id, segment.user_id
- assert_equal segment_template.visible, segment.visible
- assert_equal segment_template.tags, segment.tags
- assert_equal segment_template.timestamp.to_i, segment.timestamp.to_i
-
- assert_equal OldSegment.find(:all, :conditions => [ "id = ?", segment_template.id ]).length, 2
- assert_equal OldSegment.find(:all, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ]).length, 1
- old_segment = OldSegment.find(:first, :conditions => [ "id = ? and timestamp = ?", segment_template.id, segment_template.timestamp ])
- assert_not_nil old_segment
- assert_equal segment_template.node_a, old_segment.node_a
- assert_equal segment_template.node_b, old_segment.node_b
- assert_equal segment_template.user_id, old_segment.user_id
- assert_equal segment_template.visible, old_segment.visible
- assert_equal segment_template.tags, old_segment.tags
- assert_equal segment_template.timestamp.to_i, old_segment.timestamp.to_i
- end
-end