Class: Geocoder::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Response

Initialize the response and check for errors in the Status code

Raises:

  • (StandardError)

See Also:



11
12
13
14
15
# File 'lib/geocoder/response.rb', line 11

def initialize(json)
  @json = json
  @hash = JSON.parse(json)
  check_status
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



4
5
6
# File 'lib/geocoder/response.rb', line 4

def json
  @json
end

Instance Method Details

#addressesArray<Address>

Return an array of addresses that matched the search

Returns:



21
22
23
# File 'lib/geocoder/response.rb', line 21

def addresses
  @addresses ||= @hash["Placemark"].map {|place| Address.new(place)}
end

#check_statusObject

Check the status returned by Google and throw exception on results that are not 200

Raises:

  • (StandardError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/geocoder/response.rb', line 30

def check_status
  case @hash["Status"]["code"]
  when 200 then
    true
  when 500
    raise ServerError, "Unknown server error"
  when 601
    raise MissingAddressError, "Missing address"
  when 602
    raise UnknownAddressError, "Unknown address"
  when 603
    raise UnavailableAddressError, "Unavailable address"
  when 610
    raise InvalidMapKeyError, "Invalid map key"
  when 620
    raise TooManyQueriesError, "Too many queries for map key"
  else
    raise UnknownError, "Unknown error: #{@hash['code']}"
  end
end