Class: Geocoda::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/geocoda/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil) ⇒ Client

Initialize the Client

If the key is passed it will be used in place of the default key



21
22
23
24
25
# File 'lib/geocoda/client.rb', line 21

def initialize(key=nil)
  @key ||= self.class.key
  @http = Net::HTTP::Persistent.new
  @url  = 'http://maps.google.com/maps/geo'
end

Class Method Details

.keyObject

Returns the default key



7
8
9
# File 'lib/geocoda/client.rb', line 7

def self.key
  @key ||= ""
end

.key=(key) ⇒ Object

Sets the default key



13
14
15
# File 'lib/geocoda/client.rb', line 13

def self.key=(key)
  @key = key
end

Instance Method Details

#first(address) ⇒ Address

Search for the first address that matches a string

Parameters:

  • Address (String)

    required for search

Returns:

Raises:

  • (StandardError)

    on errors

See Also:



52
53
54
# File 'lib/geocoda/client.rb', line 52

def first(address)
  search.first
end

#search(address) ⇒ Array<Address>

Search for addresses that match a string

Parameters:

  • Address (String)

    required for search

Returns:

  • (Array<Address>)

    An array of addresses

Raises:

  • (StandardError)

    on errors



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/geocoda/client.rb', line 33

def search(address)
  begin
    response = @http.request(URI.parse(@url + hash_to_string(:q => address)))
  rescue StandardError => e
    raise(ConnectionError, e.message)
  end
  unless response.code == "200"
    raise(ConnectionError, "Response Code: #{response.code}")
  end
  Response.new(response.body).addresses
end