google_places 0.0.4 → 0.0.5
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.
- data/Gemfile.lock +1 -1
- data/google_places.gemspec +1 -1
- data/lib/google_places.rb +1 -1
- data/lib/google_places/error.rb +16 -0
- data/lib/google_places/request.rb +44 -1
- data/lib/google_places/spot.rb +6 -2
- data/spec/google_places/request_spec.rb +142 -16
- data/spec/google_places/spot_spec.rb +2 -2
- metadata +5 -4
data/Gemfile.lock
CHANGED
data/google_places.gemspec
CHANGED
data/lib/google_places.rb
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
module GooglePlaces
|
2
|
+
class OverQueryLimitError < HTTParty::ResponseError
|
3
|
+
end
|
4
|
+
|
5
|
+
class RequestDeniedError < HTTParty::ResponseError
|
6
|
+
end
|
7
|
+
|
8
|
+
class InvalidRequestError < HTTParty::ResponseError
|
9
|
+
end
|
10
|
+
|
11
|
+
class RetryError < HTTParty::ResponseError
|
12
|
+
end
|
13
|
+
|
14
|
+
class RetryTimeoutError < HTTParty::ResponseError
|
15
|
+
end
|
16
|
+
end
|
@@ -21,11 +21,54 @@ module GooglePlaces
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def initialize(url, options)
|
24
|
+
retry_options = options.delete(:retry_options) || {}
|
25
|
+
|
26
|
+
retry_options[:status] ||= []
|
27
|
+
retry_options[:max] ||= 0
|
28
|
+
retry_options[:delay] ||= 5
|
29
|
+
|
30
|
+
retry_options[:status] = [retry_options[:status]] unless retry_options[:status].is_a?(Array)
|
31
|
+
|
24
32
|
@response = self.class.get(url, :query => options)
|
33
|
+
|
34
|
+
return unless retry_options[:max] > 0 && retry_options[:status].include?(@response.parsed_response['status'])
|
35
|
+
|
36
|
+
retry_request = proc do
|
37
|
+
for i in (1..retry_options[:max])
|
38
|
+
sleep(retry_options[:delay])
|
39
|
+
|
40
|
+
@response = self.class.get(url, :query => options)
|
41
|
+
|
42
|
+
break unless retry_options[:status].include?(@response.parsed_response['status'])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
if retry_options[:timeout]
|
47
|
+
begin
|
48
|
+
Timeout::timeout(retry_options[:timeout]) do
|
49
|
+
retry_request.call
|
50
|
+
end
|
51
|
+
rescue Timeout::Error
|
52
|
+
raise RetryTimeoutError.new(@response)
|
53
|
+
end
|
54
|
+
else
|
55
|
+
retry_request.call
|
56
|
+
|
57
|
+
raise RetryError.new(@response) if retry_options[:status].include?(@response.parsed_response['status'])
|
58
|
+
end
|
25
59
|
end
|
26
60
|
|
27
61
|
def parsed_response
|
28
|
-
@response.parsed_response
|
62
|
+
case @response.parsed_response['status']
|
63
|
+
when 'OK', 'ZERO_RESULTS'
|
64
|
+
@response.parsed_response
|
65
|
+
when 'OVER_QUERY_LIMIT'
|
66
|
+
raise OverQueryLimitError.new(@response)
|
67
|
+
when 'REQUEST_DENIED'
|
68
|
+
raise RequestDeniedError.new(@response)
|
69
|
+
when 'INVALID_REQUEST'
|
70
|
+
raise InvalidRequestError.new(@response)
|
71
|
+
end
|
29
72
|
end
|
30
73
|
|
31
74
|
end
|
data/lib/google_places/spot.rb
CHANGED
@@ -11,6 +11,7 @@ module GooglePlaces
|
|
11
11
|
language = options.delete(:language)
|
12
12
|
location = Location.new(lat, lng)
|
13
13
|
exclude = options.delete(:exclude) || []
|
14
|
+
retry_options = options.delete(:retry_options) || {}
|
14
15
|
|
15
16
|
exclude = [exclude] unless exclude.is_a?(Array)
|
16
17
|
|
@@ -21,7 +22,8 @@ module GooglePlaces
|
|
21
22
|
:key => api_key,
|
22
23
|
:name => name,
|
23
24
|
:language => language,
|
24
|
-
:keyword => keyword
|
25
|
+
:keyword => keyword,
|
26
|
+
:retry_options => retry_options
|
25
27
|
}
|
26
28
|
|
27
29
|
# Accept Types as a string or array
|
@@ -39,12 +41,14 @@ module GooglePlaces
|
|
39
41
|
def self.find(reference, api_key, options = {})
|
40
42
|
sensor = options.delete(:sensor) || false
|
41
43
|
language = options.delete(:language)
|
44
|
+
retry_options = options.delete(:retry_options) || {}
|
42
45
|
|
43
46
|
response = Request.spot(
|
44
47
|
:reference => reference,
|
45
48
|
:sensor => sensor,
|
46
49
|
:key => api_key,
|
47
|
-
:language => language
|
50
|
+
:language => language,
|
51
|
+
:retry_options => retry_options
|
48
52
|
)
|
49
53
|
|
50
54
|
self.new(response['result'])
|
@@ -12,29 +12,155 @@ describe GooglePlaces::Request do
|
|
12
12
|
context 'Listing spots' do
|
13
13
|
use_vcr_cassette 'list_spots'
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
context 'with valid options' do
|
16
|
+
it 'should retrieve a list of spots' do
|
17
|
+
response = GooglePlaces::Request.spots(
|
18
|
+
:location => @location,
|
19
|
+
:radius => @radius,
|
20
|
+
:sensor => @sensor,
|
21
|
+
:key => api_key
|
22
|
+
)
|
23
|
+
|
24
|
+
response['results'].should_not be_empty
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'with missing sensor' do
|
29
|
+
it do
|
30
|
+
lambda {
|
31
|
+
GooglePlaces::Request.spots(
|
32
|
+
:location => @location,
|
33
|
+
:radius => @radius,
|
34
|
+
:key => api_key
|
35
|
+
)
|
36
|
+
}.should raise_error GooglePlaces::RequestDeniedError
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'without location' do
|
41
|
+
context 'without retry options' do
|
42
|
+
it do
|
43
|
+
lambda {
|
44
|
+
GooglePlaces::Request.spots(
|
45
|
+
:radius => @radius,
|
46
|
+
:sensor => @sensor,
|
47
|
+
:key => api_key
|
48
|
+
)
|
49
|
+
}.should raise_error GooglePlaces::InvalidRequestError
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'with retry options' do
|
54
|
+
context 'without timeout' do
|
55
|
+
it do
|
56
|
+
lambda {
|
57
|
+
GooglePlaces::Request.spots(
|
58
|
+
:radius => @radius,
|
59
|
+
:sensor => @sensor,
|
60
|
+
:key => api_key,
|
61
|
+
:retry_options => {
|
62
|
+
:max => 3,
|
63
|
+
:status => 'INVALID_REQUEST',
|
64
|
+
:delay => 1
|
65
|
+
}
|
66
|
+
)
|
67
|
+
}.should raise_error GooglePlaces::RetryError
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'with timeout' do
|
72
|
+
it do
|
73
|
+
lambda {
|
74
|
+
GooglePlaces::Request.spots(
|
75
|
+
:radius => @radius,
|
76
|
+
:sensor => @sensor,
|
77
|
+
:key => api_key,
|
78
|
+
:retry_options => {
|
79
|
+
:max => 3,
|
80
|
+
:status => 'INVALID_REQUEST',
|
81
|
+
:delay => 10,
|
82
|
+
:timeout => 1
|
83
|
+
}
|
84
|
+
)
|
85
|
+
}.should raise_error GooglePlaces::RetryTimeoutError
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
24
89
|
end
|
25
90
|
end
|
26
91
|
|
27
92
|
context 'Spot details' do
|
28
93
|
use_vcr_cassette 'single_spot'
|
29
94
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
95
|
+
context 'with valid options' do
|
96
|
+
it 'should retrieve a single spot' do
|
97
|
+
response = GooglePlaces::Request.spot(
|
98
|
+
:reference => @reference,
|
99
|
+
:sensor => @sensor,
|
100
|
+
:key => api_key
|
101
|
+
)
|
102
|
+
|
103
|
+
response['result'].should_not be_empty
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'with missing sensor' do
|
108
|
+
it do
|
109
|
+
lambda {
|
110
|
+
GooglePlaces::Request.spot(
|
111
|
+
:reference => @reference,
|
112
|
+
:key => api_key
|
113
|
+
)
|
114
|
+
}.should raise_error GooglePlaces::RequestDeniedError
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'with missing reference' do
|
119
|
+
context 'without retry options' do
|
120
|
+
it do
|
121
|
+
lambda {
|
122
|
+
GooglePlaces::Request.spot(
|
123
|
+
:sensor => @sensor,
|
124
|
+
:key => api_key
|
125
|
+
)
|
126
|
+
}.should raise_error GooglePlaces::InvalidRequestError
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'with retry options' do
|
131
|
+
context 'without timeout' do
|
132
|
+
it do
|
133
|
+
lambda {
|
134
|
+
GooglePlaces::Request.spot(
|
135
|
+
:sensor => @sensor,
|
136
|
+
:key => api_key,
|
137
|
+
:retry_options => {
|
138
|
+
:max => 3,
|
139
|
+
:status => 'INVALID_REQUEST',
|
140
|
+
:delay => 1
|
141
|
+
}
|
142
|
+
)
|
143
|
+
}.should raise_error GooglePlaces::RetryError
|
144
|
+
end
|
145
|
+
end
|
36
146
|
|
37
|
-
|
147
|
+
context 'with timeout' do
|
148
|
+
it do
|
149
|
+
lambda {
|
150
|
+
GooglePlaces::Request.spot(
|
151
|
+
:sensor => @sensor,
|
152
|
+
:key => api_key,
|
153
|
+
:retry_options => {
|
154
|
+
:max => 3,
|
155
|
+
:status => 'INVALID_REQUEST',
|
156
|
+
:delay => 10,
|
157
|
+
:timeout => 1
|
158
|
+
}
|
159
|
+
)
|
160
|
+
}.should raise_error GooglePlaces::RetryTimeoutError
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
38
164
|
end
|
39
165
|
end
|
40
166
|
|
@@ -44,7 +44,7 @@ describe GooglePlaces::Spot do
|
|
44
44
|
|
45
45
|
it 'should have Spots with specific types' do
|
46
46
|
@collection.each do |spot|
|
47
|
-
spot.types
|
47
|
+
(spot.types & ['food', 'establishment']).should be_any
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
@@ -88,7 +88,7 @@ describe GooglePlaces::Spot do
|
|
88
88
|
|
89
89
|
it 'should have Spots with specific types' do
|
90
90
|
@collection.each do |spot|
|
91
|
-
spot.types
|
91
|
+
(spot.types & ['food', 'establishment']).should be_any
|
92
92
|
end
|
93
93
|
end
|
94
94
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_places
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Marcel de Graaf
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-12-
|
18
|
+
date: 2011-12-14 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: httparty
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- google_places.gemspec
|
92
92
|
- lib/google_places.rb
|
93
93
|
- lib/google_places/client.rb
|
94
|
+
- lib/google_places/error.rb
|
94
95
|
- lib/google_places/location.rb
|
95
96
|
- lib/google_places/request.rb
|
96
97
|
- lib/google_places/spot.rb
|