Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardsamuel committed Sep 18, 2015
2 parents b547b44 + 2e224c8 commit 1d01c78
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 69 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Gem Version](https://badge.fury.io/rb/google_maps_service.svg)](http://badge.fury.io/rb/google_maps_service) [![Build Status](https://travis-ci.org/edwardsamuel/google-maps-services-ruby.svg?branch=master)](https://travis-ci.org/edwardsamuel/google-maps-services-ruby) [![Dependency Status](https://gemnasium.com/edwardsamuel/google-maps-services-ruby.svg)](https://gemnasium.com/edwardsamuel/google-maps-services-ruby) [![Code Climate](https://codeclimate.com/github/edwardsamuel/google-maps-services-ruby/badges/gpa.svg)](https://codeclimate.com/github/edwardsamuel/google-maps-services-ruby) [![Coverage Status](https://coveralls.io/repos/edwardsamuel/google-maps-services-ruby/badge.svg?branch=master&service=github)](https://coveralls.io/github/edwardsamuel/google-maps-services-ruby?branch=master)

*This is porting of [Python Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-python). All Google Maps Service APIs are supported, but some features (e.g: rate limiting) are not supported right now.*
*This is porting of [Python Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-python).*

## Description

Expand Down
2 changes: 1 addition & 1 deletion google_maps_service.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency 'multi_json', '~> 1.11'
spec.add_runtime_dependency 'hurley', '~> 0.1'
spec.add_runtime_dependency 'retriable', '~> 2.0.2'
spec.add_runtime_dependency 'retriable', '~> 2.0', '>= 2.0.2'
spec.add_runtime_dependency 'ruby-hmac', '~> 0.4.0'
spec.add_development_dependency 'bundler', '~> 1.7'
spec.add_development_dependency 'rake', '~> 10.0'
Expand Down
2 changes: 1 addition & 1 deletion lib/google_maps_service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GoogleMapsService
class << self
attr_accessor :key, :client_id, :client_secret, :connect_timeout, :read_timeout, :retry_timeout
attr_accessor :key, :client_id, :client_secret, :connect_timeout, :read_timeout, :retry_timeout, :queries_per_second

def configure
yield self
Expand Down
76 changes: 45 additions & 31 deletions lib/google_maps_service/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'hurley'
require 'multi_json'
require 'retriable'
require 'thread'

module GoogleMapsService
class Client
Expand Down Expand Up @@ -43,13 +44,28 @@ class Client
# @return [Integer]
attr_reader :retry_timeout

# Number of queries per second permitted.
# If the rate limit is reached, the client will sleep for
# the appropriate amount of time before it runs the current query.
# @return [Integer]
attr_reader :queries_per_second

def initialize(options={})
@key = options[:key] || GoogleMapsService.key
@client_id = options[:client_id] || GoogleMapsService.client_id
@client_secret = options[:client_secret] || GoogleMapsService.client_secret
@connect_timeout = options[:connect_timeout] || GoogleMapsService.connect_timeout
@read_timeout = options[:read_timeout] || GoogleMapsService.read_timeout
@retry_timeout = options[:retry_timeout] || GoogleMapsService.retry_timeout || 60
@queries_per_second = options[:queries_per_second] || GoogleMapsService.queries_per_second

# Prepare "tickets" for calling API
if @queries_per_second
@sent_times = SizedQueue.new @queries_per_second
@queries_per_second.times do
@sent_times << 0
end
end
end

# Get the current HTTP client
Expand All @@ -74,13 +90,21 @@ def new_client
def get(path, params, base_url: DEFAULT_BASE_URL, accepts_client_id: true, custom_response_decoder: nil)
url = base_url + generate_auth_url(path, params, accepts_client_id)

Retriable.retriable timeout: @retry_timeout,
on: RETRIABLE_ERRORS do |try|
response = client.get url
if custom_response_decoder
return custom_response_decoder.call(response)
Retriable.retriable timeout: @retry_timeout, on: RETRIABLE_ERRORS do |try|
# Get/wait the request "ticket" if QPS is configured
# Check for previous request time, it must be more than a second ago before calling new request
if @sent_times
elapsed_since_earliest = Time.now - @sent_times.pop
sleep(1 - elapsed_since_earliest) if elapsed_since_earliest.to_f < 1
end
return decode_response_body(response)

response = client.get url

# Release request "ticket"
@sent_times << Time.now if @sent_times

return custom_response_decoder.call(response) if custom_response_decoder
decode_response_body(response)
end
end

Expand All @@ -102,27 +126,17 @@ def decode_response_body(response)

body = MultiJson.load(response.body, :symbolize_keys => true)

api_status = body[:status]
if api_status == "OK" or api_status == "ZERO_RESULTS"
case body[:status]
when 'OK', 'ZERO_RESULTS'
return body
end

if api_status == "OVER_QUERY_LIMIT"
when 'OVER_QUERY_LIMIT'
raise GoogleMapsService::Error::RateLimitError.new(response), body[:error_message]
end

if api_status == "REQUEST_DENIED"
when 'REQUEST_DENIED'
raise GoogleMapsService::Error::RequestDeniedError.new(response), body[:error_message]
end

if api_status == "INVALID_REQUEST"
when 'INVALID_REQUEST'
raise GoogleMapsService::Error::InvalidRequestError.new(response), body[:error_message]
end

if body[:error_message]
raise GoogleMapsService::Error::ApiError.new(response), body[:error_message]
else
raise GoogleMapsService::Error::ApiError.new(response)
raise GoogleMapsService::Error::ApiError.new(response), body[:error_message]
end
end

Expand All @@ -131,20 +145,20 @@ def check_response_status_code(response)
when 200..300
# Do-nothing
when 301, 302, 303, 307
message ||= sprintf('Redirect to %s', response.header[:location])
message = sprintf('Redirect to %s', response.header[:location])
raise GoogleMapsService::Error::RedirectError.new(response), message
when 401
message ||= 'Unauthorized'
raise GoogleMapsService::Error::ClientError.new(response)
message = 'Unauthorized'
raise GoogleMapsService::Error::ClientError.new(response), message
when 304, 400, 402...500
message ||= 'Invalid request'
raise GoogleMapsService::Error::ClientError.new(response)
message = 'Invalid request'
raise GoogleMapsService::Error::ClientError.new(response), message
when 500..600
message ||= 'Server error'
raise GoogleMapsService::Error::ServerError.new(response)
message = 'Server error'
raise GoogleMapsService::Error::ServerError.new(response), message
else
message ||= 'Unknown error'
raise GoogleMapsService::Error::TransmissionError.new(response)
message = 'Unknown error'
raise GoogleMapsService::Error::Error.new(response), message
end
end

Expand Down
29 changes: 11 additions & 18 deletions lib/google_maps_service/roads.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,18 @@ def extract_roads_body(response)
error = body[:error]
status = error[:status]

if status == 'INVALID_ARGUMENT'
if error[:message] == 'The provided API key is invalid.'
case status
when 'INVALID_ARGUMENT'
if error[:message] == 'The provided API key is invalid.'
raise GoogleMapsService::Error::RequestDeniedError.new(response), error[:message]
end
raise GoogleMapsService::Error::InvalidRequestError.new(response), error[:message]
when 'PERMISSION_DENIED'
raise GoogleMapsService::Error::RequestDeniedError.new(response), error[:message]
end
raise GoogleMapsService::Error::InvalidRequestError.new(response), error[:message]
end

if status == 'PERMISSION_DENIED'
raise GoogleMapsService::Error::RequestDeniedError.new(response), error[:message]
end

if status == 'RESOURCE_EXHAUSTED'
raise GoogleMapsService::Error::RateLimitError.new(response), error[:message]
end

if error.has_key?(:message)
raise GoogleMapsService::Error::ApiError.new(response), error[:message]
else
raise GoogleMapsService::Error::ApiError.new(response)
when 'RESOURCE_EXHAUSTED'
raise GoogleMapsService::Error::RateLimitError.new(response), error[:message]
else
raise GoogleMapsService::Error::ApiError.new(response), error[:message]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/google_maps_service/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GoogleMapsService
VERSION = '0.2.0'
VERSION = '0.3.0'
end
59 changes: 43 additions & 16 deletions spec/google_maps_service/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,49 @@
end
end

# This test assumes that the time to run a mocked query is
# relatively small, eg a few milliseconds. We define a rate of
# 3 queries per second, and run double that, which should take at
# least 1 second but no more than 2.
context 'with total request is double of queries per second' do
let(:queries_per_second) { 3 }
let(:total_request) { queries_per_second * 2 }
let(:client) do
GoogleMapsService::Client.new(key: api_key, queries_per_second: queries_per_second)
end

before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/.*/).
to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, :body => '{"status":"OK","results":[]}')
end

it 'should take between 1-2 seconds' do
start_time = Time.now
total_request.times do
client.geocode(address: "Sesame St.")
end
end_time = Time.now
expect(end_time - start_time).to be_between(1, 2).inclusive
end
end


context 'with client id and secret' do
let(:client) do
client = GoogleMapsService::Client.new(client_id: 'foo', client_secret: 'a2V5')
end

before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/.*/).
to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, :body => '{"status":"OK","results":[]}')
end

it 'should be signed' do
client.geocode(address: 'Sesame St.')
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/geocode/json?address=Sesame+St.&client=foo&signature=fxbWUIcNPZSekVOhp2ul9LW5TpY=')).to have_been_made
end
end

context 'without api key and client secret pair' do
it 'should raise ArgumentError' do
client = GoogleMapsService::Client.new
Expand Down Expand Up @@ -54,22 +97,6 @@
end
end

context 'with client id and secret' do
let(:client) do
client = GoogleMapsService::Client.new(client_id: 'foo', client_secret: 'a2V5')
end

before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/.*/).
to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, :body => '{"status":"OK","results":[]}')
end

it 'should be signed' do
client.geocode(address: 'Sesame St.')
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/geocode/json?address=Sesame+St.&client=foo&signature=fxbWUIcNPZSekVOhp2ul9LW5TpY=')).to have_been_made
end
end

context 'with over query limit' do
before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/geocode\/.*/)
Expand Down

0 comments on commit 1d01c78

Please sign in to comment.