google-maps 2.2.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 102b89cceb3e12e754cf495744ec8044dadb0a06
4
- data.tar.gz: ea3661301148cd5c9d140a5d5fa5d092d5d25717
3
+ metadata.gz: 8c9ea7de658231206acfde4f02867362e4b43953
4
+ data.tar.gz: e09e76851c8d3d2f963ade490ebcbff26b44703e
5
5
  SHA512:
6
- metadata.gz: 713d16adce4b90aeb0e88e5090a5ed116c179057511d590d69204f6a8542b0943c74a5ae1e3253f152dae4ade162087415accbc5bce29918a715f29163179252
7
- data.tar.gz: e490d7924df3db9ceb07163c290549f5fc89d9111201c9f40cffca7aac7a15326eb197cb50d767300e61b2a1dae3388befd08b852e1087052447426b4be479ab
6
+ metadata.gz: 603662cd8f896c0145d0d390feac907b909be471848cfb72674717fad7d0d39f22bc57005cb63a19106e14ec3c1f9bf29824c54381f6a269a119179d9f4231e6
7
+ data.tar.gz: 8fdf84b4466a49a63ce26ef28f7a77ae79ad291843942534337d869269820ee6f7d29c8e367bf0bcd6f9a7d4d42604c72a3db2d784e32c6ae0080f108bfcf189
@@ -0,0 +1,20 @@
1
+ AllCops:
2
+ Exclude:
3
+ - ./google-maps.gemspec
4
+
5
+ Metrics/BlockLength:
6
+ Exclude:
7
+ - "spec/**/*.rb"
8
+ Max: 80
9
+
10
+ Metrics/LineLength:
11
+ Max: 130
12
+
13
+ Metrics/MethodLength:
14
+ Max: 15
15
+
16
+ Metrics/AbcSize:
17
+ Max: 20
18
+
19
+ Style/Documentation:
20
+ Enabled: false
@@ -1,4 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.5
4
3
  - 2.2.2
4
+ - 2.5.3
5
+ before_install:
6
+ - gem install bundler
7
+ script:
8
+ - bundle exec rspec
9
+ - bundle exec rubocop
data/Gemfile CHANGED
@@ -1,4 +1,6 @@
1
- source "http://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'http://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in google-maps.gemspec
4
6
  gemspec
File without changes
@@ -0,0 +1,216 @@
1
+ [![Gem Version](https://badge.fury.io/rb/google-maps.svg)](http://badge.fury.io/rb/google-maps)
2
+ [![Build Status](https://travis-ci.org/zilverline/google-maps.svg?branch=master)](https://travis-ci.org/zilverline/google-maps)
3
+ [![Coverage Status](https://coveralls.io/repos/zilverline/google-maps/badge.svg?branch=master)](https://coveralls.io/r/zilverline/google-maps?branch=master)
4
+ [![Code Climate](https://codeclimate.com/repos/55671579695680044d01e0ac/badges/8f4d88f30585847e4fcf/gpa.svg)](https://codeclimate.com/repos/55671579695680044d01e0ac/feed)
5
+
6
+ # Google Maps
7
+
8
+ ## Installation
9
+
10
+ `gem install google-maps`
11
+
12
+ Or add the following line to your Gemfile:
13
+
14
+ `gem 'google-maps'`
15
+
16
+ ## Configuration
17
+
18
+ You can either authenticate with an API key or a digital signature.
19
+
20
+ API key:
21
+
22
+ ```ruby
23
+ Google::Maps.configure do |config|
24
+ config.authentication_mode = Google::Maps::Authentication::API_KEY
25
+ config.api_key = 'xxxxxxxxxxx'
26
+ end
27
+ ```
28
+
29
+ Digital signature:
30
+
31
+ ```ruby
32
+ Google::Maps.configure do |config|
33
+ config.authentication_mode = Google::Maps::Authentication::DIGITAL_SIGNATURE
34
+ config.client_id = 'xxxxxxxxxxx'
35
+ config.client_secret = 'xxxxxxxxxxx'
36
+ end
37
+ ```
38
+
39
+ ### Default language
40
+
41
+ Set the default language.
42
+
43
+ ```ruby
44
+ Google::Maps.configure do |config|
45
+ config.default_language = :nl #dutch
46
+ end
47
+ ```
48
+
49
+ ## Usage Examples
50
+
51
+ ### Distance
52
+
53
+ ```ruby
54
+ Google::Maps.distance("Science Park, Amsterdam", "Deventer")
55
+ #=> "105 km"
56
+ Google::Maps.distance("52.3545543,4.9519029", "Deventer")
57
+ #=> "105 km"
58
+ ```
59
+
60
+ ### Duration
61
+
62
+ ```ruby
63
+ Google::Maps.duration("Science Park, Amsterdam", "Deventer")
64
+ #=> "1 hour 12 mins"
65
+ Google::Maps.duration("52.3545543,4.9519029", "Deventer")
66
+ #=> "1 hour 12 mins"
67
+ ```
68
+
69
+ ### Route
70
+
71
+ ```ruby
72
+ route = Google::Maps.route("Science Park, Amsterdam", "Deventer")
73
+ route = Google::Maps.route("52.3545543,4.9519029", "Deventer")
74
+ route.distance.text
75
+ #=> "104 km"
76
+ route.duration.text
77
+ #=> "1 hour 12 mins"
78
+ route.distance.value
79
+ #=> 103712
80
+ route.duration.value
81
+ #=> 4337
82
+ route.steps
83
+ #=> [
84
+ {
85
+ "distance" => {
86
+ "text" => "0,1 km",
87
+ "value" => 125
88
+ },
89
+ "duration" => {
90
+ "text" => "1 min.",
91
+ "value" => 35
92
+ },
93
+ "end_location" => {
94
+ "lat" => 52.3556768,
95
+ "lng" => 4.9545739
96
+ },
97
+ "html_instructions" => "Rijd <b>naar het noordwesten</b>, richting het <b>Science Park</b>",
98
+ "polyline" => {
99
+ "points" => "oqp~Hqpf]?@?@?@KNOVEHA@A?s@wAQ]Q_@We@?A?ADI"
100
+ },
101
+ "start_location" => {
102
+ "lat" => 52.3549602,
103
+ "lng" => 4.9538473
104
+ },
105
+ "travel_mode" => "DRIVING"
106
+ },
107
+ {
108
+ "distance" => {
109
+ "text" => "37 m",
110
+ ........
111
+
112
+ ```
113
+
114
+ ### Options
115
+
116
+ The distance, duration and route methods allow you to pass an options hash to the Directions API.
117
+ All options can also be set as default_params for these services.
118
+
119
+ ```ruby
120
+ Google::Maps.configure do |config|
121
+ ...
122
+ config.default_params = {
123
+ directions_service: {
124
+ mode: 'transit',
125
+ transit_routing_preference: 'fewer_transfers'
126
+ }
127
+ }
128
+ ...
129
+ end
130
+
131
+ ```
132
+
133
+ The following options are permitted:
134
+
135
+ ```ruby
136
+ {
137
+ mode: 'driving', #default
138
+ transit_mode: 'train', #only used when mode is set to 'transit'
139
+ transit_routing_preference: 'fewer_transfers',
140
+ avoid: 'tolls',
141
+ arrival_time: 1545397430, # In seconds since midnight, January 1, 1970 UTC.
142
+ departure_time: 1545397430, # Cannot be in the past.
143
+ }
144
+
145
+ # NB! You can specify either departure_time or arrival_time, but not both.
146
+ TRAVEL_MODES = [
147
+ 'driving', #(Default) indicates standard driving directions using the road network.
148
+ 'bicycling', #requests bicycling directions via bicycle paths & preferred streets.
149
+ 'transit', #requests directions via public transit routes.
150
+ 'walking'
151
+ ]
152
+
153
+ TRANSIT_MODES = [
154
+ 'bus',
155
+ 'subway',
156
+ 'train',
157
+ 'tram',
158
+ 'rail' # This is equivalent to transit_mode=train|tram|subway
159
+ ]
160
+
161
+ TRANSIT_ROUTING_PREFERENCES = [
162
+ 'less_walking', prefer limited amounts of walking.
163
+ 'fewer_transfers' prefer a limited number of transfers.
164
+ ]
165
+
166
+ AVOID = [
167
+ 'tolls',
168
+ 'highways',
169
+ 'ferries',
170
+ 'indoor', # Avoid indoor steps for walking and transit directions.
171
+ ]
172
+
173
+ ```
174
+
175
+ ### Places
176
+
177
+ ```ruby
178
+ places = Google::Maps.places('Amsterdam')
179
+ places.first.text
180
+ #=> "Amsterdam, Nederland"
181
+ places.first.place_id
182
+ #=> "ChIJVXealLU_xkcRja_At0z9AGY"
183
+
184
+ place = Google::Maps.place("ChIJVXealLU_xkcRja_At0z9AGY")
185
+
186
+ place.latitude
187
+ #=> "52.3679843"
188
+
189
+ place.longitude
190
+ #=> "4.9035614"
191
+
192
+ place.address
193
+ #=> "Amsterdam, Nederland"
194
+ ```
195
+
196
+ ### Geocode
197
+
198
+ ```ruby
199
+ geocodes = Google::Maps.geocode("Science Park, Amsterdam")
200
+ geocodes.first.address = "Science Park, 1012 WX Amsterdam, Nederland"
201
+ geocodes.first.latitude = 52.3545543
202
+ geocodes.first.longitude = 4.9540916
203
+ ```
204
+
205
+ ## Testing
206
+
207
+ Run all tests:
208
+
209
+ ```
210
+ rspec spec/
211
+ ```
212
+
213
+ ## Copyright
214
+
215
+ Copyright (c) 2011-2019 Zilverline.
216
+ See [LICENSE](https://github.com/zilverline/google-maps/blob/master/LICENSE.md) for details.
data/Rakefile CHANGED
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler'
2
4
  Bundler::GemHelper.install_tasks
3
5
 
4
6
  require 'rspec/core/rake_task'
5
7
  RSpec::Core::RakeTask.new(:spec)
6
8
 
7
- task :default => :spec
9
+ task default: :spec
8
10
 
9
11
  namespace :doc do
10
12
  require 'yard'
@@ -13,7 +15,7 @@ namespace :doc do
13
15
  task.options = [
14
16
  '--protected',
15
17
  '--output-dir', 'doc/yard',
16
- '--markup', 'markdown',
18
+ '--markup', 'markdown'
17
19
  ]
18
20
  end
19
- end
21
+ end
@@ -1,34 +1,37 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "google-maps/version"
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
4
+ require 'google_maps/version'
4
5
 
5
6
  Gem::Specification.new do |s|
6
- s.name = "google-maps"
7
+ s.name = 'google-maps'
8
+ s.license = 'MIT'
7
9
  s.version = Google::Maps::VERSION
8
- s.authors = ["Daniel van Hoesel"]
9
- s.email = ["daniel@danielvanhoesel.nl"]
10
- s.homepage = "http://zilverline.com/"
11
- s.summary = %q{Ruby wrapper for the Google Maps API}
12
- s.description = %q{This is a ruby wrapper for the Google Maps api}
10
+ s.authors = ['Daniel van Hoesel']
11
+ s.email = ['info@zilverline.com']
12
+ s.homepage = 'http://zilverline.com/'
13
+ s.summary = 'Ruby wrapper for the Google Maps API'
14
+ s.description = 'This is a ruby wrapper for the Google Maps api'
13
15
 
14
- s.rubyforge_project = "google-maps"
16
+ s.rubyforge_project = 'google-maps'
15
17
 
16
18
  s.files = `git ls-files`.split("\n")
17
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
21
+ s.require_paths = ['lib']
20
22
 
21
23
  s.add_development_dependency('bundler', '~> 1.0')
22
- s.add_development_dependency('rake', '~> 0.9')
23
- s.add_development_dependency('rspec', '~> 2.6')
24
- s.add_development_dependency('rspec-its', '~> 1.0')
24
+ s.add_development_dependency('coveralls', '~> 0.8')
25
+ s.add_development_dependency('mocha', '~> 1.7.0')
26
+ s.add_development_dependency('rake', '~> 12.3.2')
27
+ s.add_development_dependency('rspec', '~> 3.8.0')
25
28
  s.add_development_dependency('rspec-collection_matchers', '~> 1.1')
26
- s.add_development_dependency('mocha', '~> 0.10')
29
+ s.add_development_dependency('rspec-its', '~> 1.2')
30
+ s.add_development_dependency('rubocop', '~> 0.61.1')
27
31
  s.add_development_dependency('simplecov', '~> 0.5')
28
- s.add_development_dependency('coveralls', '~> 0.8')
29
- s.add_development_dependency('yard', '~> 0.7')
30
- s.add_dependency('json', '>= 1.7.5')
31
- s.add_dependency('hashie', '~> 2.0.5')
32
- s.add_dependency('httpclient', '~> 2.7.1')
32
+ s.add_development_dependency('yard', '~> 0.9', '>= 0.9.11')
33
+ s.add_dependency('hashie', '~> 3.6', '>= 3.6.0')
34
+ s.add_dependency('httpclient', '~> 2.7', '>= 2.7.1')
35
+ s.add_dependency('json', '~> 2.1', '>= 2.1.0')
33
36
  s.add_dependency('ruby-hmac', '~> 0.4.0')
34
37
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('google_maps/configuration', __dir__)
4
+ require File.expand_path('google_maps/logger', __dir__)
5
+ require File.expand_path('google_maps/route', __dir__)
6
+ require File.expand_path('google_maps/place', __dir__)
7
+ require File.expand_path('google_maps/location', __dir__)
8
+
9
+ module Google
10
+ module Maps
11
+ extend Configuration
12
+ extend Logger
13
+
14
+ def self.route(from, to, options = {})
15
+ Route.new(from, to, options_with_defaults(options))
16
+ end
17
+
18
+ def self.distance(from, to, options = {})
19
+ Route.new(from, to, options_with_defaults(options)).distance.text
20
+ end
21
+
22
+ def self.duration(from, to, options = {})
23
+ Route.new(from, to, options_with_defaults(options)).duration.text
24
+ end
25
+
26
+ def self.places(keyword, language = default_language)
27
+ Place.find(keyword, language)
28
+ end
29
+
30
+ def self.place(place_id, language = default_language)
31
+ PlaceDetails.find(place_id, language)
32
+ end
33
+
34
+ def self.geocode(address, language = default_language)
35
+ Location.find(address, language)
36
+ rescue ZeroResultsException
37
+ []
38
+ end
39
+
40
+ class << self
41
+ protected
42
+
43
+ def options_with_defaults(options)
44
+ { language: default_language }.merge(options)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ # require 'net/http'
4
+ require 'httpclient'
5
+ require 'uri'
6
+ require 'json'
7
+ require 'base64'
8
+ require 'hmac'
9
+ require 'hmac-sha1'
10
+
11
+ require_relative 'result'
12
+
13
+ module Google
14
+ module Maps
15
+ class InvalidResponseException < StandardError; end
16
+ class InvalidPremierConfigurationException < StandardError; end
17
+ class ZeroResultsException < InvalidResponseException; end
18
+
19
+ class API
20
+ STATUS_OK = 'OK'.freeze
21
+ STATUS_ZERO_RESULTS = 'ZERO_RESULTS'.freeze
22
+
23
+ class << self
24
+ def query(service, args = {})
25
+ args = args.merge(Google::Maps.default_params[service]) if Google::Maps.default_params[service]
26
+ url = url(service, args)
27
+ response(url)
28
+ end
29
+
30
+ private
31
+
32
+ def decode_url_safe_base_64(value)
33
+ Base64.decode64(value.tr('-_', '+/'))
34
+ end
35
+
36
+ def encode_url_safe_base_64(value)
37
+ Base64.encode64(value).tr('+/', '-_')
38
+ end
39
+
40
+ def add_digital_signature(url)
41
+ parsed_url = url.is_a?(URI) ? url : URI.parse(url)
42
+ url_to_sign = parsed_url.path + '?' + parsed_url.query
43
+
44
+ # Decode the private key
45
+ raw_key = decode_url_safe_base_64(Google::Maps.client_secret)
46
+
47
+ # create a signature using the private key and the URL
48
+ sha1 = HMAC::SHA1.new(raw_key)
49
+ sha1 << url_to_sign
50
+ raw_sig = sha1.digest
51
+
52
+ # encode the signature into base64 for url use form.
53
+ signature = encode_url_safe_base_64(raw_sig)
54
+
55
+ # prepend the server and append the signature.
56
+ "#{parsed_url.scheme}://#{parsed_url.host}#{url_to_sign}&signature=#{signature}".strip
57
+ end
58
+
59
+ def response(url)
60
+ begin
61
+ result = Google::Maps::Result.new JSON.parse(HTTPClient.new.get_content(url))
62
+ rescue StandardError => error
63
+ Google::Maps.logger.error error.message.to_s
64
+ raise InvalidResponseException, "unknown error: #{error.message}"
65
+ end
66
+ handle_result_status(result.status)
67
+ result
68
+ end
69
+
70
+ def handle_result_status(status)
71
+ raise ZeroResultsException, "Google did not return any results: #{status}" if status == STATUS_ZERO_RESULTS
72
+ raise InvalidResponseException, "Google returned an error status: #{status}" if status != STATUS_OK
73
+ end
74
+
75
+ def url_with_api_key(service, args = {})
76
+ base_url(service, args.merge(api_key: Google::Maps.api_key))
77
+ end
78
+
79
+ def url_with_digital_signature(service, args = {})
80
+ url = base_url(service, args.merge(client: Google::Maps.client_id))
81
+ add_digital_signature(url)
82
+ end
83
+
84
+ def url(service, args = {})
85
+ if Google::Maps.authentication_mode == Google::Maps::Configuration::API_KEY
86
+ url_with_api_key(service, args)
87
+ elsif Google::Maps.authentication_mode == Google::Maps::Configuration::DIGITAL_SIGNATURE
88
+ url_with_digital_signature(service, args)
89
+ end
90
+ end
91
+
92
+ def base_url(service, args = {})
93
+ url = URI.parse("#{Google::Maps.end_point}#{Google::Maps.send(service)}/#{Google::Maps.format}#{query_string(args)}")
94
+ Google::Maps.logger.debug("url before possible signing: #{url}")
95
+ url.to_s
96
+ end
97
+
98
+ def query_string(args = {})
99
+ '?' + URI.encode_www_form(args) unless args.empty?
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end