google_places 0.1.1 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ spec/api_key.rb
8
8
  .yardoc/
9
9
  doc/
10
10
  spec/vcr_cassettes/*
11
+ .idea
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- google_places (0.1.0)
4
+ google_places (0.1.1)
5
5
  httparty
6
6
 
7
7
  GEM
@@ -9,20 +9,20 @@ GEM
9
9
  specs:
10
10
  diff-lcs (1.1.3)
11
11
  fakeweb (1.3.0)
12
- httparty (0.8.3)
12
+ httparty (0.9.0)
13
13
  multi_json (~> 1.0)
14
14
  multi_xml
15
15
  multi_json (1.3.6)
16
16
  multi_xml (0.5.1)
17
- rspec (2.10.0)
18
- rspec-core (~> 2.10.0)
19
- rspec-expectations (~> 2.10.0)
20
- rspec-mocks (~> 2.10.0)
21
- rspec-core (2.10.1)
22
- rspec-expectations (2.10.0)
17
+ rspec (2.11.0)
18
+ rspec-core (~> 2.11.0)
19
+ rspec-expectations (~> 2.11.0)
20
+ rspec-mocks (~> 2.11.0)
21
+ rspec-core (2.11.1)
22
+ rspec-expectations (2.11.3)
23
23
  diff-lcs (~> 1.1.3)
24
- rspec-mocks (2.10.1)
25
- vcr (2.2.2)
24
+ rspec-mocks (2.11.3)
25
+ vcr (2.2.5)
26
26
 
27
27
  PLATFORMS
28
28
  ruby
data/Rakefile CHANGED
@@ -2,6 +2,8 @@ require 'bundler'
2
2
  require 'rspec/core/rake_task'
3
3
 
4
4
  Bundler::GemHelper.install_tasks
5
- RSpec::Core::RakeTask.new('spec')
5
+ RSpec::Core::RakeTask.new('spec') do |task|
6
+ task.rspec_opts = "--color"
7
+ end
6
8
 
7
9
  task :default => :spec
@@ -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.1.1'
6
+ s.version = '0.9'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Marcel de Graaf"]
9
9
  s.email = ["mail@marceldegraaf.net"]
@@ -1,23 +1,165 @@
1
1
  module GooglePlaces
2
+ # This class acts as a proxy to the working classes when requesting data from the API.
2
3
  class Client
4
+ # @return [String] the provided api key
3
5
  attr_reader :api_key
6
+ # @return [Hash] the provided options hash
4
7
  attr_reader :options
8
+ attr_reader :sensor
5
9
 
6
- def initialize(api_key, options = {})
10
+ # Creates a new Client instance which proxies the requests to the certain classes
11
+ #
12
+ # @param [String] api_key The api key to use for the requests
13
+ # @param [Hash] options An options hash for requests. Is used as the query parameters on server requests
14
+ # @option options [String,Integer] lat
15
+ # the latitude for the search
16
+ # @option options [String,Integer] lng
17
+ # the longitude for the search
18
+ # @option options [Integer] :radius
19
+ # Defines the distance (in meters) within which to return Place results.
20
+ # The maximum allowed radius is 50,000 meters.
21
+ # Note that radius must not be included if <b>:rankby</b> is specified
22
+ # @option options [Boolean] :sensor
23
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
24
+ # @option options [String,Array] :types
25
+ # Restricts the results to Spots matching at least one of the specified types
26
+ # @option options [String] :name
27
+ # A term to be matched against the names of Places.
28
+ # Results will be restricted to those containing the passed name value.
29
+ # @option options [String] :keyword
30
+ # A term to be matched against all content that Google has indexed for this Spot,
31
+ # including but not limited to name, type, and address,
32
+ # as well as customer reviews and other third-party content.
33
+ # @option options [String] :language
34
+ # The language code, indicating in which language the results should be returned, if possible.
35
+ # @option options [String,Array<String>] :exclude
36
+ # A String or an Array of <b>types</b> to exclude from results
37
+ #
38
+ # @option options [Hash] :retry_options
39
+ # A Hash containing parameters for search retries
40
+ # @option options [Object] :retry_options[:status]
41
+ # @option options [Integer] :retry_options[:max] the maximum retries
42
+ # @option options [Integer] :retry_options[:delay] the delay between each retry in seconds
43
+ #
44
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
45
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
46
+ def initialize(api_key, sensor = false, options = {})
7
47
  @api_key = api_key
48
+ @sensor = sensor
8
49
  @options = options
9
50
  end
10
51
 
52
+ # Search for Spots at the provided location
53
+ #
54
+ # @return [Array<Spot>]
55
+ # @param [String,Integer] lat the latitude for the search
56
+ # @param [String,Integer] lng the longitude for the search
57
+ # @param [Hash] options
58
+ # @option options [Integer] :radius (1000)
59
+ # Defines the distance (in meters) within which to return Place results.
60
+ # The maximum allowed radius is 50,000 meters.
61
+ # Note that radius must not be included if :rankby => 'distance' (described below) is specified.
62
+ # <b>Note that this is a mandatory parameter</b>
63
+ # @option options [String] :rankby
64
+ # Specifies the order in which results are listed. Possible values are:
65
+ # - prominence (default). This option sorts results based on their importance.
66
+ # Ranking will favor prominent places within the specified area.
67
+ # Prominence can be affected by a Place's ranking in Google's index,
68
+ # the number of check-ins from your application, global popularity, and other factors.
69
+ # - distance. This option sorts results in ascending order by their distance from the specified location.
70
+ # Ranking results by distance will set a fixed search radius of 50km.
71
+ # One or more of keyword, name, or types is required. distance. This option sorts results in ascending order by their distance from the specified location. Ranking results by distance will set a fixed search radius of 50km. One or more of keyword, name, or types is required.
72
+ # @option options [Boolean] :sensor (false)
73
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS)
74
+ # to determine the location sent in this request.
75
+ # <b>Note that this is a mandatory parameter</b>
76
+ # @option options [String,Array] :types
77
+ # Restricts the results to Spots matching at least one of the specified types
78
+ # @option options [String] :name
79
+ # A term to be matched against the names of Places.
80
+ # Results will be restricted to those containing the passed name value.
81
+ # @option options [String] :keyword
82
+ # A term to be matched against all content that Google has indexed for this Spot,
83
+ # including but not limited to name, type, and address,
84
+ # as well as customer reviews and other third-party content.
85
+ # @option options [String] :language
86
+ # The language code, indicating in which language the results should be returned, if possible.
87
+ # @option options [String,Array<String>] :exclude ([])
88
+ # A String or an Array of <b>types</b> to exclude from results
89
+ #
90
+ # @option options [Hash] :retry_options ({})
91
+ # A Hash containing parameters for search retries
92
+ # @option options [Object] :retry_options[:status] ([])
93
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
94
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
95
+ #
96
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
97
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
11
98
  def spots(lat, lng, options = {})
12
- Spot.list(lat, lng, @api_key, @options.merge(options))
99
+ Spot.list(lat, lng, @api_key, @sensor, @options.merge(options))
13
100
  end
14
101
 
102
+ # Search for a Spot with a reference key
103
+ #
104
+ # @return [Spot]
105
+ # @param [String] reference the reference of the spot
106
+ # @param [Hash] options
107
+ # @option options [Boolean] :sensor (false)
108
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
109
+ # <b>Note that this is a mandatory parameter</b>
110
+ # @option options [String] :language
111
+ # The language code, indicating in which language the results should be returned, if possible.
112
+ #
113
+ # @option options [Hash] :retry_options ({})
114
+ # A Hash containing parameters for search retries
115
+ # @option options [Object] :retry_options[:status] ([])
116
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
117
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
15
118
  def spot(reference, options = {})
16
- Spot.find(reference, @api_key, @options.merge(options))
119
+ Spot.find(reference, @api_key, @sensor, @options.merge(options))
17
120
  end
18
121
 
122
+ # Search for Spots with a query
123
+ #
124
+ # @return [Array<Spot>]
125
+ # @param [String] query the query to search for
126
+ # @param [Hash] options
127
+ # @option options [String,Integer] lat the latitude for the search
128
+ # @option options [String,Integer] lng the longitude for the search
129
+ # @option options [Integer] :radius (1000)
130
+ # Defines the distance (in meters) within which to return Place results.
131
+ # The maximum allowed radius is 50,000 meters.
132
+ # Note that radius must not be included if :rankby => 'distance' (described below) is specified.
133
+ # <b>Note that this is a mandatory parameter</b>
134
+ # @option options [String] :rankby
135
+ # Specifies the order in which results are listed. Possible values are:
136
+ # - prominence (default). This option sorts results based on their importance.
137
+ # Ranking will favor prominent places within the specified area.
138
+ # Prominence can be affected by a Place's ranking in Google's index,
139
+ # the number of check-ins from your application, global popularity, and other factors.
140
+ # - distance. This option sorts results in ascending order by their distance from the specified location.
141
+ # Ranking results by distance will set a fixed search radius of 50km.
142
+ # One or more of keyword, name, or types is required. distance. This option sorts results in ascending order by their distance from the specified location. Ranking results by distance will set a fixed search radius of 50km. One or more of keyword, name, or types is required.
143
+ # @option options [Boolean] :sensor (false)
144
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
145
+ # <b>Note that this is a mandatory parameter</b>
146
+ # @option options [String,Array] :types
147
+ # Restricts the results to Spots matching at least one of the specified types
148
+ # @option options [String] :language
149
+ # The language code, indicating in which language the results should be returned, if possible.
150
+ # @option options [String,Array<String>] :exclude ([])
151
+ # A String or an Array of <b>types</b> to exclude from results
152
+ #
153
+ # @option options [Hash] :retry_options ({})
154
+ # A Hash containing parameters for search retries
155
+ # @option options [Object] :retry_options[:status] ([])
156
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
157
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
158
+ #
159
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
160
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
19
161
  def spots_by_query(query, options = {})
20
- Spot.list_by_query(query, @api_key, @options.merge(options))
162
+ Spot.list_by_query(query, @api_key, @sensor, @options.merge(options))
21
163
  end
22
164
  end
23
165
  end
@@ -2,9 +2,21 @@ module GooglePlaces
2
2
  class OverQueryLimitError < HTTParty::ResponseError
3
3
  end
4
4
 
5
+ # Thrown when a request was denied by the server
6
+ #
7
+ # This can be the case when:
8
+ # - querying the SPOT_LIST_URL <b>without</b> the following parameters:
9
+ # - - key
10
+ # - - sensor
5
11
  class RequestDeniedError < HTTParty::ResponseError
6
12
  end
7
13
 
14
+ # Thrown when a request was rejected as invalid by the server
15
+ #
16
+ # This can be the case when:
17
+ # - querying the SPOT_LIST_URL <b>without</b> the following parameters:
18
+ # - - location
19
+ # - - radius
8
20
  class InvalidRequestError < HTTParty::ResponseError
9
21
  end
10
22
 
@@ -1,8 +1,8 @@
1
1
  module GooglePlaces
2
2
  class Location
3
3
  def initialize(lat, lng)
4
- @lat = lat
5
- @lng = lng
4
+ @lat = ("%.8f"%lat)
5
+ @lng = ("%.8f"%lng)
6
6
  end
7
7
 
8
8
  def format
@@ -1,29 +1,167 @@
1
1
  module GooglePlaces
2
+ # This class performs the queries on the API
2
3
  class Request
4
+ # @return [HTTParty::Response] the retrieved response from the API
3
5
  attr_accessor :response
6
+ attr_reader :options
4
7
 
5
8
  include ::HTTParty
6
9
  format :json
7
10
 
8
- SPOTS_LIST_URL = 'https://maps.googleapis.com/maps/api/place/search/json'
9
- SPOT_URL = 'https://maps.googleapis.com/maps/api/place/details/json'
10
- SPOTS_LIST_QUERY_URL = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
11
-
11
+ NEARBY_SEARCH_URL = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json'
12
+ DETAILS_URL = 'https://maps.googleapis.com/maps/api/place/details/json'
13
+ TEXT_SEARCH_URL = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
14
+
15
+ # Search for Spots at the provided location
16
+ #
17
+ # @return [Array<Spot>]
18
+ # @param [Hash] options
19
+ # @option options [String] :key
20
+ # the provided api key.
21
+ # <b>Note that this is a mandatory parameter</b>
22
+ # @option options [Integer] :radius
23
+ # Defines the distance (in meters) within which to return Place results.
24
+ # The maximum allowed radius is 50,000 meters.
25
+ # Note that radius must not be included if <b>:rankby</b> is specified
26
+ # <b>Note that this is a mandatory parameter</b>
27
+ # @option options [Boolean] :sensor
28
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
29
+ # <b>Note that this is a mandatory parameter</b>
30
+ # @option options [(Integer,Integer),String] :location
31
+ # The latitude/longitude around which to retrieve Spot information. This must be specified as latitude,longitude
32
+ # <b>Note that this is a mandatory parameter</b>
33
+ # @option options [String,Array] :types
34
+ # Restricts the results to Spots matching at least one of the specified types
35
+ # @option options [String] :name
36
+ # A term to be matched against the names of Places.
37
+ # Results will be restricted to those containing the passed name value.
38
+ # @option options [String] :keyword
39
+ # A term to be matched against all content that Google has indexed for this Spot,
40
+ # including but not limited to name, type, and address,
41
+ # as well as customer reviews and other third-party content.
42
+ # @option options [String] :language
43
+ # The language code, indicating in which language the results should be returned, if possible.
44
+ # @option options [String,Array<String>] :exclude ([])
45
+ # A String or an Array of <b>types</b> to exclude from results
46
+ #
47
+ # @option options [Hash] :retry_options ({})
48
+ # A Hash containing parameters for search retries
49
+ # @option options [Object] :retry_options[:status] ([])
50
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
51
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
52
+ #
53
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
54
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
12
55
  def self.spots(options = {})
13
- request = new(SPOTS_LIST_URL, options)
56
+ request = new(NEARBY_SEARCH_URL, options)
14
57
  request.parsed_response
15
58
  end
16
59
 
60
+ # Search for a Spot with a reference key
61
+ #
62
+ # @return [Spot]
63
+ # @param [Hash] options
64
+ # @option options [String] :key
65
+ # the provided api key.
66
+ # <b>Note that this is a mandatory parameter</b>
67
+ # @option options [String] :reference
68
+ # The reference of a already retrieved Spot
69
+ # <b>Note that this is a mandatory parameter</b>
70
+ # @option options [Boolean] :sensor
71
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
72
+ # <b>Note that this is a mandatory parameter</b>
73
+ # @option options [String] :language
74
+ # The language code, indicating in which language the results should be returned, if possible.
75
+ #
76
+ # @option options [Hash] :retry_options ({})
77
+ # A Hash containing parameters for search retries
78
+ # @option options [Object] :retry_options[:status] ([])
79
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
80
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
17
81
  def self.spot(options = {})
18
- request = new(SPOT_URL, options)
82
+ request = new(DETAILS_URL, options)
19
83
  request.parsed_response
20
84
  end
21
85
 
86
+ # Search for Spots with a query
87
+ #
88
+ # @return [Array<Spot>]
89
+ # @param [String] query the query to search for
90
+ # @param [String] api_key the provided api key
91
+ # @param [Hash] options
92
+ # @option options [String,Integer] :lat
93
+ # the latitude for the search
94
+ # @option options [String,Integer] :lng
95
+ # the longitude for the search
96
+ # @option options [Integer] :radius
97
+ # Defines the distance (in meters) within which to return Place results.
98
+ # The maximum allowed radius is 50,000 meters.
99
+ # Note that radius must not be included if <b>:rankby</b> is specified
100
+ # <b>Note that this is a mandatory parameter</b>
101
+ # @option options [Boolean] :sensor
102
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
103
+ # <b>Note that this is a mandatory parameter</b>
104
+ # @option options [String,Array] :types
105
+ # Restricts the results to Spots matching at least one of the specified types
106
+ # @option options [String] :language
107
+ # The language code, indicating in which language the results should be returned, if possible.
108
+ # @option options [String,Array<String>] :exclude ([])
109
+ # A String or an Array of <b>types</b> to exclude from results
110
+ #
111
+ # @option options [Hash] :retry_options ({})
112
+ # A Hash containing parameters for search retries
113
+ # @option options [Object] :retry_options[:status] ([])
114
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
115
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
116
+ #
117
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
118
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
22
119
  def self.spots_by_query(options = {})
23
- request = new(SPOTS_LIST_QUERY_URL, options)
120
+ request = new(TEXT_SEARCH_URL, options)
24
121
  request.parsed_response
25
122
  end
26
123
 
124
+ # Create a new Request for a given uri and the provided params
125
+ #
126
+ # @return [Request]
127
+ # @param [String] url The uri to make the query on
128
+ # @param [Hash] options A Hash providing options for the request
129
+ # @option options [String] :key
130
+ # the provided api key.
131
+ # <b>Note that this is a mandatory parameter</b>
132
+ # @option options [Integer] :radius
133
+ # Defines the distance (in meters) within which to return Place results.
134
+ # The maximum allowed radius is 50,000 meters.
135
+ # Note that radius must not be included if <b>:rankby</b> is specified
136
+ # <b>Note that this is a mandatory parameter</b>
137
+ # @option options [Boolean] :sensor
138
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
139
+ # <b>Note that this is a mandatory parameter</b>
140
+ # @option options [(Integer,Integer),String] :location
141
+ # The latitude/longitude around which to retrieve Spot information. This must be specified as latitude,longitude
142
+ # <b>Note that this is a mandatory parameter</b>
143
+ # @option options [String,Array] :types
144
+ # Restricts the results to Spots matching at least one of the specified types
145
+ # @option options [String] :name
146
+ # A term to be matched against the names of Places.
147
+ # Results will be restricted to those containing the passed name value.
148
+ # @option options [String] :keyword
149
+ # A term to be matched against all content that Google has indexed for this Spot,
150
+ # including but not limited to name, type, and address,
151
+ # as well as customer reviews and other third-party content.
152
+ # @option options [String] :language
153
+ # The language code, indicating in which language the results should be returned, if possible.
154
+ # @option options [String,Array<String>] :exclude ([])
155
+ # A String or an Array of <b>types</b> to exclude from results
156
+ #
157
+ # @option options [Hash] :retry_options ({})
158
+ # A Hash containing parameters for search retries
159
+ # @option options [Object] :retry_options[:status] ([])
160
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
161
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
162
+ #
163
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
164
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
27
165
  def initialize(url, options)
28
166
  retry_options = options.delete(:retry_options) || {}
29
167
 
@@ -62,6 +200,16 @@ module GooglePlaces
62
200
  end
63
201
  end
64
202
 
203
+ def execute
204
+ @response = self.class.get(url, :query => options)
205
+ end
206
+
207
+ # Parse errors from the server respons, if any
208
+ # @raise [OverQueryLimitError] when server response object includes status 'OVER_QUERY_LIMIT'
209
+ # @raise [RequestDeniedError] when server response object includes 'REQUEST_DENIED'
210
+ # @raise [InvalidRequestError] when server response object includes 'INVALID_REQUEST'
211
+ # @raise [UnknownError] when server response object includes 'UNKNOWN_ERROR'
212
+ # @return [String] the response from the server as JSON
65
213
  def parsed_response
66
214
  case @response.parsed_response['status']
67
215
  when 'OK', 'ZERO_RESULTS'
@@ -4,14 +4,60 @@ module GooglePlaces
4
4
  class Spot
5
5
  attr_accessor :lat, :lng, :name, :icon, :reference, :vicinity, :types, :id, :formatted_phone_number, :international_phone_number, :formatted_address, :address_components, :street_number, :street, :city, :region, :postal_code, :country, :rating, :url, :cid, :website, :reviews
6
6
 
7
- def self.list(lat, lng, api_key, options = {})
8
- radius = options.delete(:radius) || 200
9
- sensor = options.delete(:sensor) || false
7
+ # Search for Spots at the provided location
8
+ #
9
+ # @return [Array<Spot>]
10
+ # @param [String,Integer] lat the latitude for the search
11
+ # @param [String,Integer] lng the longitude for the search
12
+ # @param [String] api_key the provided api key
13
+ # @param [Boolean] sensor
14
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request.
15
+ # <b>Note that this is a mandatory parameter</b>
16
+ # @param [Hash] options
17
+ # @option options [Integer] :radius (1000)
18
+ # Defines the distance (in meters) within which to return Place results.
19
+ # The maximum allowed radius is 50,000 meters.
20
+ # Note that radius must not be included if :rankby => 'distance' (described below) is specified.
21
+ # <b>Note that this is a mandatory parameter</b>
22
+ # @option options [String] :rankby
23
+ # Specifies the order in which results are listed. Possible values are:
24
+ # - prominence (default). This option sorts results based on their importance.
25
+ # Ranking will favor prominent places within the specified area.
26
+ # Prominence can be affected by a Place's ranking in Google's index,
27
+ # the number of check-ins from your application, global popularity, and other factors.
28
+ # - distance. This option sorts results in ascending order by their distance from the specified location.
29
+ # Ranking results by distance will set a fixed search radius of 50km.
30
+ # One or more of keyword, name, or types is required. distance. This option sorts results in ascending order by their distance from the specified location. Ranking results by distance will set a fixed search radius of 50km. One or more of keyword, name, or types is required.
31
+ # @option options [String,Array] :types
32
+ # Restricts the results to Spots matching at least one of the specified types
33
+ # @option options [String] :name
34
+ # A term to be matched against the names of Places.
35
+ # Results will be restricted to those containing the passed name value.
36
+ # @option options [String] :keyword
37
+ # A term to be matched against all content that Google has indexed for this Spot,
38
+ # including but not limited to name, type, and address,
39
+ # as well as customer reviews and other third-party content.
40
+ # @option options [String] :language
41
+ # The language code, indicating in which language the results should be returned, if possible.
42
+ # @option options [String,Array<String>] :exclude ([])
43
+ # A String or an Array of <b>types</b> to exclude from results
44
+ #
45
+ # @option options [Hash] :retry_options ({})
46
+ # A Hash containing parameters for search retries
47
+ # @option options [Object] :retry_options[:status] ([])
48
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
49
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
50
+ #
51
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
52
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
53
+ def self.list(lat, lng, api_key, sensor, options = {})
54
+ location = Location.new(lat, lng)
55
+ rankby = options.delete(:rankby)
56
+ radius = options.delete(:radius) || 1000 if rankby.nil?
10
57
  types = options.delete(:types)
11
58
  name = options.delete(:name)
12
59
  keyword = options.delete(:keyword)
13
60
  language = options.delete(:language)
14
- location = Location.new(lat, lng)
15
61
  exclude = options.delete(:exclude) || []
16
62
  retry_options = options.delete(:retry_options) || {}
17
63
 
@@ -21,6 +67,7 @@ module GooglePlaces
21
67
  :location => location.format,
22
68
  :radius => radius,
23
69
  :sensor => sensor,
70
+ :rankby => rankby,
24
71
  :key => api_key,
25
72
  :name => name,
26
73
  :language => language,
@@ -34,14 +81,32 @@ module GooglePlaces
34
81
  options.merge!(:types => types)
35
82
  end
36
83
 
37
- response = Request.spots(options)
38
- response['results'].map do |result|
39
- self.new(result) if (result['types'] & exclude) == []
40
- end.compact
84
+ results = []
85
+ self.multi_pages_request(:spots, options) do |result|
86
+ results << self.new(result) if (result['types'] & exclude) == []
87
+ end
88
+ results
41
89
  end
42
90
 
43
- def self.find(reference, api_key, options = {})
44
- sensor = options.delete(:sensor) || false
91
+ # Search for a Spot with a reference key
92
+ #
93
+ # @return [Spot]
94
+ # @param [String] reference the reference of the spot
95
+ # @param [String] api_key the provided api key
96
+ # @param [Boolean] sensor
97
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS)
98
+ # to determine the location sent in this request.
99
+ # <b>Note that this is a mandatory parameter</b>
100
+ # @param [Hash] options
101
+ # @option options [String] :language
102
+ # The language code, indicating in which language the results should be returned, if possible.
103
+ #
104
+ # @option options [Hash] :retry_options ({})
105
+ # A Hash containing parameters for search retries
106
+ # @option options [Object] :retry_options[:status] ([])
107
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
108
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
109
+ def self.find(reference, api_key, sensor, options = {})
45
110
  language = options.delete(:language)
46
111
  retry_options = options.delete(:retry_options) || {}
47
112
 
@@ -56,7 +121,50 @@ module GooglePlaces
56
121
  self.new(response['result'])
57
122
  end
58
123
 
59
- def self.list_by_query(query, api_key, options)
124
+ # Search for Spots with a query
125
+ #
126
+ # @return [Array<Spot>]
127
+ # @param [String] query the query to search for
128
+ # @param [String] api_key the provided api key
129
+ # @param [Boolean] sensor
130
+ # Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS)
131
+ # to determine the location sent in this request.
132
+ # <b>Note that this is a mandatory parameter</b>
133
+ # @param [Hash] options
134
+ # @option options [String,Integer] :lat
135
+ # the latitude for the search
136
+ # @option options [String,Integer] :lng
137
+ # the longitude for the search
138
+ # @option options [Integer] :radius (1000)
139
+ # Defines the distance (in meters) within which to return Place results.
140
+ # The maximum allowed radius is 50,000 meters.
141
+ # Note that radius must not be included if :rankby => 'distance' (described below) is specified.
142
+ # <b>Note that this is a mandatory parameter</b>
143
+ # @option options [String] :rankby
144
+ # Specifies the order in which results are listed. Possible values are:
145
+ # - prominence (default). This option sorts results based on their importance.
146
+ # Ranking will favor prominent places within the specified area.
147
+ # Prominence can be affected by a Place's ranking in Google's index,
148
+ # the number of check-ins from your application, global popularity, and other factors.
149
+ # - distance. This option sorts results in ascending order by their distance from the specified location.
150
+ # Ranking results by distance will set a fixed search radius of 50km.
151
+ # One or more of keyword, name, or types is required.
152
+ # @option options [String,Array] :types
153
+ # Restricts the results to Spots matching at least one of the specified types
154
+ # @option options [String] :language
155
+ # The language code, indicating in which language the results should be returned, if possible.
156
+ # @option options [String,Array<String>] :exclude ([])
157
+ # A String or an Array of <b>types</b> to exclude from results
158
+ #
159
+ # @option options [Hash] :retry_options ({})
160
+ # A Hash containing parameters for search retries
161
+ # @option options [Object] :retry_options[:status] ([])
162
+ # @option options [Integer] :retry_options[:max] (0) the maximum retries
163
+ # @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
164
+ #
165
+ # @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
166
+ # @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
167
+ def self.list_by_query(query, api_key, sensor, options = {})
60
168
  if options.has_key?(:lat) && options.has_key?(:lng)
61
169
  with_location = true
62
170
  else
@@ -70,9 +178,10 @@ module GooglePlaces
70
178
  end
71
179
 
72
180
  query = query
73
- sensor = options.delete(:sensor) || false
181
+ sensor = sensor
74
182
  location = Location.new(options.delete(:lat), options.delete(:lng)) if with_location
75
183
  radius = options.delete(:radius) if with_radius
184
+ rankby = options.delete(:rankby)
76
185
  language = options.delete(:language)
77
186
  types = options.delete(:types)
78
187
  exclude = options.delete(:exclude) || []
@@ -84,6 +193,7 @@ module GooglePlaces
84
193
  :query => query,
85
194
  :sensor => sensor,
86
195
  :key => api_key,
196
+ :rankby => rankby,
87
197
  :language => language,
88
198
  :retry_options => retry_options
89
199
  }
@@ -97,12 +207,38 @@ module GooglePlaces
97
207
  options.merge!(:types => types)
98
208
  end
99
209
 
100
- response = Request.spots_by_query(options)
101
- response['results'].map do |result|
102
- self.new(result) if (result['types'] & exclude) == []
103
- end.compact
210
+ results = []
211
+ self.multi_pages_request(:spots_by_query, options) do |result|
212
+ results << self.new(result) if (result['types'] & exclude) == []
213
+ end
214
+ results
215
+ end
216
+
217
+ def self.multi_pages_request(method, options)
218
+ begin
219
+
220
+ response = Request.send(method, options)
221
+ response['results'].each do |result|
222
+ yield(result)
223
+ end
224
+
225
+ # request the next page if presence of a "next_page" token
226
+ next_page = false
227
+ unless response["next_page_token"].nil?
228
+ options = {
229
+ pagetoken: response["next_page_token"],
230
+ key: options[:key],
231
+ sensor: options[:sensor]
232
+ }
233
+ sleep(2) # the time the token is issued, else InvalidRequestError
234
+ next_page = true
235
+ end
236
+
237
+ end while (next_page)
104
238
  end
105
239
 
240
+ # @param [JSON] json_result_object a JSON object to create a Spot from
241
+ # @return [Spot] a newly created spot
106
242
  def initialize(json_result_object)
107
243
  @reference = json_result_object['reference']
108
244
  @vicinity = json_result_object['vicinity']
@@ -14,7 +14,7 @@ describe GooglePlaces::Client do
14
14
  it 'should request spots' do
15
15
  lat, lng = '-33.8670522', '151.1957362'
16
16
  @client = GooglePlaces::Client.new(api_key)
17
- GooglePlaces::Spot.should_receive(:list).with(lat, lng, api_key, {})
17
+ GooglePlaces::Spot.should_receive(:list).with(lat, lng, api_key, false, {})
18
18
 
19
19
  @client.spots(lat, lng)
20
20
  end
@@ -22,7 +22,7 @@ describe GooglePlaces::Client do
22
22
  it 'should request a single spot' do
23
23
  reference = "CoQBeAAAAO-prCRp9Atcj_rvavsLyv-DnxbGkw8QyRZb6Srm6QHOcww6lqFhIs2c7Ie6fMg3PZ4PhicfJL7ZWlaHaLDTqmRisoTQQUn61WTcSXAAiCOzcm0JDBnafqrskSpFtNUgzGAOx29WGnWSP44jmjtioIsJN9ik8yjK7UxP4buAmMPVEhBXPiCfHXk1CQ6XRuQhpztsGhQU4U6-tWjTHcLSVzjbNxoiuihbaA"
24
24
  @client = GooglePlaces::Client.new(api_key)
25
- GooglePlaces::Spot.should_receive(:find).with(reference, api_key, {})
25
+ GooglePlaces::Spot.should_receive(:find).with(reference, api_key, false, {})
26
26
 
27
27
  @client.spot(reference)
28
28
  end
@@ -30,7 +30,7 @@ describe GooglePlaces::Client do
30
30
  it 'should request spots by query' do
31
31
  query = "Statue of liberty, New York"
32
32
  @client = GooglePlaces::Client.new(api_key)
33
- GooglePlaces::Spot.should_receive(:list_by_query).with(query, api_key, {})
33
+ GooglePlaces::Spot.should_receive(:list_by_query).with(query, api_key, false, {})
34
34
 
35
35
  @client.spots_by_query(query)
36
36
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe GooglePlaces::Location do
4
4
 
5
5
  it 'should return a location based on a lat and lng' do
6
- GooglePlaces::Location.new('123', '456').format.should == '123,456'
6
+ GooglePlaces::Location.new('123', '456').format.should == '123.00000000,456.00000000'
7
7
  end
8
8
 
9
9
  end
@@ -18,14 +18,14 @@ describe GooglePlaces::Spot do
18
18
  end
19
19
 
20
20
  it 'should be a collection of Spots' do
21
- @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :sensor => @sensor)
21
+ @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, @sensor, :radius => @radius)
22
22
  end
23
23
 
24
24
  describe 'with a single type' do
25
25
  use_vcr_cassette 'list_spots_with_single_type'
26
26
 
27
27
  before(:each) do
28
- @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :sensor => @sensor, :types => 'cafe')
28
+ @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, @sensor, :radius => @radius, :types => 'cafe')
29
29
  end
30
30
 
31
31
  it 'should have Spots with a specific type' do
@@ -39,7 +39,7 @@ describe GooglePlaces::Spot do
39
39
  use_vcr_cassette 'list_spots_with_multiple_types'
40
40
 
41
41
  before(:each) do
42
- @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :sensor => @sensor, :types => ['food','establishment'])
42
+ @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, @sensor, :radius => @radius, :types => ['food','establishment'])
43
43
  end
44
44
 
45
45
  it 'should have Spots with specific types' do
@@ -53,7 +53,7 @@ describe GooglePlaces::Spot do
53
53
  use_vcr_cassette 'list_spots_with_name'
54
54
 
55
55
  before(:each) do
56
- @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :sensor => @sensor, :name => 'italian')
56
+ @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, @sensor, :radius => @radius, :name => 'italian')
57
57
  end
58
58
 
59
59
  # Apparently the Google Places API returns spots with
@@ -72,7 +72,7 @@ describe GooglePlaces::Spot do
72
72
  use_vcr_cassette 'list_spots_with_name_and_types'
73
73
 
74
74
  before(:each) do
75
- @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :sensor => @sensor, :types => ['food','establishment'], :name => 'italian')
75
+ @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, @sensor, :radius => @radius, :types => ['food','establishment'], :name => 'italian')
76
76
  end
77
77
 
78
78
  # Apparently the Google Places API returns spots with
@@ -97,7 +97,7 @@ describe GooglePlaces::Spot do
97
97
  use_vcr_cassette 'list_spots_with_types_and_exclusion'
98
98
 
99
99
  it 'should exclude spots with type "restaurant"' do
100
- @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :sensor => @sensor, :types => ['food','establishment'], :exclude => 'restaurant')
100
+ @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, @sensor, :radius => @radius, :types => ['food','establishment'], :exclude => 'restaurant')
101
101
 
102
102
  @collection.map(&:types).each do |types|
103
103
  types.should_not include('restaurant')
@@ -105,7 +105,7 @@ describe GooglePlaces::Spot do
105
105
  end
106
106
 
107
107
  it 'should exclude spots with type "restaurant" and "cafe"' do
108
- @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :sensor => @sensor, :types => ['food','establishment'], :exclude => ['restaurant', 'cafe'])
108
+ @collection = GooglePlaces::Spot.list(@lat, @lng, api_key, @sensor, :radius => @radius, :types => ['food','establishment'], :exclude => ['restaurant', 'cafe'])
109
109
 
110
110
  @collection.map(&:types).each do |types|
111
111
  types.should_not include('restaurant')
@@ -124,7 +124,7 @@ describe GooglePlaces::Spot do
124
124
  end
125
125
 
126
126
  it 'should be a collection of Spots' do
127
- @collection = GooglePlaces::Spot.list_by_query("Statue of liberty, New York", api_key, :sensor => @sensor)
127
+ @collection = GooglePlaces::Spot.list_by_query("Statue of liberty, New York", api_key, @sensor)
128
128
  end
129
129
 
130
130
  end
@@ -132,7 +132,7 @@ describe GooglePlaces::Spot do
132
132
  context 'Find a single spot' do
133
133
  use_vcr_cassette 'single_spot'
134
134
  before :each do
135
- @spot = GooglePlaces::Spot.find(@reference, api_key, :sensor => @sensor)
135
+ @spot = GooglePlaces::Spot.find(@reference, api_key, @sensor)
136
136
  end
137
137
  it 'should be a Spot' do
138
138
  @spot.class.should == GooglePlaces::Spot
data/spec/vcr_setup.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'vcr'
2
2
 
3
- VCR.config do |c|
3
+ VCR.configure do |c|
4
4
  c.cassette_library_dir = 'spec/vcr_cassettes'
5
- c.stub_with :fakeweb
5
+ c.hook_into :fakeweb
6
6
  c.ignore_localhost = true
7
7
  c.default_cassette_options = { :record => :new_episodes }
8
8
  end
metadata CHANGED
@@ -1,88 +1,88 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: google_places
3
- version: !ruby/object:Gem::Version
4
- hash: 25
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.9'
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Marcel de Graaf
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-07-14 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: httparty
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rspec
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
38
33
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
46
38
  type: :development
47
- version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: fakeweb
50
39
  prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: fakeweb
48
+ requirement: !ruby/object:Gem::Requirement
52
49
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 3
57
- segments:
58
- - 0
59
- version: "0"
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
60
54
  type: :development
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
63
- name: vcr
64
55
  prerelease: false
65
- requirement: &id004 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: vcr
64
+ requirement: !ruby/object:Gem::Requirement
66
65
  none: false
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- hash: 3
71
- segments:
72
- - 0
73
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
74
70
  type: :development
75
- version_requirements: *id004
76
- description: This gem provides a Ruby wrapper around the Google Places API for use in your own project. Please note that this gem does not provide OAuth authentication.
77
- email:
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: This gem provides a Ruby wrapper around the Google Places API for use
79
+ in your own project. Please note that this gem does not provide OAuth authentication.
80
+ email:
78
81
  - mail@marceldegraaf.net
79
82
  executables: []
80
-
81
83
  extensions: []
82
-
83
84
  extra_rdoc_files: []
84
-
85
- files:
85
+ files:
86
86
  - .gitignore
87
87
  - Gemfile
88
88
  - Gemfile.lock
@@ -105,38 +105,29 @@ files:
105
105
  - spec/vcr_setup.rb
106
106
  homepage: https://github.com/marceldegraaf/google_places
107
107
  licenses: []
108
-
109
108
  post_install_message:
110
109
  rdoc_options: []
111
-
112
- require_paths:
110
+ require_paths:
113
111
  - lib
114
- required_ruby_version: !ruby/object:Gem::Requirement
112
+ required_ruby_version: !ruby/object:Gem::Requirement
115
113
  none: false
116
- requirements:
117
- - - ">="
118
- - !ruby/object:Gem::Version
119
- hash: 3
120
- segments:
121
- - 0
122
- version: "0"
123
- required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
119
  none: false
125
- requirements:
126
- - - ">="
127
- - !ruby/object:Gem::Version
128
- hash: 3
129
- segments:
130
- - 0
131
- version: "0"
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
132
124
  requirements: []
133
-
134
125
  rubyforge_project:
135
126
  rubygems_version: 1.8.23
136
127
  signing_key:
137
128
  specification_version: 3
138
129
  summary: A Ruby wrapper around the Google Places API.
139
- test_files:
130
+ test_files:
140
131
  - spec/api_key.sample.rb
141
132
  - spec/google_places/client_spec.rb
142
133
  - spec/google_places/location_spec.rb
@@ -144,3 +135,4 @@ test_files:
144
135
  - spec/google_places/spot_spec.rb
145
136
  - spec/spec_helper.rb
146
137
  - spec/vcr_setup.rb
138
+ has_rdoc: