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
- #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
19 20 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 50 51 52 53 54 55 |
# File 'lib/barometer/data/geo.rb', line 19 def build_from_hash(location=nil) return nil unless location raise ArgumentError unless location.is_a?(Hash) if location["geometry"] && location["geometry"]["location"] @latitude = location["geometry"]["location"]["lat"].to_f @longitude = location["geometry"]["location"]["lng"].to_f end query_parts = [] if location["address_components"] location["address_components"].each do |address_components| skip unless address_components["types"] # sublocality trumps locality if address_components["types"].include?('sublocality') @locality = address_components["long_name"] end if address_components["types"].include?('locality') @locality ||= address_components["long_name"] end if address_components["types"].include?('administrative_area_level_1') #@region = address_components["long_name"] @region = address_components["short_name"] end if address_components["types"].include?('country') @country = address_components["long_name"] @country_code = address_components["short_name"] end if !(address_components["types"] & location["types"]).empty? query_parts << address_components["long_name"] end end end @query = query_parts.join(', ') @address = "" end |
#coordinates ⇒ Object
57 58 59 |
# File 'lib/barometer/data/geo.rb', line 57 def coordinates [@latitude, @longitude].join(',') end |
#to_s ⇒ Object
61 62 63 64 65 |
# File 'lib/barometer/data/geo.rb', line 61 def to_s s = [@address, @locality, @region, @country || @country_code] s.delete("") s.compact.join(', ') end |