Class: Data::Geo
- Inherits:
-
Object
- Object
- Data::Geo
- Defined in:
- lib/barometer/data/geo.rb
Overview
A simple Geo class
Used to store location data
Instance Attribute Summary collapse
-
#address ⇒ Object
Returns the value of attribute address.
-
#country ⇒ Object
Returns the value of attribute country.
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#latitude ⇒ Object
Returns the value of attribute latitude.
-
#locality ⇒ Object
Returns the value of attribute locality.
-
#longitude ⇒ Object
Returns the value of attribute longitude.
-
#query ⇒ Object
Returns the value of attribute query.
-
#region ⇒ Object
Returns the value of attribute region.
Instance Method Summary collapse
-
#build_from_hash(location = nil) ⇒ Object
build the Geo object from a Hash.
- #coordinates ⇒ Object
-
#initialize(location = nil) ⇒ Geo
constructor
A new instance of Geo.
- #to_s ⇒ Object
Constructor Details
#initialize(location = nil) ⇒ Geo
Returns a new instance of Geo.
12 13 14 15 16 17 |
# File 'lib/barometer/data/geo.rb', line 12 def initialize(location=nil) return unless location raise ArgumentError unless location.is_a?(Hash) self.build_from_hash(location) self end |
Instance Attribute Details
#address ⇒ Object
Returns the value of attribute address.
10 11 12 |
# File 'lib/barometer/data/geo.rb', line 10 def address @address end |
#country ⇒ Object
Returns the value of attribute country.
10 11 12 |
# File 'lib/barometer/data/geo.rb', line 10 def country @country end |
#country_code ⇒ Object
Returns the value of attribute country_code.
10 11 12 |
# File 'lib/barometer/data/geo.rb', line 10 def country_code @country_code end |
#latitude ⇒ Object
Returns the value of attribute latitude.
9 10 11 |
# File 'lib/barometer/data/geo.rb', line 9 def latitude @latitude end |
#locality ⇒ Object
Returns the value of attribute locality.
10 11 12 |
# File 'lib/barometer/data/geo.rb', line 10 def locality @locality end |
#longitude ⇒ Object
Returns the value of attribute longitude.
9 10 11 |
# File 'lib/barometer/data/geo.rb', line 9 def longitude @longitude end |
#query ⇒ Object
Returns the value of attribute query.
9 10 11 |
# File 'lib/barometer/data/geo.rb', line 9 def query @query end |
#region ⇒ Object
Returns the value of attribute region.
10 11 12 |
# File 'lib/barometer/data/geo.rb', line 10 def region @region end |
Instance Method Details
#build_from_hash(location = nil) ⇒ Object
build the Geo object from a Hash
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/barometer/data/geo.rb', line 21 def build_from_hash(location=nil) return nil unless location raise ArgumentError unless location.is_a?(Hash) @query = location["name"] placemark = location["Placemark"] placemark = placemark.first if placemark.is_a?(Array) if placemark && placemark["Point"] && placemark["Point"]["coordinates"] @latitude = placemark["Point"]["coordinates"].split(',')[1].to_f @longitude = placemark["Point"]["coordinates"].split(',')[0].to_f end if placemark && placemark["AddressDetails"] && placemark["AddressDetails"]["Country"] if placemark["AddressDetails"]["Country"]["AdministrativeArea"] if placemark["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"] locality = placemark["AddressDetails"]["Country"]["AdministrativeArea"]["SubAdministrativeArea"]["Locality"] else locality = placemark["AddressDetails"]["Country"]["AdministrativeArea"]["Locality"] end if locality @locality = locality["LocalityName"] end @region = placemark["AddressDetails"]["Country"]["AdministrativeArea"]["AdministrativeAreaName"] end @country = placemark["AddressDetails"]["Country"]["CountryName"] @country_code = placemark["AddressDetails"]["Country"]["CountryNameCode"] @address = placemark["AddressDetails"]["Country"]["AddressLine"] end end |
#coordinates ⇒ Object
51 52 53 |
# File 'lib/barometer/data/geo.rb', line 51 def coordinates [@latitude, @longitude].join(',') end |
#to_s ⇒ Object
55 56 57 58 59 |
# File 'lib/barometer/data/geo.rb', line 55 def to_s s = [@address, @locality, @region, @country || @country_code] s.delete("") s.compact.join(', ') end |