module MapCoder
Constants
- GOOGLE_API_NOT_FOUND_RESPONSE
- GOOGLE_API_OVER_QUERY_LIMIT_RESPONSE
- GOOGLE_API_SUCCESSFUL_RESPONSE
- REQUEST_URL
Public Class Methods
coordinates(address)
click to toggle source
# File lib/mapcoder.rb, line 18 def self.coordinates(address) raise EmptyAddressError if address.nil? || address.empty? escaped_address = CGI.escape(address) url = "#{REQUEST_URL}?address=#{escaped_address}&sensor=false" attempt_number = 0 loop do attempt_number += 1 begin raw_response = RestClient.get(url) rescue return { status: :api_unavailable } end response = JSON.parse(raw_response) status = response['status'] if status == GOOGLE_API_SUCCESSFUL_RESPONSE coordinates = response['results'].map { |result| result['geometry']['location'] } coordinates.map! { |result| { latitude: result['lat'], longitude: result['lng'] } } return { status: :ok, coordinates: coordinates } elsif status == GOOGLE_API_NOT_FOUND_RESPONSE return { status: :ok, coordinates: [] } elsif status == GOOGLE_API_OVER_QUERY_LIMIT_RESPONSE if attempt_number == 1 # Failed for the first time, sleep for 2 seconds and retry sleep 2 else # Failed again, we are limited for the day return { status: :over_daily_limit } end else raise UnknownGoogleAPIResponseError end end end