google_maps_service_ruby 0.6.2 → 0.7.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
  SHA256:
3
- metadata.gz: 25cb368b535afc02deeda844ba39c3129173217bc66ecaaa0f62d06fa20c1914
4
- data.tar.gz: b31f5a94a276b608df9f3e24eaecccd0cf21436f3f4a8361d42058a588531318
3
+ metadata.gz: 651ee8dbc068582df401c0504894e364990b1ca0e246f72f421ca0d0a7010829
4
+ data.tar.gz: a802fec4e670dab3fcb56c5c5988d1255121a909798b204af265bc4bacfdc37e
5
5
  SHA512:
6
- metadata.gz: 175a5f27bf2c23e6d9958eda7b686a3be90a53d29e90fbf1d6fe54bc790c6266f8caa6a0c7b7319a6ea32f442b8b4a21ac66b785ec71b8f8ab32753c3db62eec
7
- data.tar.gz: dee6fc2abdd6ffa26bed8c4ef20374962b0a539f12faa4b00b33cc3843055f3cabf50ced8f13472201e936a11e9cf0f7980eedc402d78152c3cc01f6cb298eb1
6
+ metadata.gz: f1336fa544bf170a6304cdd9e96006c7bd2a6b810e4ce7000f49bd79f658895c841bf6d1f3ad996a1ac2733c9eb70e4a03a59bb3e6644f278463ec2d5bc8b732
7
+ data.tar.gz: bba938de0653a91533f991eb74b7b62ec9d957798715b5e65e87cd9a1b838d22396d0226fa3fe390bde49e8b3fe61c511de3a0525036e71b056523cde72e37e0
data/CHANGELOG.md CHANGED
@@ -2,6 +2,15 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.7.0 - 2025-02-23
6
+
7
+ * Add Support for Routes API including compute_routes and compute_route_matrix
8
+ * Explicitly require base64 gem as required by ruby 3.4
9
+
10
+ ## 0.6.3 - 2023-06-04
11
+
12
+ * Add Places API place, places, places_nearby and places_photo support
13
+
5
14
  ## 0.6.2 - 2023-03-18
6
15
 
7
16
  * Add support for ruby 3.2
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,29 @@
1
+ # Contributing
2
+ We welcome contributions to the google_maps_service_ruby gem!
3
+
4
+ ## Give us a star
5
+
6
+ You can contribute by starring the project to show your appreciation. This will help more people discover the project.
7
+
8
+ ## Reporting issues
9
+
10
+ If you find a bug or have a feature request, please [log an issue](https://github.com/langsharpe/google-maps-services-ruby/issues). The more details you can provide, the better.
11
+
12
+ ## Contributing code
13
+
14
+ The goal of this gem is to follow the API and implementation of the [Python client](https://github.com/googlemaps/google-maps-services-python) as much as possible. If you would like to add support for an API please consider porting the python implementation.
15
+
16
+ If you want to hack a quick fix or extension, please feel free to do so in a fork of the project. Please consider creating a [draft PR](https://github.blog/2019-02-14-introducing-draft-pull-requests/) so we can see what improvements the community is interested in and eventually merge them into the project.
17
+
18
+ Changes that get merged will:
19
+ * Follow the existing code style (standard.rb needs to pass)
20
+ * Add tests for any new features
21
+ * Update the documentation and readme as needed
22
+ * Add a line to the CHANGELOG.md file
23
+
24
+ To create a new pull request:
25
+ 1. Fork it (https://github.com/langsharpe/google-maps-services-ruby/fork).
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`).
28
+ 4. Push to the branch (`git push origin my-new-feature`).
29
+ 5. Create a new Pull Request. Change the base repository of the PR to langsharpe/google-maps-services-ruby.
data/README.md CHANGED
@@ -16,8 +16,10 @@ The Ruby gem for Google Maps Web Service APIs is a gem for the following Google
16
16
  - [Google Maps Distance Matrix API][Distance Matrix API]
17
17
  - [Google Maps Elevation API][Elevation API]
18
18
  - [Google Maps Geocoding API][Geocoding API]
19
+ - [Google Maps Places API][Places API]
19
20
  - [Google Maps Time Zone API][Time Zone API]
20
21
  - [Google Maps Roads API][Roads API]
22
+ - [Google Maps Routes API][Routes API]
21
23
 
22
24
  Keep in mind that the same [terms and conditions](https://developers.google.com/maps/terms) apply
23
25
  to usage of the APIs when they're accessed through this gem.
@@ -45,6 +47,10 @@ Note: Currently, [Roads API] does not accept client ID. It requires API key to a
45
47
 
46
48
  This gem return a Ruby Hash/Array object as the API result. The result format structure is same as in Google Maps API documentation.
47
49
 
50
+ ## Contributing
51
+
52
+ See [CONTRIBUTING.md](https://github.com/langsharpe/google-maps-services-ruby/blob/master/CONTRIBUTING.md) for details on contributing to this project.
53
+
48
54
  ## Requirements
49
55
 
50
56
  - Ruby 2.7 or later.
@@ -141,7 +147,7 @@ end
141
147
  gmaps = GoogleMapsService::Client.new
142
148
  ```
143
149
 
144
- For more examples and detail (setup **proxy**, **timeout**, **caching**, etc.) while initializing the client, check out [Client documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Client#initialize-instance_method).
150
+ For more examples and detail (setup **proxy**, **timeout**, **caching**, etc.) while initializing the client, check out [Client documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/Client#initialize-instance_method).
145
151
 
146
152
  ### Latitude/longitude pairs format
147
153
 
@@ -160,6 +166,27 @@ latlng = {'lat' => 40.714224, 'lng' => -73.961452}
160
166
  latlng = {'latitude' => 40.714224, 'longitude' => -73.961452}
161
167
  ```
162
168
 
169
+ ### Routes API
170
+
171
+ ```ruby
172
+ route = gmaps.compute_routes(
173
+ {address: 'South Brisbane, QLD, AU'},
174
+ {address: 'Fitzroy, VIC, AU'}
175
+ )
176
+
177
+ routes = gmaps.compute_route_matrix(
178
+ [
179
+ {waypoint: {'South Brisbane, QLD, AU'}},
180
+ ],
181
+ [
182
+ {waypoint: {address: 'Fitzroy, VIC, AU'}},
183
+ {waypoint: {address: 'Richmond, VIC, AU'}}
184
+ ],
185
+ )
186
+ ```
187
+
188
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/Routes), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/routes_spec.rb), and [Google Maps Routes API documentation][Routes API].
189
+
163
190
  ### Directions API
164
191
 
165
192
  ```ruby
@@ -237,7 +264,7 @@ Sample result:
237
264
  }]
238
265
  ```
239
266
 
240
- For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Directions), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/directions_spec.rb), and [Google Maps Directions API documentation][Directions API].
267
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/Directions), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/directions_spec.rb), and [Google Maps Directions API documentation][Directions API].
241
268
 
242
269
  ### Distance Matrix API
243
270
 
@@ -252,7 +279,7 @@ matrix = gmaps.distance_matrix(origins, destinations,
252
279
  units: 'imperial')
253
280
  ```
254
281
 
255
- For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/DistanceMatrix), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/distance_matrix_spec.rb), and [Google Maps Distance Matrix API documentation][Distance Matrix API].
282
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/DistanceMatrix), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/distance_matrix_spec.rb), and [Google Maps Distance Matrix API documentation][Distance Matrix API].
256
283
 
257
284
  ### Elevation API
258
285
 
@@ -266,7 +293,7 @@ locations = [[40.714728, -73.998672], [-34.397, 150.644]]
266
293
  results = gmaps.elevation_along_path(locations, 5)
267
294
  ```
268
295
 
269
- For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Elevation), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/elevation_spec.rb), and [Google Maps Elevation API documentation][Elevation API].
296
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/Elevation), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/elevation_spec.rb), and [Google Maps Elevation API documentation][Elevation API].
270
297
 
271
298
  ### Geocoding API
272
299
 
@@ -278,7 +305,27 @@ results = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
278
305
  results = gmaps.reverse_geocode([40.714224, -73.961452])
279
306
  ```
280
307
 
281
- For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Geocoding), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/geocoding_spec.rb), and [Google Maps Geocoding API documentation][Geocoding API].
308
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/Geocoding), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/geocoding_spec.rb), and [Google Maps Geocoding API documentation][Geocoding API].
309
+
310
+ ### Places API
311
+
312
+ ```ruby
313
+ # Search for places
314
+ results = gmaps.places(
315
+ 'Pizza Restaurant',
316
+ location: {:lat=>37.4221114, :lng=>-122.0867443},
317
+ )
318
+
319
+ # Search for places nearby
320
+ results = gmaps.places_nearby(
321
+ location: {:lat=>37.4221114, :lng=>-122.0867443},
322
+ radius: 5000,
323
+ )
324
+
325
+ # Get the url for a photo returned by the places api
326
+ url = gmaps.places_photo(photo_reference, max_width: 400, max_height: 400)
327
+ ```
328
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/Places), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/places_spec.rb), and [Google Maps Places API documentation][Places API].
282
329
 
283
330
  ### Roads API
284
331
 
@@ -309,7 +356,7 @@ place_ids = [
309
356
  results = gmaps.speed_limits(place_ids)
310
357
  ```
311
358
 
312
- For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/Roads), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/roads_spec.rb), and [Google Maps Roads API documentation][Roads API].
359
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/Roads), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/roads_spec.rb), and [Google Maps Roads API documentation][Roads API].
313
360
 
314
361
  ### Time Zone API
315
362
 
@@ -321,7 +368,7 @@ timezone = gmaps.timezone([39.603481, -119.682251])
321
368
  timezone = gmaps.timezone([39.603481, -119.682251], timestamp: Time.at(1608))
322
369
  ```
323
370
 
324
- For more usage examples and result format, check out [gem documentation](http://www.rubydoc.info/gems/google_maps_service/GoogleMapsService/Apis/TimeZone), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/time_zone_spec.rb), and [Google Maps Time Zone API documentation][Time Zone API].
371
+ For more usage examples and result format, check out [gem documentation](https://www.rubydoc.info/gems/google_maps_service_ruby/GoogleMapsService/Apis/TimeZone), [test script](https://github.com/langsharpe/google-maps-services-ruby/tree/master/spec/google_maps_service/apis/time_zone_spec.rb), and [Google Maps Time Zone API documentation][Time Zone API].
325
372
 
326
373
  ### Polyline encoder/decoder
327
374
 
@@ -346,19 +393,6 @@ encoded_path = GoogleMapsService::Polyline.encode(path)
346
393
  #=> "_p~iF~ps|U_ulLnnqC_mqNvxq`@"
347
394
  ```
348
395
 
349
- ## Issues and feature suggestions
350
-
351
- If you find a bug, or have a feature suggestion, please [log an issue][issues]. If you'd like to
352
- contribute, please read [How to Contribute](#contributing).
353
-
354
- ## Contributing
355
-
356
- 1. Fork it (https://github.com/langsharpe/google-maps-services-ruby/fork).
357
- 2. Create your feature branch (`git checkout -b my-new-feature`).
358
- 3. Commit your changes (`git commit -am 'Add some feature'`).
359
- 4. Push to the branch (`git push origin my-new-feature`).
360
- 5. Create a new Pull Request.
361
-
362
396
  [apikey]: https://developers.google.com/maps/faq#keysystem
363
397
  [clientid]: https://developers.google.com/maps/documentation/business/webservices/auth
364
398
 
@@ -367,8 +401,10 @@ contribute, please read [How to Contribute](#contributing).
367
401
  [Distance Matrix API]: https://developers.google.com/maps/documentation/distancematrix/
368
402
  [Elevation API]: https://developers.google.com/maps/documentation/elevation/
369
403
  [Geocoding API]: https://developers.google.com/maps/documentation/geocoding/
404
+ [Places API]: https://developers.google.com/maps/documentation/places/
370
405
  [Time Zone API]: https://developers.google.com/maps/documentation/timezone/
371
406
  [Roads API]: https://developers.google.com/maps/documentation/roads/
407
+ [Routes API]: https://developers.google.com/maps/documentation/routes/
372
408
 
373
409
  [Google Encoded Polyline]: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
374
410
 
@@ -64,7 +64,6 @@ module GoogleMapsService::Apis
64
64
  language: nil, units: nil, region: nil, departure_time: nil,
65
65
  arrival_time: nil, optimize_waypoints: false, transit_mode: nil,
66
66
  transit_routing_preference: nil, response_slice: :routes)
67
-
68
67
  params = {
69
68
  origin: GoogleMapsService::Convert.waypoint(origin),
70
69
  destination: GoogleMapsService::Convert.waypoint(destination)
@@ -0,0 +1,151 @@
1
+ require_relative "../validator"
2
+
3
+ module GoogleMapsService::Apis
4
+ # Performs requests to the Google Maps Places API.
5
+ module Places
6
+ # Places search.
7
+ #
8
+ # @param [String] query The text string on which to search, for example: "restaurant".
9
+ # @param [String, Hash, Array] location The latitude/longitude value for which you wish to obtain the
10
+ # closest, human-readable address.
11
+ # @param [Integer] radius Distance in meters within which to bias results.
12
+ # @param [String] language The language in which to return results.
13
+ # @param [Integer] min_price Restricts results to only those places with no less than
14
+ # this price level. Valid values are in the range from 0 (most affordable)
15
+ # to 4 (most expensive).
16
+ # @param [Integer] max_price Restricts results to only those places with no greater
17
+ # than this price level. Valid values are in the range from 0 (most
18
+ # affordable) to 4 (most expensive).
19
+ # @param [Boolean] open_now Return only those places that are open for business at
20
+ # the time the query is sent.
21
+ # @param [String] type Restricts the results to places matching the specified type.
22
+ # The full list of supported types is available here:
23
+ # https://developers.google.com/places/supported_types
24
+ # @param [String] page_token Token from a previous search that when provided will
25
+ # returns the next page of results for the same search.
26
+ # @return [Hash] Hash with the following keys:
27
+ # results: list of places
28
+ # html_attributions: set of attributions which must be displayed
29
+ # next_page_token: token for retrieving the next page of results
30
+ def places(query, location: nil, radius: nil, language: nil, min_price: nil,
31
+ max_price: nil, open_now: false, type: nil, page_token: nil)
32
+ _places("text", query: query, location: location, radius: radius,
33
+ language: language, min_price: min_price, max_price: max_price,
34
+ open_now: open_now, type: type, page_token: page_token)
35
+ end
36
+
37
+ # Performs nearby search for places.
38
+ #
39
+ # @param [String, Hash, Array] location The latitude/longitude value for
40
+ # which you wish to obtain the closest, human-readable address.
41
+ # @param [Integer] radius Distance in meters within which to bias results.
42
+ # @param [String] keyword A term to be matched against all content that
43
+ # Google has indexed for this place.
44
+ # @param [String] language The language in which to return results.
45
+ # @param [Integer] min_price Restricts results to only those places with no
46
+ # less than this price level. Valid values are in the range from 0
47
+ # (most affordable) to 4 (most expensive).
48
+ # @param [Integer] max_price Restricts results to only those places with no
49
+ # greater than this price level. Valid values are in the range
50
+ # from 0 (most affordable) to 4 (most expensive).
51
+ # @param [String, Array] name One or more terms to be matched against the
52
+ # names of places.
53
+ # @param [Boolean] open_now Return only those places that are open for
54
+ # business at the time the query is sent.
55
+ # @param [String] rank_by Specifies the order in which results are listed.
56
+ # Possible values are: prominence (default), distance
57
+ # @param [String] type Restricts the results to places matching the
58
+ # specified type. The full list of supported types is available
59
+ # here: https://developers.google.com/places/supported_types
60
+ # @param [String] page_token Token from a previous search that when provided
61
+ # will returns the next page of results for the same search.
62
+ # @return [Hash] Hash with the following keys:
63
+ # status: status code
64
+ # results: list of places
65
+ # html_attributions: set of attributions which must be displayed
66
+ # next_page_token: token for retrieving the next page of results
67
+ def places_nearby(location: nil, radius: nil, keyword: nil, language: nil,
68
+ min_price: nil, max_price: nil, name: nil, open_now: false,
69
+ rank_by: nil, type: nil, page_token: nil)
70
+ if rank_by == "distance"
71
+ if !(keyword || name || type)
72
+ raise ArgumentError, "either a keyword, name, or type arg is " \
73
+ "required when rank_by is set to distance"
74
+ elsif radius
75
+ raise ArgumentError, "radius cannot be specified when rank_by " \
76
+ "is set to distance"
77
+ end
78
+ end
79
+
80
+ _places("nearby", location: location, radius: radius,
81
+ keyword: keyword, language: language, min_price: min_price,
82
+ max_price: max_price, name: name, open_now: open_now,
83
+ rank_by: rank_by, type: type, page_token: page_token)
84
+ end
85
+
86
+ # Comprehensive details for an individual place.
87
+ #
88
+ # @param[String] place_id A textual identifier that uniquely identifies a
89
+ # place, returned from a Places search.
90
+ # @param[String] language The language in which to return results.
91
+ # @return[Hash] Hash with the following keys:
92
+ # result: dict containing place details
93
+ # html_attributions: set of attributions which must be displayed
94
+ def place(place_id, language: nil)
95
+ params = {placeid: place_id}
96
+ params[:language] = language if language
97
+ get("/maps/api/place/details/json", params)
98
+ end
99
+
100
+ # Photo URL from the Places API.
101
+ #
102
+ # @param[String] photo_reference A string identifier that uniquely
103
+ # identifies a photo, as provided by either a Places search or Places
104
+ # detail request.
105
+ # @param[Integer] max_width Specifies the maximum desired width, in pixels.
106
+ # @param[Integer] max_height Specifies the maximum desired height, in pixels.
107
+ # @return[String] String URL of the photo or nil upon error.
108
+ def places_photo(photo_reference, max_width: nil, max_height: nil)
109
+ unless max_width || max_height
110
+ raise ArgumentError, "a max_width or max_height arg is required"
111
+ end
112
+
113
+ params = {photoreference: photo_reference}
114
+
115
+ params[:maxwidth] = max_width if max_width
116
+ params[:maxheight] = max_height if max_height
117
+
118
+ image_response_decoder = ->(response) {
119
+ response["location"]
120
+ }
121
+
122
+ get("/maps/api/place/photo", params,
123
+ custom_response_decoder: image_response_decoder)
124
+ end
125
+
126
+ private
127
+
128
+ # Internal handler for ``places``, ``places_nearby``, and ``places_radar``.
129
+ # See each method's docs for arg details.
130
+ def _places(url_part,
131
+ query: nil, location: nil, radius: nil, keyword: nil, language: nil,
132
+ min_price: 0, max_price: 4, name: nil, open_now: nil,
133
+ rank_by: nil, type: nil, page_token: nil)
134
+ params = {}
135
+ params[:query] = query if query
136
+ params[:minprice] = min_price if min_price
137
+ params[:maxprice] = max_price if max_price
138
+ params[:location] = GoogleMapsService::Convert.latlng(location) if location
139
+ params[:radius] = radius if radius
140
+ params[:keyword] = keyword if keyword
141
+ params[:language] = language if language
142
+ params[:name] = GoogleMapsService::Convert.join_list(" ", name) if name
143
+ params[:opennow] = "true" if open_now
144
+ params[:rankby] = rank_by if rank_by
145
+ params[:type] = type if type
146
+ params[:pagetoken] = page_token if page_token
147
+
148
+ get("/maps/api/place/%ssearch/json" % url_part, params)
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,217 @@
1
+ module GoogleMapsService::Apis
2
+ # Performs requests to the Google Maps Routes API.
3
+ module Routes
4
+ # Base URL of Google Maps Routes API
5
+ ROUTES_BASE_URL = "https://routes.googleapis.com"
6
+
7
+ # Compute a route matrix
8
+ #
9
+ # Only origins and destinations are required. The formats for each of the paramters are defined at
10
+ # https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRouteMatrix
11
+ #
12
+ # @example Simple route matrix
13
+ # matrix = gmaps.compute_route_matrix(
14
+ # [
15
+ # {waypoint: {address: 'South Brisbane, QLD, AU'}}
16
+ # ],
17
+ # [
18
+ # {waypoint: {address: 'Fitzroy, VIC, AU'}},
19
+ # {waypoint: {address: 'Richmond, VIC, AU'}}
20
+ # ],
21
+ # )
22
+ #
23
+ # @example Complex route matrix
24
+ # matrix = gmaps.compute_route_matrix(
25
+ # [{waypoint: {address: 'South Brisbane, QLD, AU'}}],
26
+ # [{waypoint: {address: 'Fitzroy, VIC, AU'}},{waypoint: {address: 'Richmond, VIC, AU'}}],
27
+ # travel_mode: "TRANSIT",
28
+ # departure_time: Time.now,
29
+ # language_code: "en",
30
+ # region_code: "AU",
31
+ # units: "METRIC",
32
+ # extra_computations: ["TOLLS"],
33
+ # transit_preferences: {
34
+ # allowedTravelModes: [ "RAIL" ],
35
+ # routingPreference: "FEWER_TRANSFERS"
36
+ # }
37
+ # )
38
+ #
39
+ # @param [Array] origins One or more RouteMatrixOrigin
40
+ # @param [Array] destinations One or more RouteMatrixDestination
41
+ # @param [String] field_mask The fields that should be returned in the response. The default is "*"
42
+ # @param [String] travel_mode
43
+ # @param [String] routing_preference
44
+ # @param [String, Time] departure_time
45
+ # @param [String, Time] arrival_time
46
+ # @param [String] language_code
47
+ # @param [String] region_code
48
+ # @param [String] units
49
+ # @param [Array] extra_computations
50
+ # @param [String] traffic_model
51
+ # @param [Hash] transit_preferences
52
+ #
53
+ def compute_route_matrix(origins, destinations,
54
+ field_mask: nil,
55
+ travel_mode: nil,
56
+ routing_preference: nil,
57
+ departure_time: nil,
58
+ arrival_time: nil,
59
+ language_code: nil,
60
+ region_code: nil,
61
+ units: nil,
62
+ extra_computations: nil,
63
+ traffic_model: nil,
64
+ transit_preferences: nil)
65
+ params = {
66
+ origins: origins,
67
+ destinations: destinations
68
+ }
69
+
70
+ params[:travelMode] = travel_mode if travel_mode
71
+ params[:routingPreference] = routing_preference if routing_preference
72
+ params[:departureTime] = time_convert(departure_time) if departure_time
73
+ params[:arrivalTime] = time_convert(arrival_time) if arrival_time
74
+ params[:languageCode] = language_code if language_code
75
+ params[:regionCode] = region_code if region_code
76
+ params[:units] = units if units
77
+ params[:extraComputations] = extra_computations if extra_computations
78
+ params[:trafficModel] = traffic_model if traffic_model
79
+ params[:transitPreferences] = transit_preferences if transit_preferences
80
+
81
+ field_mask ||= "*"
82
+
83
+ post("/distanceMatrix/v2:computeRouteMatrix", params,
84
+ base_url: ROUTES_BASE_URL,
85
+ custom_response_decoder: method(:extract_routes_body),
86
+ field_mask: field_mask)
87
+ end
88
+
89
+ # Compute routes between locations
90
+ #
91
+ # Returns the primary route along with optional alternate routes, given a set of terminal
92
+ # and intermediate waypoints.
93
+ #
94
+ # Only the origin and destination is required. The formats for each of the paramters are defined at
95
+ # https://developers.google.com/maps/documentation/routes/reference/rest/v2/TopLevel/computeRoutes
96
+ #
97
+ # @example Simple route request
98
+ # route = gmaps.compute_routes(
99
+ # {address: 'South Brisbane, QLD, AU'},
100
+ # {address: 'Fitzroy, VIC, AU'}
101
+ # )
102
+ #
103
+ # @example Complex route request with options
104
+ # route = gmaps.compute_routes(
105
+ # {location: {latLng: {latitude: -27.4698, longitude: 153.0251}}},
106
+ # {address: 'Fitzroy, VIC, AU'},
107
+ # intermediates: [{address: 'Ultimo, NSW, AU'}],
108
+ # travel_mode: 'DRIVE',
109
+ # routing_preference: 'TRAFFIC_AWARE',
110
+ # departure_time: Time.now + 3600,
111
+ # language_code: 'en',
112
+ # region_code: 'AU',
113
+ # units: 'METRIC',
114
+ # extra_computations: ['TOLLS'],
115
+ # compute_alternative_routes: true
116
+ # )
117
+ #
118
+ # @param [Hash] origin Required. Origin waypoint
119
+ # @param [Hash] destination Required. Destination waypoint
120
+ # @param [Array] intermediates
121
+ # @param [String] travel_mode
122
+ # @param [String] routing_preference
123
+ # @param [String] polyline_quality
124
+ # @param [String] polyline_encoding
125
+ # @param [String, Time] departure_time
126
+ # @param [String, Time] arrival_time
127
+ # @param [Boolean] compute_alternative_routes
128
+ # @param [Hash] route_modifiers
129
+ # @param [String] language_code
130
+ # @param [String] region_code
131
+ # @param [String] units
132
+ # @param [Boolean] optimize_waypoint_order
133
+ # @param [Array] requested_reference_routes
134
+ # @param [Array] extra_computations
135
+ # @param [String] traffic_model
136
+ # @param [Hash] transit_preferences
137
+ # @param [String] field_mask
138
+ #
139
+ def compute_routes(origin, destination,
140
+ intermediates: nil,
141
+ travel_mode: nil,
142
+ routing_preference: nil,
143
+ polyline_quality: nil,
144
+ polyline_encoding: nil,
145
+ departure_time: nil,
146
+ arrival_time: nil,
147
+ compute_alternative_routes: nil,
148
+ route_modifiers: nil,
149
+ language_code: nil,
150
+ region_code: nil,
151
+ units: nil,
152
+ optimize_waypoint_order: nil,
153
+ requested_reference_routes: nil,
154
+ extra_computations: nil,
155
+ traffic_model: nil,
156
+ transit_preferences: nil,
157
+ field_mask: nil)
158
+ params = {
159
+ origin: origin,
160
+ destination: destination
161
+ }
162
+
163
+ params[:intermediates] = intermediates if intermediates
164
+ params[:travelMode] = travel_mode if travel_mode
165
+ params[:routingPreference] = routing_preference if routing_preference
166
+ params[:polylineQuality] = polyline_quality if polyline_quality
167
+ params[:polylineEncoding] = polyline_encoding if polyline_encoding
168
+ params[:departureTime] = time_convert(departure_time) if departure_time
169
+ params[:arrivalTime] = time_convert(arrival_time) if arrival_time
170
+ params[:computeAlternativeRoutes] = compute_alternative_routes unless compute_alternative_routes.nil?
171
+ params[:routeModifiers] = route_modifiers if route_modifiers
172
+ params[:languageCode] = language_code if language_code
173
+ params[:regionCode] = region_code if region_code
174
+ params[:units] = units if units
175
+ params[:optimizeWaypointOrder] = optimize_waypoint_order unless optimize_waypoint_order.nil?
176
+ params[:requestedReferenceRoutes] = requested_reference_routes if requested_reference_routes
177
+ params[:extraComputations] = extra_computations if extra_computations
178
+ params[:trafficModel] = traffic_model if traffic_model
179
+ params[:transitPreferences] = transit_preferences if transit_preferences
180
+
181
+ field_mask ||= "*"
182
+
183
+ post("/directions/v2:computeRoutes", params,
184
+ base_url: ROUTES_BASE_URL,
185
+ custom_response_decoder: method(:extract_routes_body),
186
+ field_mask: field_mask)
187
+ end
188
+
189
+ private
190
+
191
+ # Extracts a result from a Routes API HTTP response.
192
+ def extract_routes_body(response)
193
+ begin
194
+ body = MultiJson.load(response.body, symbolize_keys: true)
195
+ rescue
196
+ raise GoogleMapsService::Error::ApiError.new(response), "Received a malformed response."
197
+ end
198
+
199
+ if response.code == "400"
200
+ message = body.is_a?(Array) ? body.first[:error][:message] : body[:error][:message]
201
+ raise GoogleMapsService::Error::ClientError.new(response), message
202
+ else
203
+ check_response_status_code(response)
204
+ end
205
+
206
+ body
207
+ end
208
+
209
+ def time_convert(value)
210
+ if value.is_a?(Time)
211
+ value.utc.iso8601
212
+ else
213
+ value
214
+ end
215
+ end
216
+ end
217
+ end
@@ -8,7 +8,9 @@ require "google_maps_service/apis/directions"
8
8
  require "google_maps_service/apis/distance_matrix"
9
9
  require "google_maps_service/apis/elevation"
10
10
  require "google_maps_service/apis/geocoding"
11
+ require "google_maps_service/apis/places"
11
12
  require "google_maps_service/apis/roads"
13
+ require "google_maps_service/apis/routes"
12
14
  require "google_maps_service/apis/time_zone"
13
15
 
14
16
  module GoogleMapsService
@@ -25,7 +27,9 @@ module GoogleMapsService
25
27
  include GoogleMapsService::Apis::DistanceMatrix
26
28
  include GoogleMapsService::Apis::Elevation
27
29
  include GoogleMapsService::Apis::Geocoding
30
+ include GoogleMapsService::Apis::Places
28
31
  include GoogleMapsService::Apis::Roads
32
+ include GoogleMapsService::Apis::Routes
29
33
  include GoogleMapsService::Apis::TimeZone
30
34
 
31
35
  # Secret key for accessing Google Maps Web Service.
@@ -78,7 +82,7 @@ module GoogleMapsService
78
82
  def initialize(**options)
79
83
  [:key, :client_id, :client_secret,
80
84
  :retry_timeout, :queries_per_second].each do |key|
81
- instance_variable_set("@#{key}".to_sym, options[key] || GoogleMapsService.instance_variable_get("@#{key}"))
85
+ instance_variable_set(:"@#{key}", options[key] || GoogleMapsService.instance_variable_get(:"@#{key}"))
82
86
  end
83
87
  [:request_options, :ssl_options, :connection].each do |key|
84
88
  if options.has_key?(key)
@@ -150,6 +154,38 @@ module GoogleMapsService
150
154
  end
151
155
  end
152
156
 
157
+ # Make API call using an http post.
158
+ #
159
+ # @param [String] path Url path.
160
+ # @param [String] params Request parameters.
161
+ # @param [String] base_url Base Google Maps Web Service API endpoint url.
162
+ # @param [Boolean] accepts_client_id Sign the request using API {#keys} instead of {#client_id}.
163
+ # @param [Method] custom_response_decoder Custom method to decode raw API response.
164
+ #
165
+ # @return [Object] Decoded response body.
166
+ def post(path, params, base_url: DEFAULT_BASE_URL, accepts_client_id: true, custom_response_decoder: nil, field_mask: nil)
167
+ url = URI(base_url + generate_auth_url(path, {}, accepts_client_id))
168
+
169
+ Retriable.retriable timeout: @retry_timeout, on: RETRIABLE_ERRORS do |try|
170
+ begin
171
+ request_query_ticket
172
+ request = Net::HTTP::Post.new(url)
173
+ request["User-Agent"] = user_agent
174
+ request["X-Goog-FieldMask"] = field_mask if field_mask
175
+ request["Content-Type"] = "application/json"
176
+ request.body = MultiJson.dump(params)
177
+ response = Net::HTTP.start(url.hostname, url.port, use_ssl: url.scheme == "https") do |http|
178
+ http.request(request)
179
+ end
180
+ ensure
181
+ release_query_ticket
182
+ end
183
+
184
+ return custom_response_decoder.call(response) if custom_response_decoder
185
+ decode_response_body(response)
186
+ end
187
+ end
188
+
153
189
  # Get/wait the request "ticket" if QPS is configured.
154
190
  # Check for previous request time, it must be more than a second ago before calling new request.
155
191
  #
@@ -1,4 +1,4 @@
1
- require_relative "./convert"
1
+ require_relative "convert"
2
2
 
3
3
  module GoogleMapsService
4
4
  # Validate value that is accepted by Google Maps.
@@ -1,6 +1,6 @@
1
1
  module GoogleMapsService
2
2
  # GoogleMapsService gem version
3
- VERSION = "0.6.2"
3
+ VERSION = "0.7.0"
4
4
 
5
5
  # Current operating system
6
6
  # @private
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_maps_service_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lang Sharpe
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-03-18 00:00:00.000000000 Z
10
+ date: 2025-02-23 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: multi_json
@@ -38,6 +37,20 @@ dependencies:
38
37
  - - "~>"
39
38
  - !ruby/object:Gem::Version
40
39
  version: '3.1'
40
+ - !ruby/object:Gem::Dependency
41
+ name: base64
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
41
54
  - !ruby/object:Gem::Dependency
42
55
  name: coveralls_reborn
43
56
  requirement: !ruby/object:Gem::Requirement
@@ -150,9 +163,37 @@ dependencies:
150
163
  - - "~>"
151
164
  - !ruby/object:Gem::Version
152
165
  version: 0.9.28
153
- description: Google Maps API Client, including the Directions API, Distance Matrix
154
- API and Geocoding API. google_maps_service_ruby is a fork of google_maps_service,
155
- which is a fork of google-maps-services-python.
166
+ - !ruby/object:Gem::Dependency
167
+ name: irb
168
+ requirement: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - ">="
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ type: :development
174
+ prerelease: false
175
+ version_requirements: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ - !ruby/object:Gem::Dependency
181
+ name: rdoc
182
+ requirement: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ type: :development
188
+ prerelease: false
189
+ version_requirements: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ description: Google Maps API Client, including the Routes API, Directions API, Distance
195
+ Matrix API, Geocoding API and Places API. google_maps_service_ruby is a fork of
196
+ google_maps_service, which is a fork of google-maps-services-python.
156
197
  email:
157
198
  - langer8191@gmail.com
158
199
  executables: []
@@ -161,6 +202,7 @@ extra_rdoc_files: []
161
202
  files:
162
203
  - CHANGELOG.md
163
204
  - CODE_OF_CONDUCT.md
205
+ - CONTRIBUTING.md
164
206
  - LICENSE
165
207
  - README.md
166
208
  - lib/google_maps_service.rb
@@ -169,7 +211,9 @@ files:
169
211
  - lib/google_maps_service/apis/distance_matrix.rb
170
212
  - lib/google_maps_service/apis/elevation.rb
171
213
  - lib/google_maps_service/apis/geocoding.rb
214
+ - lib/google_maps_service/apis/places.rb
172
215
  - lib/google_maps_service/apis/roads.rb
216
+ - lib/google_maps_service/apis/routes.rb
173
217
  - lib/google_maps_service/apis/time_zone.rb
174
218
  - lib/google_maps_service/client.rb
175
219
  - lib/google_maps_service/convert.rb
@@ -187,8 +231,6 @@ metadata:
187
231
  changelog_uri: https://raw.githubusercontent.com/langsharpe/google-maps-services-ruby/master/CHANGELOG.md
188
232
  documentation_uri: https://www.rubydoc.info/gems/google_maps_service_ruby
189
233
  homepage_uri: https://github.com/langsharpe/google-maps-services-ruby
190
- source_code_uri: https://github.com/langsharpe/google-maps-services-ruby
191
- post_install_message:
192
234
  rdoc_options: []
193
235
  require_paths:
194
236
  - lib
@@ -203,8 +245,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
203
245
  - !ruby/object:Gem::Version
204
246
  version: '0'
205
247
  requirements: []
206
- rubygems_version: 3.4.6
207
- signing_key:
248
+ rubygems_version: 3.6.2
208
249
  specification_version: 4
209
250
  summary: Google Maps API Client
210
251
  test_files: []