Class: GCoder::GeocodingAPI::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/gcoder/geocoding_api.rb

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Response

Returns a new instance of Response.



90
91
92
93
# File 'lib/gcoder/geocoding_api.rb', line 90

def initialize(request)
  @request = request
  @response = JSON.parse(@request.http_get)
end

Instance Method Details

#accuracyObject



131
132
133
# File 'lib/gcoder/geocoding_api.rb', line 131

def accuracy
  address_details['Accuracy']
end

#administrative_area_nameObject



151
152
153
# File 'lib/gcoder/geocoding_api.rb', line 151

def administrative_area_name
  administrative_area['AdministrativeAreaName']
end

#boxObject



124
125
126
127
128
129
# File 'lib/gcoder/geocoding_api.rb', line 124

def box
  { :north => latlon_box['north'],
    :south => latlon_box['south'],
    :east => latlon_box['east'],
    :west => latlon_box['west'] }
end

#country_codeObject



147
148
149
# File 'lib/gcoder/geocoding_api.rb', line 147

def country_code
  country['CountryNameCode']
end

#country_nameObject



143
144
145
# File 'lib/gcoder/geocoding_api.rb', line 143

def country_name
  country['CountryName']
end

#latitudeObject



135
136
137
# File 'lib/gcoder/geocoding_api.rb', line 135

def latitude
  coordinates[1]
end

#longitudeObject



139
140
141
# File 'lib/gcoder/geocoding_api.rb', line 139

def longitude
  coordinates[0]
end

#statusObject



95
96
97
# File 'lib/gcoder/geocoding_api.rb', line 95

def status
  @response['Status']['code']
end

#to_hObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gcoder/geocoding_api.rb', line 112

def to_h
  { :accuracy => accuracy,
    :country => {
      :name => country_name,
      :code => country_code,
      :administrative_area => administrative_area_name },
    :point => {
      :longitude => longitude,
      :latitude => latitude },
    :box => box }
end

#validate!Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gcoder/geocoding_api.rb', line 99

def validate!
  case status
  when 400
    raise Errors::APIMalformedRequestError, 'The GMaps Geo API has ' \
    'indicated that the request is not formed correctly: ' \
    "(#{@request.uri})\n\n#{@request.inspect}"
  when 602
    raise Errors::APIGeocodingError, 'The GMaps Geo API has indicated ' \
    "that it is not able to geocode the request: (#{@request.uri})" \
    "\n\n#{@request.inspect}"
  end
end