google_timezone 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -0
- data/lib/google_timezone.rb +4 -0
- data/lib/google_timezone/base.rb +10 -6
- data/lib/google_timezone/version.rb +1 -1
- data/spec/google_timezone_spec.rb +40 -39
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae74565f590f1ed3353d714d67960da46128420b
|
4
|
+
data.tar.gz: 6aab6816a3c652bbdbd7b504d7d359e7f880e5dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dbdbc71964dd180eecb60a566c49246770dd9223f3b1de127215e6e87436295449845be1c66349c62b358c7ad6c8fd46d839c148ed53f34e1fb3dbd542ff5a9
|
7
|
+
data.tar.gz: 15121d942e69ec62b8b978b793dbea9501ed5b2218e65e0b646a90c16d9cafeb919217b2e228d5f4db0c932312ab3ceb215b852f376db17bbfd494ccd0db9fc2
|
data/README.md
CHANGED
@@ -38,6 +38,18 @@ Also there is `GoogleTimezone::Result#success?` method. It returns true if respo
|
|
38
38
|
|
39
39
|
The bang version `fetch!` raises an `GoogleTimezone::Error` exception with error message if response from Google wasn't success.
|
40
40
|
|
41
|
+
## Usage in tests/specs
|
42
|
+
|
43
|
+
During tests, you probably don't want to make remote calls. To achieve this, you should set a default stub in your test/spec helper file, e.g.:
|
44
|
+
|
45
|
+
# spec/support/google_timezone.rb
|
46
|
+
GoogleTimezone.set_default_stub(
|
47
|
+
'dstOffset' => 3600,
|
48
|
+
'rawOffset' => -10800,
|
49
|
+
'status' => 'OK',
|
50
|
+
'timeZoneId' => 'America/Sao_Paulo',
|
51
|
+
'timeZoneName' => 'Brasilia Summer Time'
|
52
|
+
)
|
41
53
|
|
42
54
|
## Contributing
|
43
55
|
|
data/lib/google_timezone.rb
CHANGED
data/lib/google_timezone/base.rb
CHANGED
@@ -6,7 +6,11 @@ module GoogleTimezone
|
|
6
6
|
class Error < StandardError; end
|
7
7
|
|
8
8
|
class Base
|
9
|
-
ALLOWED_PARAMS = [:language, :
|
9
|
+
ALLOWED_PARAMS = [:language, :timestamp, :client, :signature, :key]
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :default_stub
|
13
|
+
end
|
10
14
|
|
11
15
|
def initialize(*args)
|
12
16
|
@lat, @lon = if args.first.is_a? Array
|
@@ -21,15 +25,15 @@ module GoogleTimezone
|
|
21
25
|
|
22
26
|
def fetch
|
23
27
|
location = [@lat, @lon].join(',')
|
24
|
-
params = { location: location,
|
28
|
+
params = { location: location, timestamp: Time.now.to_i }.merge(@options)
|
25
29
|
result = get_result(params)
|
26
30
|
Result.new(result)
|
27
31
|
end
|
28
32
|
|
29
33
|
def fetch!
|
30
|
-
|
31
|
-
|
32
|
-
|
34
|
+
fetch.tap do |result|
|
35
|
+
raise(GoogleTimezone::Error.new(result.result)) unless result.success?
|
36
|
+
end
|
33
37
|
end
|
34
38
|
|
35
39
|
private
|
@@ -47,7 +51,7 @@ module GoogleTimezone
|
|
47
51
|
end
|
48
52
|
|
49
53
|
def get_result(params)
|
50
|
-
open(url(params)) { |r| JSON.parse(r.read) }
|
54
|
+
self.class.default_stub || open(url(params)) { |r| JSON.parse(r.read) }
|
51
55
|
end
|
52
56
|
end
|
53
57
|
end
|
@@ -1,23 +1,20 @@
|
|
1
1
|
require 'google_timezone'
|
2
2
|
|
3
3
|
describe GoogleTimezone do
|
4
|
-
|
5
|
-
|
6
|
-
let(:base_instance) { spy }
|
7
|
-
let(:result) { GoogleTimezone::Result.new({}) }
|
4
|
+
def self.stub_remote_calls!
|
5
|
+
let(:raw_result) { {} }
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
7
|
+
before do
|
8
|
+
allow_any_instance_of(GoogleTimezone::Base).to(
|
9
|
+
receive(:get_result).and_return(raw_result)
|
10
|
+
)
|
11
|
+
end
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
.and_return(result)
|
18
|
-
)
|
19
|
-
end
|
14
|
+
describe '.fetch' do
|
15
|
+
stub_remote_calls!
|
20
16
|
|
17
|
+
context 'without any optional parameters' do
|
21
18
|
it 'should accept separate lat/long paramenters' do
|
22
19
|
g = GoogleTimezone.fetch(0, 0)
|
23
20
|
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
@@ -30,32 +27,20 @@ describe GoogleTimezone do
|
|
30
27
|
end
|
31
28
|
|
32
29
|
context 'with optional parameters' do
|
33
|
-
let(:base_instance) { spy }
|
34
|
-
let(:result) { GoogleTimezone::Result.new(0) }
|
35
30
|
let(:valid_params) { { timestamp: 1331161200,
|
36
31
|
key: 'A24br8v2w' } }
|
37
32
|
let(:invalid_params) { { timestamp: 1331161200,
|
38
33
|
key: 'A24br8v2w',
|
39
34
|
unallowed_param: true } }
|
40
35
|
|
41
|
-
before(:each) do
|
42
|
-
allow_any_instance_of(GoogleTimezone::Base).to(
|
43
|
-
receive(:new).with(0, 0, valid_params)
|
44
|
-
.and_return(base_instance)
|
45
|
-
)
|
46
|
-
|
47
|
-
allow(base_instance).to(
|
48
|
-
receive(:fetch).with(no_args)
|
49
|
-
.and_return(result)
|
50
|
-
)
|
51
|
-
end
|
52
|
-
|
53
36
|
it 'should pass allowed parameters on to the query' do
|
54
37
|
g = GoogleTimezone.fetch(0, 0, valid_params)
|
55
38
|
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
56
39
|
end
|
57
40
|
|
58
41
|
it 'should reject unallowed parameters' do
|
42
|
+
a_hash_excluding = -> (*keys) { satisfy { |actual| (keys & actual.keys).empty? } }
|
43
|
+
expect_any_instance_of(GoogleTimezone::Base).to receive(:get_result).with(a_hash_excluding[:unallowed_param])
|
59
44
|
g = GoogleTimezone.fetch(0, 0, invalid_params)
|
60
45
|
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
61
46
|
end
|
@@ -63,21 +48,37 @@ describe GoogleTimezone do
|
|
63
48
|
end
|
64
49
|
|
65
50
|
describe '.fetch!' do
|
51
|
+
stub_remote_calls!
|
52
|
+
|
66
53
|
describe 'if result is not successful' do
|
67
|
-
|
68
|
-
|
54
|
+
it 'should raise an error' do
|
55
|
+
expect { GoogleTimezone.fetch!(0, 0) }.to raise_error(GoogleTimezone::Error)
|
56
|
+
end
|
57
|
+
end
|
69
58
|
|
70
|
-
|
71
|
-
|
72
|
-
before {
|
73
|
-
allow_any_instance_of(GoogleTimezone::Base).to(
|
74
|
-
receive(:fetch).and_return(result)
|
75
|
-
)
|
76
|
-
}
|
59
|
+
describe 'if result is successul' do
|
60
|
+
let(:raw_result) { { 'status' => 'OK' } }
|
77
61
|
|
78
|
-
it 'should
|
79
|
-
|
62
|
+
it 'should return a result' do
|
63
|
+
g = GoogleTimezone.fetch!(0, 0)
|
64
|
+
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
80
65
|
end
|
81
66
|
end
|
82
67
|
end
|
68
|
+
|
69
|
+
describe '.set_default_stub' do
|
70
|
+
after { GoogleTimezone.set_default_stub(nil) }
|
71
|
+
|
72
|
+
it 'does not make a remote call' do
|
73
|
+
GoogleTimezone.set_default_stub({})
|
74
|
+
expect_any_instance_of(GoogleTimezone::Base).not_to receive(:open)
|
75
|
+
GoogleTimezone.fetch(0, 0)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'returns the configured stub' do
|
79
|
+
GoogleTimezone.set_default_stub('timeZoneId' => 'Europe/Berlin')
|
80
|
+
g = GoogleTimezone.fetch(0, 0)
|
81
|
+
expect(g.time_zone_id).to eq('Europe/Berlin')
|
82
|
+
end
|
83
|
+
end
|
83
84
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_timezone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sck-v
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|