Class: Graticule::Geocoder::Google::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/graticule/geocoder/google.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribs) ⇒ Result

Returns a new instance of Result.


31
32
33
34
35
36
37
# File 'lib/graticule/geocoder/google.rb', line 31

def initialize(attribs)
  @latitude = attribs["geometry"]["location"]["lat"]
  @longitude = attribs["geometry"]["location"]["lng"]
  @formatted_address = attribs["formatted_address"]
  @precision = determine_precision(attribs["types"])
  parse_address_components(attribs["address_components"])
end

Instance Attribute Details

#countryObject

Returns the value of attribute country


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def country
  @country
end

#formatted_addressObject

Returns the value of attribute formatted_address


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def formatted_address
  @formatted_address
end

#latitudeObject

Returns the value of attribute latitude


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def latitude
  @latitude
end

#localityObject

Returns the value of attribute locality


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def locality
  @locality
end

#longitudeObject

Returns the value of attribute longitude


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def longitude
  @longitude
end

#postal_codeObject

Returns the value of attribute postal_code


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def postal_code
  @postal_code
end

#precisionObject

Returns the value of attribute precision


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def precision
  @precision
end

#regionObject

Returns the value of attribute region


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def region
  @region
end

#routeObject

Returns the value of attribute route


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def route
  @route
end

#street_numberObject

Returns the value of attribute street_number


30
31
32
# File 'lib/graticule/geocoder/google.rb', line 30

def street_number
  @street_number
end

Instance Method Details

#determine_precision(types) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/graticule/geocoder/google.rb', line 64

def determine_precision(types)
  precision = Precision::Unknown
  types.each do |type|
    precision = case type
    when "premise", "subpremise"
      Precision::Premise
    when "street_address"
      Precision::Address
    when "route", "intersection"
      Precision::Street
    when "postal_code"
      Precision::PostalCode
    when "locality","sublocality","neighborhood"
      Precision::Locality
    when "administrative_area_level_1", "administrative_area_level_2","administrative_area_level_3"
      Precision::Region
    when "country"
      Precision::Country
    else
      precision
    end
  end
  return precision
end

#parse_address_components(components) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/graticule/geocoder/google.rb', line 39

def parse_address_components(components)
  components.each do |component|
    component["types"].each do |type|
      case type
      when "street_number"
        @street_number = component["short_name"]
      when "route"
        @route = component["short_name"]
      when "locality", "sublocality"
        @locality = component["long_name"] 
      when "administrative_area_level_1"
        @region = component["short_name"]
      when "country"
        @country = component["short_name"]
      when "postal_code"
        @postal_code = component["long_name"]
      end
    end
  end
end

#streetObject


60
61
62
# File 'lib/graticule/geocoder/google.rb', line 60

def street
  "#{@street_number.to_s}#{" " unless @street_number.blank? || @route.blank?}#{@route.to_s}"
end