require 'rexml/document' require 'rexml/xpath' module CanControlsGateway class CursorVector attr_reader :x, :y def initialize xml_node @xml_node = REXML::Document.new(xml_node.to_s) @x = nil @y = nil parse_xml end def is_valid? if (@y && @x) true else false end end def dimensions if is_valid? 2 else 0 end end def to_s if is_valid? "#{x},#{y}" else "" end end private def parse_xml my_vector_data = REXML::XPath.first(@xml_node, "/position").text my_vector_data.to_s.strip! my_vector_data = my_vector_data.split(",") if my_vector_data.size > 1 @x = Float(my_vector_data[0].to_s.strip) @y = Float(my_vector_data[1].to_s.strip.to_f) else @x, @y = nil end rescue @x, @y = nil end end end