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.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- google_places (0.0.3)
4
+ google_places (0.0.5)
5
5
  httparty
6
6
 
7
7
  GEM
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "google_places"
6
- s.version = '0.0.4'
6
+ s.version = '0.0.5'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Marcel de Graaf"]
9
9
  s.email = ["mail@marceldegraaf.net"]
@@ -2,6 +2,6 @@ require 'rubygems'
2
2
  require 'httparty'
3
3
 
4
4
 
5
- %w(client location request spot).each do |file|
5
+ %w(client location request spot error).each do |file|
6
6
  require File.join(File.dirname(__FILE__), 'google_places', file)
7
7
  end
@@ -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
@@ -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
- it 'should retrieve a list of spots' do
16
- response = GooglePlaces::Request.spots(
17
- :location => @location,
18
- :radius => @radius,
19
- :sensor => @sensor,
20
- :key => api_key
21
- )
22
-
23
- response['results'].should_not be_empty
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
- it 'should retrieve a single spot' do
31
- response = GooglePlaces::Request.spot(
32
- :reference => @reference,
33
- :sensor => @sensor,
34
- :key => api_key
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
- response['result'].should_not be_empty
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.should include('establishment')
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.should include('establishment')
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: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
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-05 00:00:00 Z
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