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

build the Geo object from a Hash

Raises:

  • (ArgumentError)


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

#coordinatesObject



51
52
53
# File 'lib/barometer/data/geo.rb', line 51

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

#to_sObject



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