timezone 0.3.11 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.markdown +4 -0
- data/README.markdown +1 -0
- data/lib/timezone/configure.rb +23 -0
- data/lib/timezone/lookup/google.rb +26 -5
- data/lib/timezone/version.rb +1 -1
- data/test/google_lookup_test.rb +26 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d2bc909888147c6c7fa503b834d851be11d210d
|
4
|
+
data.tar.gz: e4361fd7832d57ebfd1d54d9e5846badeaf7c489
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6e23b7d2937984bc404b826bca5f3d2ef6e4f05d05f42cf940c2e71de5edf10be6054561112c209ddcbac61d9a4b0cc3e0eb955e974da95c42629dfb62c4222
|
7
|
+
data.tar.gz: 982b85508bd4b1691c37c0b67036df691c75c9b5b1c821bc99d76187c967ee40e60d32a47b4e87af86d852b3beb03b209c9994475e18c5e2eb3fc460e95097a7
|
data/CHANGES.markdown
CHANGED
data/README.markdown
CHANGED
@@ -78,6 +78,7 @@ Next, add the following to your application.rb file, or before you perform a coo
|
|
78
78
|
|
79
79
|
Timezone::Configure.begin do |c|
|
80
80
|
c.google_api_key = 'your_google_api_key_goes_here'
|
81
|
+
c.google_client_id = 'your_google_client_id' # only if using 'Google for Work'
|
81
82
|
end
|
82
83
|
|
83
84
|
Finally, for either geonames or Google implementation, pass the coordinates to your timezone initialization function.
|
data/lib/timezone/configure.rb
CHANGED
@@ -40,6 +40,22 @@ module Timezone
|
|
40
40
|
@google_api_key = api_key
|
41
41
|
end
|
42
42
|
|
43
|
+
# The Google Client ID (for enterprise)
|
44
|
+
#
|
45
|
+
# @return [String]
|
46
|
+
# the Google Client ('abc123')
|
47
|
+
def self.google_client_id
|
48
|
+
@google_client_id ||= nil
|
49
|
+
end
|
50
|
+
|
51
|
+
# Google Client ID (for enterprise)
|
52
|
+
#
|
53
|
+
# @param [String] client
|
54
|
+
# the Google Client
|
55
|
+
def self.google_client_id=(client)
|
56
|
+
@google_client_id = client
|
57
|
+
end
|
58
|
+
|
43
59
|
# Use Google API if key has been set
|
44
60
|
#
|
45
61
|
# @return [Boolean]
|
@@ -47,6 +63,13 @@ module Timezone
|
|
47
63
|
!!google_api_key
|
48
64
|
end
|
49
65
|
|
66
|
+
# Sign Google API request if client given (for enterprise)
|
67
|
+
#
|
68
|
+
# @return [Boolean]
|
69
|
+
def self.use_google_enterprise?
|
70
|
+
use_google? && !!google_client_id
|
71
|
+
end
|
72
|
+
|
50
73
|
def self.lookup
|
51
74
|
use_google? ? google_lookup : geonames_lookup
|
52
75
|
end
|
@@ -2,6 +2,9 @@ require 'timezone/lookup/basic'
|
|
2
2
|
require 'timezone/error'
|
3
3
|
require 'json'
|
4
4
|
require 'uri'
|
5
|
+
require 'base64'
|
6
|
+
require 'openssl'
|
7
|
+
require 'cgi'
|
5
8
|
|
6
9
|
module Timezone
|
7
10
|
module Lookup
|
@@ -16,6 +19,10 @@ module Timezone
|
|
16
19
|
def lookup(lat,lng)
|
17
20
|
response = client.get(url(lat,lng))
|
18
21
|
|
22
|
+
if response.code == '403'
|
23
|
+
raise(Timezone::Error::Google, '403 Forbidden')
|
24
|
+
end
|
25
|
+
|
19
26
|
return unless response.code =~ /^2\d\d$/
|
20
27
|
data = JSON.parse(response.body)
|
21
28
|
|
@@ -30,13 +37,27 @@ module Timezone
|
|
30
37
|
|
31
38
|
private
|
32
39
|
|
40
|
+
def authorize(url)
|
41
|
+
if config.use_google_enterprise?
|
42
|
+
url += "&client=#{CGI.escape(config.google_client_id)}"
|
43
|
+
|
44
|
+
sha1 = OpenSSL::Digest.new('sha1')
|
45
|
+
binary_key = Base64.decode64(config.google_api_key.tr('-_','+/'))
|
46
|
+
binary_signature = OpenSSL::HMAC.digest(sha1, binary_key, url)
|
47
|
+
signature = Base64.encode64(binary_signature).tr('+/','-_').strip
|
48
|
+
|
49
|
+
url + "&signature=#{signature}"
|
50
|
+
else
|
51
|
+
url + "&key=#{config.google_api_key}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
33
55
|
def url(lat,lng)
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
'key' => config.google_api_key)
|
56
|
+
query = URI.encode_www_form(
|
57
|
+
'location' => "#{lat},#{lng}",
|
58
|
+
'timestamp' => Time.now.to_i)
|
38
59
|
|
39
|
-
|
60
|
+
authorize("/maps/api/timezone/json?#{query}")
|
40
61
|
end
|
41
62
|
end
|
42
63
|
end
|
data/lib/timezone/version.rb
CHANGED
data/test/google_lookup_test.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
require 'timezone/configure'
|
2
2
|
require 'timezone/lookup/google'
|
3
3
|
require 'minitest/autorun'
|
4
|
+
require 'timecop'
|
4
5
|
require_relative 'http_test_client'
|
5
6
|
|
6
7
|
class GoogleLookupTest < ::Minitest::Unit::TestCase
|
7
8
|
def setup
|
8
9
|
Timezone::Configure.begin do |c|
|
9
10
|
c.http_client = HTTPTestClient
|
10
|
-
c.google_api_key = '
|
11
|
+
c.google_api_key = 'MTIzYWJj'
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
@@ -23,7 +24,7 @@ class GoogleLookupTest < ::Minitest::Unit::TestCase
|
|
23
24
|
Timezone::Configure.begin{ |c| c.google_api_key = nil }
|
24
25
|
assert_raises(::Timezone::Error::InvalidConfig){ lookup }
|
25
26
|
ensure
|
26
|
-
Timezone::Configure.begin{ |c| c.google_api_key = '
|
27
|
+
Timezone::Configure.begin{ |c| c.google_api_key = 'MTIzYWJj' }
|
27
28
|
end
|
28
29
|
|
29
30
|
def test_google_using_lat_lon_coordinates
|
@@ -39,6 +40,29 @@ class GoogleLookupTest < ::Minitest::Unit::TestCase
|
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
43
|
+
def test_url_non_enterprise
|
44
|
+
Timecop.freeze(Time.at(1433347661)) do
|
45
|
+
result = lookup.send(:url, '123', '123')
|
46
|
+
assert_equal "/maps/api/timezone/json?location=123%2C123×tamp=1433347661&key=MTIzYWJj", result
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_url_enterprise
|
51
|
+
Timezone::Configure.begin do |c|
|
52
|
+
c.google_client_id = '123&asdf'
|
53
|
+
end
|
54
|
+
|
55
|
+
Timecop.freeze(Time.at(1433347661)) do
|
56
|
+
result = lookup.send(:url, '123', '123')
|
57
|
+
assert_equal '/maps/api/timezone/json?location=123%2C123×tamp=1433347661&client=123%26asdf&signature=B1TNSSvIw9Wvf_ZjjW5uRzGm4F4=', result
|
58
|
+
end
|
59
|
+
|
60
|
+
ensure
|
61
|
+
Timezone::Configure.begin do |c|
|
62
|
+
c.google_client_id = nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
42
66
|
private
|
43
67
|
|
44
68
|
def mock_path
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timezone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pan Thomakos
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -721,7 +721,7 @@ rubyforge_project: timezone
|
|
721
721
|
rubygems_version: 2.0.14
|
722
722
|
signing_key:
|
723
723
|
specification_version: 4
|
724
|
-
summary: timezone-0.
|
724
|
+
summary: timezone-0.4.0
|
725
725
|
test_files:
|
726
726
|
- test/basic_lookup_test.rb
|
727
727
|
- test/data/Helsinki_rules_without_timestamps.json
|