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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 03d75b9918a795abfabe46f98659846b37979be9
4
- data.tar.gz: 7a501e622f0d8898ad38b65b30e9957fce151aad
3
+ metadata.gz: dc96e3ee965d73c64abc9bb90649c0d328c6c372
4
+ data.tar.gz: f4da7866e12df9d983e8b93e4572f6094dfc42b8
5
5
  SHA512:
6
- metadata.gz: b67c6aac29d022afc1fb48106c436cfcec7f3370d1de7e7a5eb63f339ebe52ef198cd4f7e6797a2865ff7ea1566165ad40f6c413e92de58ccd27bc82140e965b
7
- data.tar.gz: 972a7298efc5729bf9a1cba1f617a9dfdee1096fcc3056dcad1b705d792df8230e5fd48caed3009aeee552594a26c1ad00f2df3a362feee2898d5232fa40cf37
6
+ metadata.gz: 08a82eb2e0d70161128a3045bb329d351ded380709626420c89a99d7b949ba3c5a0eb31d2f17206cc40123f7566a776c57cbd2b86652ef347c3beccb9120008e
7
+ data.tar.gz: 6e28ceff3bbada51ae762ea0d1875c644dec83250199e39815a1695cfe1c0cb382f9bcdd7ced9a3b205144eae10b9f20c5015d2bb0fd69c3fdfe3f3d609b66f8
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![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)
4
4
 
5
- *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.*
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
 
@@ -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'
@@ -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
- on: RETRIABLE_ERRORS do |try|
79
- response = client.get url
80
- if custom_response_decoder
81
- return custom_response_decoder.call(response)
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
- return decode_response_body(response)
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
- api_status = body[:status]
106
- if api_status == "OK" or api_status == "ZERO_RESULTS"
129
+ case body[:status]
130
+ when 'OK', 'ZERO_RESULTS'
107
131
  return body
108
- end
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
- end
113
-
114
- if api_status == "REQUEST_DENIED"
134
+ when 'REQUEST_DENIED'
115
135
  raise GoogleMapsService::Error::RequestDeniedError.new(response), body[:error_message]
116
- end
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 ||= sprintf('Redirect to %s', response.header[:location])
148
+ message = sprintf('Redirect to %s', response.header[:location])
135
149
  raise GoogleMapsService::Error::RedirectError.new(response), message
136
150
  when 401
137
- message ||= 'Unauthorized'
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 ||= 'Invalid request'
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 ||= 'Server error'
144
- raise GoogleMapsService::Error::ServerError.new(response)
157
+ message = 'Server error'
158
+ raise GoogleMapsService::Error::ServerError.new(response), message
145
159
  else
146
- message ||= 'Unknown error'
147
- raise GoogleMapsService::Error::TransmissionError.new(response)
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
- if status == 'INVALID_ARGUMENT'
99
- if error[:message] == 'The provided API key is invalid.'
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
- end
102
- raise GoogleMapsService::Error::InvalidRequestError.new(response), error[:message]
103
- end
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
 
@@ -1,3 +1,3 @@
1
1
  module GoogleMapsService
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
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.2.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-17 00:00:00.000000000 Z
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