lib/fireeagle/location.rb in jnewland-fireeagle-0.8.0.0 vs lib/fireeagle/location.rb in jnewland-fireeagle-0.8.0.1

- old
+ new

@@ -1,70 +1,55 @@ -#Describes a location -class FireEagle - class Location +module FireEagle + class StringWithExactMatch < String + attr_writer :exact_match - #Initialize a Location from an XML response - def initialize(doc) - doc = Hpricot(doc) unless doc.is_a?(Hpricot::Doc || Hpricot::Elem) - @doc = doc - end + def initialize(value = "") + node = XML::Parser.string(value).parse.root + str = super(node.content) + str.exact_match = node.attributes.to_h["exact-match"] == "true" + node = nil - def label - @label ||= @doc.at("/location/label").innerText rescue nil + str end - #Level of granularity for this Location - def level - @level ||= @doc.at("/location/level").innerText.to_i rescue nil + def exact_match? + @exact_match end + end - #Name of the level of granularity for this Location - def level_name - @level_name ||= @doc.at("/location/level-name").innerText rescue nil - end + # Represents a location + class Location + include HappyMapper - #Human Name for this Location - def name - @name ||= @doc.at("/location/name").innerText rescue nil - end - alias_method :to_s, :name + tag "location" + attribute :best_guess, Boolean, :tag => "best-guess" + element :label, String + element :level, Integer + element :level_name, String, :tag => "level-name" + element :located_at, Time, :tag => "located-at" + element :name, String + element :normal_name, String, :tag => "normal-name" + element :place_id, StringWithExactMatch, :tag => "place-id", :parser => :new, :raw => true + element :query, String + element :woeid, StringWithExactMatch, :parser => :new, :raw => true - #Unique identifier for this place. Pro-tip: This is the same place_id used by Flickr. - def place_id - @place_id ||= @doc.at("/location/place-id").innerText rescue nil - end - - #Numeric unique identifier for this place. - def woeid - @woeid ||= @doc.at("/location/woeid").innerText rescue nil - end + element :_box, GeoRuby::SimpleFeatures::Geometry, :tag => "box", + :namespace => "georss", :parser => :from_georss, :raw => true + element :_point, GeoRuby::SimpleFeatures::Geometry, :tag => "point", + :namespace => "georss", :parser => :from_georss, :raw => true - #The Time at which the User last updated in this Location - def located_at - @located_at ||= Time.parse(@doc.at("/location/located-at").innerText) rescue nil + def best_guess? + best_guess == true end - #The coordinates of the lower corner of a bounding box surrounding this Location - def lower_corner - @georss ||= @doc.at("/location//georss:box").innerText.split.map{ |l| l.to_f } rescue nil - @lower_corner ||= @georss[0..1] rescue nil - end - # The GeoRuby[http://georuby.rubyforge.org/] representation of this location def geom - if @doc.at("/location//georss:box") - @geo ||= GeoRuby::SimpleFeatures::Geometry.from_georss(@doc.at("/location//georss:box").to_s) - elsif @doc.at("/location//georss:point") - @geo ||= GeoRuby::SimpleFeatures::Geometry.from_georss(@doc.at("/location//georss:point").to_s) - else - return nil - end + _point || _box end + alias_method :geo, :geom - - #Is this Location FireEagle's best guess? - def best_guess? - @best_guess ||= @doc.at("/location").attributes["best-guess"] == "true" rescue false - end + def to_s + name + end end -end \ No newline at end of file +end