google_maps_service 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/google_maps_service.gemspec +1 -1
- data/lib/google_maps_service.rb +1 -1
- data/lib/google_maps_service/client.rb +45 -31
- data/lib/google_maps_service/roads.rb +11 -18
- data/lib/google_maps_service/version.rb +1 -1
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc96e3ee965d73c64abc9bb90649c0d328c6c372
|
4
|
+
data.tar.gz: f4da7866e12df9d983e8b93e4572f6094dfc42b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 08a82eb2e0d70161128a3045bb329d351ded380709626420c89a99d7b949ba3c5a0eb31d2f17206cc40123f7566a776c57cbd2b86652ef347c3beccb9120008e
|
7
|
+
data.tar.gz: 6e28ceff3bbada51ae762ea0d1875c644dec83250199e39815a1695cfe1c0cb382f9bcdd7ced9a3b205144eae10b9f20c5015d2bb0fd69c3fdfe3f3d609b66f8
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/google_maps_service) [](https://travis-ci.org/edwardsamuel/google-maps-services-ruby) [](https://gemnasium.com/edwardsamuel/google-maps-services-ruby) [](https://codeclimate.com/github/edwardsamuel/google-maps-services-ruby) [](https://coveralls.io/github/edwardsamuel/google-maps-services-ruby?branch=master)
|
4
4
|
|
5
|
-
*This is porting of [Python Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-python)
|
5
|
+
*This is porting of [Python Client for Google Maps Services](https://github.com/googlemaps/google-maps-services-python).*
|
6
6
|
|
7
7
|
## Description
|
8
8
|
|
data/google_maps_service.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.add_runtime_dependency 'multi_json', '~> 1.11'
|
21
21
|
spec.add_runtime_dependency 'hurley', '~> 0.1'
|
22
|
-
spec.add_runtime_dependency 'retriable', '~> 2.0.2'
|
22
|
+
spec.add_runtime_dependency 'retriable', '~> 2.0', '>= 2.0.2'
|
23
23
|
spec.add_runtime_dependency 'ruby-hmac', '~> 0.4.0'
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.7'
|
25
25
|
spec.add_development_dependency 'rake', '~> 10.0'
|
data/lib/google_maps_service.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module GoogleMapsService
|
2
2
|
class << self
|
3
|
-
attr_accessor :key, :client_id, :client_secret, :connect_timeout, :read_timeout, :retry_timeout
|
3
|
+
attr_accessor :key, :client_id, :client_secret, :connect_timeout, :read_timeout, :retry_timeout, :queries_per_second
|
4
4
|
|
5
5
|
def configure
|
6
6
|
yield self
|
@@ -2,6 +2,7 @@ require 'uri'
|
|
2
2
|
require 'hurley'
|
3
3
|
require 'multi_json'
|
4
4
|
require 'retriable'
|
5
|
+
require 'thread'
|
5
6
|
|
6
7
|
module GoogleMapsService
|
7
8
|
class Client
|
@@ -43,6 +44,12 @@ module GoogleMapsService
|
|
43
44
|
# @return [Integer]
|
44
45
|
attr_reader :retry_timeout
|
45
46
|
|
47
|
+
# Number of queries per second permitted.
|
48
|
+
# If the rate limit is reached, the client will sleep for
|
49
|
+
# the appropriate amount of time before it runs the current query.
|
50
|
+
# @return [Integer]
|
51
|
+
attr_reader :queries_per_second
|
52
|
+
|
46
53
|
def initialize(options={})
|
47
54
|
@key = options[:key] || GoogleMapsService.key
|
48
55
|
@client_id = options[:client_id] || GoogleMapsService.client_id
|
@@ -50,6 +57,15 @@ module GoogleMapsService
|
|
50
57
|
@connect_timeout = options[:connect_timeout] || GoogleMapsService.connect_timeout
|
51
58
|
@read_timeout = options[:read_timeout] || GoogleMapsService.read_timeout
|
52
59
|
@retry_timeout = options[:retry_timeout] || GoogleMapsService.retry_timeout || 60
|
60
|
+
@queries_per_second = options[:queries_per_second] || GoogleMapsService.queries_per_second
|
61
|
+
|
62
|
+
# Prepare "tickets" for calling API
|
63
|
+
if @queries_per_second
|
64
|
+
@sent_times = SizedQueue.new @queries_per_second
|
65
|
+
@queries_per_second.times do
|
66
|
+
@sent_times << 0
|
67
|
+
end
|
68
|
+
end
|
53
69
|
end
|
54
70
|
|
55
71
|
# Get the current HTTP client
|
@@ -74,13 +90,21 @@ module GoogleMapsService
|
|
74
90
|
def get(path, params, base_url: DEFAULT_BASE_URL, accepts_client_id: true, custom_response_decoder: nil)
|
75
91
|
url = base_url + generate_auth_url(path, params, accepts_client_id)
|
76
92
|
|
77
|
-
Retriable.retriable timeout: @retry_timeout,
|
78
|
-
|
79
|
-
|
80
|
-
if
|
81
|
-
|
93
|
+
Retriable.retriable timeout: @retry_timeout, on: RETRIABLE_ERRORS do |try|
|
94
|
+
# Get/wait the request "ticket" if QPS is configured
|
95
|
+
# Check for previous request time, it must be more than a second ago before calling new request
|
96
|
+
if @sent_times
|
97
|
+
elapsed_since_earliest = Time.now - @sent_times.pop
|
98
|
+
sleep(1 - elapsed_since_earliest) if elapsed_since_earliest.to_f < 1
|
82
99
|
end
|
83
|
-
|
100
|
+
|
101
|
+
response = client.get url
|
102
|
+
|
103
|
+
# Release request "ticket"
|
104
|
+
@sent_times << Time.now if @sent_times
|
105
|
+
|
106
|
+
return custom_response_decoder.call(response) if custom_response_decoder
|
107
|
+
decode_response_body(response)
|
84
108
|
end
|
85
109
|
end
|
86
110
|
|
@@ -102,27 +126,17 @@ module GoogleMapsService
|
|
102
126
|
|
103
127
|
body = MultiJson.load(response.body, :symbolize_keys => true)
|
104
128
|
|
105
|
-
|
106
|
-
|
129
|
+
case body[:status]
|
130
|
+
when 'OK', 'ZERO_RESULTS'
|
107
131
|
return body
|
108
|
-
|
109
|
-
|
110
|
-
if api_status == "OVER_QUERY_LIMIT"
|
132
|
+
when 'OVER_QUERY_LIMIT'
|
111
133
|
raise GoogleMapsService::Error::RateLimitError.new(response), body[:error_message]
|
112
|
-
|
113
|
-
|
114
|
-
if api_status == "REQUEST_DENIED"
|
134
|
+
when 'REQUEST_DENIED'
|
115
135
|
raise GoogleMapsService::Error::RequestDeniedError.new(response), body[:error_message]
|
116
|
-
|
117
|
-
|
118
|
-
if api_status == "INVALID_REQUEST"
|
136
|
+
when 'INVALID_REQUEST'
|
119
137
|
raise GoogleMapsService::Error::InvalidRequestError.new(response), body[:error_message]
|
120
|
-
end
|
121
|
-
|
122
|
-
if body[:error_message]
|
123
|
-
raise GoogleMapsService::Error::ApiError.new(response), body[:error_message]
|
124
138
|
else
|
125
|
-
raise GoogleMapsService::Error::ApiError.new(response)
|
139
|
+
raise GoogleMapsService::Error::ApiError.new(response), body[:error_message]
|
126
140
|
end
|
127
141
|
end
|
128
142
|
|
@@ -131,20 +145,20 @@ module GoogleMapsService
|
|
131
145
|
when 200..300
|
132
146
|
# Do-nothing
|
133
147
|
when 301, 302, 303, 307
|
134
|
-
message
|
148
|
+
message = sprintf('Redirect to %s', response.header[:location])
|
135
149
|
raise GoogleMapsService::Error::RedirectError.new(response), message
|
136
150
|
when 401
|
137
|
-
message
|
138
|
-
raise GoogleMapsService::Error::ClientError.new(response)
|
151
|
+
message = 'Unauthorized'
|
152
|
+
raise GoogleMapsService::Error::ClientError.new(response), message
|
139
153
|
when 304, 400, 402...500
|
140
|
-
message
|
141
|
-
raise GoogleMapsService::Error::ClientError.new(response)
|
154
|
+
message = 'Invalid request'
|
155
|
+
raise GoogleMapsService::Error::ClientError.new(response), message
|
142
156
|
when 500..600
|
143
|
-
message
|
144
|
-
raise GoogleMapsService::Error::ServerError.new(response)
|
157
|
+
message = 'Server error'
|
158
|
+
raise GoogleMapsService::Error::ServerError.new(response), message
|
145
159
|
else
|
146
|
-
message
|
147
|
-
raise GoogleMapsService::Error::
|
160
|
+
message = 'Unknown error'
|
161
|
+
raise GoogleMapsService::Error::Error.new(response), message
|
148
162
|
end
|
149
163
|
end
|
150
164
|
|
@@ -95,25 +95,18 @@ module GoogleMapsService
|
|
95
95
|
error = body[:error]
|
96
96
|
status = error[:status]
|
97
97
|
|
98
|
-
|
99
|
-
|
98
|
+
case status
|
99
|
+
when 'INVALID_ARGUMENT'
|
100
|
+
if error[:message] == 'The provided API key is invalid.'
|
101
|
+
raise GoogleMapsService::Error::RequestDeniedError.new(response), error[:message]
|
102
|
+
end
|
103
|
+
raise GoogleMapsService::Error::InvalidRequestError.new(response), error[:message]
|
104
|
+
when 'PERMISSION_DENIED'
|
100
105
|
raise GoogleMapsService::Error::RequestDeniedError.new(response), error[:message]
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
if status == 'PERMISSION_DENIED'
|
106
|
-
raise GoogleMapsService::Error::RequestDeniedError.new(response), error[:message]
|
107
|
-
end
|
108
|
-
|
109
|
-
if status == 'RESOURCE_EXHAUSTED'
|
110
|
-
raise GoogleMapsService::Error::RateLimitError.new(response), error[:message]
|
111
|
-
end
|
112
|
-
|
113
|
-
if error.has_key?(:message)
|
114
|
-
raise GoogleMapsService::Error::ApiError.new(response), error[:message]
|
115
|
-
else
|
116
|
-
raise GoogleMapsService::Error::ApiError.new(response)
|
106
|
+
when 'RESOURCE_EXHAUSTED'
|
107
|
+
raise GoogleMapsService::Error::RateLimitError.new(response), error[:message]
|
108
|
+
else
|
109
|
+
raise GoogleMapsService::Error::ApiError.new(response), error[:message]
|
117
110
|
end
|
118
111
|
end
|
119
112
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_maps_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edward Samuel Pasaribu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -43,6 +43,9 @@ dependencies:
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
- - ">="
|
46
49
|
- !ruby/object:Gem::Version
|
47
50
|
version: 2.0.2
|
48
51
|
type: :runtime
|
@@ -50,6 +53,9 @@ dependencies:
|
|
50
53
|
version_requirements: !ruby/object:Gem::Requirement
|
51
54
|
requirements:
|
52
55
|
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '2.0'
|
58
|
+
- - ">="
|
53
59
|
- !ruby/object:Gem::Version
|
54
60
|
version: 2.0.2
|
55
61
|
- !ruby/object:Gem::Dependency
|