Class: Gmaps4rails::Geocoder

Inherits:
Object
  • Object
show all
Includes:
BaseNetMethods
Defined in:
lib/gmaps4rails/api_wrappers/geocoder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BaseNetMethods

#base_url, #checked_google_response, #get_response, #parsed_response, #response, #valid_parsed_response?, #valid_response?

Constructor Details

#initialize(address, options = {}) ⇒ Geocoder

Returns a new instance of Geocoder.



8
9
10
11
12
13
14
15
# File 'lib/gmaps4rails/api_wrappers/geocoder.rb', line 8

def initialize(address, options = {})
  raise Gmaps4rails::GeocodeInvalidQuery, "You must provide an address" if address.empty?
  
  @address  = address
  @language = options[:language] || "en"
  @raw      = options[:raw]      || false
  @protocol = options[:protocol] || "http"
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



6
7
8
# File 'lib/gmaps4rails/api_wrappers/geocoder.rb', line 6

def address
  @address
end

#languageObject (readonly)

Returns the value of attribute language.



6
7
8
# File 'lib/gmaps4rails/api_wrappers/geocoder.rb', line 6

def language
  @language
end

#protocolObject (readonly)

Returns the value of attribute protocol.



6
7
8
# File 'lib/gmaps4rails/api_wrappers/geocoder.rb', line 6

def protocol
  @protocol
end

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/gmaps4rails/api_wrappers/geocoder.rb', line 6

def raw
  @raw
end

Instance Method Details

#get_coordinatesObject

returns an array of hashes with the following keys:

  • lat: mandatory for acts_as_gmappable

  • lng: mandatory for acts_as_gmappable

  • matched_address: facultative

  • bounds: facultative

  • full_data: facultative



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gmaps4rails/api_wrappers/geocoder.rb', line 23

def get_coordinates
  checked_google_response do
    return parsed_response if raw
    parsed_response["results"].inject([]) do |memo, result|
      memo << { 
               :lat             => result["geometry"]["location"]["lat"], 
               :lng             => result["geometry"]["location"]["lng"],
               :matched_address => result["formatted_address"],
               :bounds          => result["geometry"]["bounds"],
               :full_data       => result
              }
    end
  end
end