Class: Data::Geo

Inherits:
Object
  • Object
show all
Defined in:
lib/barometer/data/geo.rb

Overview

A simple Geo class

Used to store location data

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location = nil) ⇒ Geo

Returns a new instance of Geo.

Raises:

  • (ArgumentError)


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

#addressObject

Returns the value of attribute address.



10
11
12
# File 'lib/barometer/data/geo.rb', line 10

def address
  @address
end

#countryObject

Returns the value of attribute country.



10
11
12
# File 'lib/barometer/data/geo.rb', line 10

def country
  @country
end

#country_codeObject

Returns the value of attribute country_code.



10
11
12
# File 'lib/barometer/data/geo.rb', line 10

def country_code
  @country_code
end

#latitudeObject

Returns the value of attribute latitude.



9
10
11
# File 'lib/barometer/data/geo.rb', line 9

def latitude
  @latitude
end

#localityObject

Returns the value of attribute locality.



10
11
12
# File 'lib/barometer/data/geo.rb', line 10

def locality
  @locality
end

#longitudeObject

Returns the value of attribute longitude.



9
10
11
# File 'lib/barometer/data/geo.rb', line 9

def longitude
  @longitude
end

#queryObject

Returns the value of attribute query.



9
10
11
# File 'lib/barometer/data/geo.rb', line 9

def query
  @query
end

#regionObject

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

Raises:

  • (ArgumentError)


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

#coordinatesObject



57
58
59
# File 'lib/barometer/data/geo.rb', line 57

def coordinates
  [@latitude, @longitude].join(',')
end

#to_sObject



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