Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardsamuel committed Sep 22, 2015
1 parent efd15ad commit 43fc03e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Using required positional and optional named parameters (_breaking changes_)
* Documentation with examples
* Documentation using markdown syntax
* Fix QPS bug: ensure number of queue items is the given value

## 0.3.0

Expand Down
6 changes: 3 additions & 3 deletions lib/google_maps_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ class << self
# Global request_options.
# @see Client#initialize-instance_method
# @return [Hurley::RequestOptions]
attr_reader :request_options
attr_accessor :request_options

# Global ssl_options.
# @see Client#initialize-instance_method
# @return [Hurley::SslOptions]
attr_reader :ssl_options
attr_accessor :ssl_options

# Global connection.
# @see Client#initialize-instance_method
# @return [Object]
attr_reader :connection
attr_accessor :connection

def configure
yield self
Expand Down
15 changes: 5 additions & 10 deletions lib/google_maps_service/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,15 @@ def check_response_status_code(response)
when 200..300
# Do-nothing
when 301, 302, 303, 307
message = sprintf('Redirect to %s', response.header[:location])
raise GoogleMapsService::Error::RedirectError.new(response), message
raise GoogleMapsService::Error::RedirectError.new(response), sprintf('Redirect to %s', response.header[:location])
when 401
message = 'Unauthorized'
raise GoogleMapsService::Error::ClientError.new(response), message
raise GoogleMapsService::Error::ClientError.new(response), 'Unauthorized'
when 304, 400, 402...500
message = 'Invalid request'
raise GoogleMapsService::Error::ClientError.new(response), message
raise GoogleMapsService::Error::ClientError.new(response), 'Invalid request'
when 500..600
message = 'Server error'
raise GoogleMapsService::Error::ServerError.new(response), message
raise GoogleMapsService::Error::ServerError.new(response), 'Server error'
else
message = 'Unknown error'
raise GoogleMapsService::Error::BaseError.new(response), message
raise ArgumentError, 'Invalid response status code'
end
end

Expand Down
24 changes: 10 additions & 14 deletions lib/google_maps_service/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module Url
# @param [String] secret The key used for the signature, base64 encoded.
# @param [String] payload The payload to sign.
#
# @return [String]
# @return [String] Base64-encoded HMAC-SHA1 signature
#
# @todo Use OpenSSL instead of Ruby-HMAC
def sign_hmac(secret, payload)
require 'base64'
require 'hmac'
Expand Down Expand Up @@ -40,9 +42,6 @@ def urlencode_params(params)
unquote_unreserved(URI.encode_www_form(params))
end

# The unreserved URI characters (RFC 3986)
UNRESERVED_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"

# Un-escape any percent-escape sequences in a URI that are unreserved
# characters. This leaves all reserved, illegal and non-ASCII bytes encoded.
#
Expand All @@ -55,20 +54,17 @@ def unquote_unreserved(uri)
(1..parts.length-1).each do |i|
h = parts[i][0..1]

if h.length == 2 and !h.match(/[^A-Za-z0-9]/)
c = h.to_i(16).chr

if UNRESERVED_SET.include?(c)
parts[i] = c + parts[i][2..-1]
else
parts[i] = "%#{parts[i]}"
end
if h =~ /^([\h]{2})(.*)/ and c = $1.to_i(16).chr and UNRESERVED_SET.include?(c)
parts[i] = c + $2
else
parts[i] = "%#{parts[i]}"
parts[i] = '%' + parts[i]
end
end

return parts.join
parts.join
end

# The unreserved URI characters (RFC 3986)
UNRESERVED_SET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~"
end
end
108 changes: 64 additions & 44 deletions spec/google_maps_service/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
end
end

context 'with global parameters' do
before(:example) do
GoogleMapsService.configure do |config|
config.key = 'AIZaGLOBAL'
end
end

it 'should take global parameters' do
client = GoogleMapsService::Client.new
expect(client.key).to eq('AIZaGLOBAL')
end

after(:example) do
GoogleMapsService.configure do |config|
config.key = nil
end
end
end

context 'with client id and secret' do
let(:client) do
Expand All @@ -46,69 +64,71 @@
end
end

context 'without api key and client secret pair' do
it 'should raise ArgumentError' do
client = GoogleMapsService::Client.new
expect { client.directions("Sydney", "Melbourne") }.to raise_error ArgumentError
context 'error handling' do
context 'without api key and client secret pair' do
it 'should raise ArgumentError' do
client = GoogleMapsService::Client.new
expect { client.directions("Sydney", "Melbourne") }.to raise_error ArgumentError
end
end
end

context 'with invalid api key' do
let(:client) do
client = GoogleMapsService::Client.new(key: "AIzaINVALID")
end
context 'with invalid api key' do
let(:client) do
client = GoogleMapsService::Client.new(key: "AIzaINVALID")
end

before(:example) do
json = <<EOF
before(:example) do
json = <<EOF
{
"error_message": "The provided API key is invalid.",
"routes": [],
"status": "REQUEST_DENIED"
}
EOF
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/.*/)
.to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, body: json)
end
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/.*/)
.to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, body: json)
end

it 'should raise GoogleMapsService::Error::RequestDeniedError' do
expect { client.directions("Sydney", "Melbourne") }.to raise_error GoogleMapsService::Error::RequestDeniedError
it 'should raise GoogleMapsService::Error::RequestDeniedError' do
expect { client.directions("Sydney", "Melbourne") }.to raise_error GoogleMapsService::Error::RequestDeniedError
end
end
end

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

it 'should make request twice' do
results = client.geocode('Sydney')
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/geocode/json?key=%s&address=Sydney' % api_key)).to have_been_made.times(2)
it 'should make request twice' do
results = client.geocode('Sydney')
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/geocode/json?key=%s&address=Sydney' % api_key)).to have_been_made.times(2)
end
end
end

context 'with server error' do
before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/geocode\/.*/)
.to_return(:status => 500, body: 'Internal server error.').then
.to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, body: '{"status":"OK","results":[]}')
end
context 'with server error and then success' do
before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/geocode\/.*/)
.to_return(:status => 500, body: 'Internal server error.').then
.to_return(:status => 200, headers: { 'Content-Type' => 'application/json' }, body: '{"status":"OK","results":[]}')
end

it 'should make request twice' do
results = client.geocode('Sydney')
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/geocode/json?key=%s&address=Sydney' % api_key)).to have_been_made.times(2)
it 'should make request twice' do
results = client.geocode('Sydney')
expect(a_request(:get, 'https://maps.googleapis.com/maps/api/geocode/json?key=%s&address=Sydney' % api_key)).to have_been_made.times(2)
end
end
end

context 'with connection failed' do
before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/geocode\/.*/)
.to_raise(Hurley::ConnectionFailed)
end
context 'with connection failed' do
before(:example) do
stub_request(:get, /https:\/\/maps.googleapis.com\/maps\/api\/geocode\/.*/)
.to_raise(Hurley::ConnectionFailed)
end

it 'should raise Hurley::ConnectionFailed' do
expect { client.geocode(address: 'Sydney') }.to raise_error Hurley::ConnectionFailed
it 'should raise Hurley::ConnectionFailed' do
expect { client.geocode(address: 'Sydney') }.to raise_error Hurley::ConnectionFailed
end
end
end
end

0 comments on commit 43fc03e

Please sign in to comment.