google_timezone 0.0.3 → 0.0.4
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 +1 -1
- data/lib/google_timezone.rb +2 -2
- data/lib/google_timezone/base.rb +8 -9
- data/lib/google_timezone/version.rb +1 -1
- data/spec/google_timezone_result_spec.rb +3 -3
- data/spec/google_timezone_spec.rb +67 -15
- 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: dea49ca959de6ba6012065a8aeff097548ac5b90
|
4
|
+
data.tar.gz: 5daf4c90ce40c1babbc0daa07810b51adb9b423d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f067c96016a48390ed2904fa607e29a8a725e44184a923371cbc461a76c75e23ab43138e027bba52c02154e55aa1b44b2083b7253cace049a782cb742290fa92
|
7
|
+
data.tar.gz: 8af5396dcb560ea1d5a5c37375f96ecfe5c597c6e8d824c67eaff3ac0d7a485271787de9cd137f020e85d1a29ce64383a2f52751b348879b9db2002c083dc6d6
|
data/README.md
CHANGED
@@ -36,7 +36,7 @@ It will get `GoogleTimezone::Result` object which maps major google api responce
|
|
36
36
|
More information [here](https://developers.google.com/maps/documentation/timezone/)
|
37
37
|
Also there is `GoogleTimezone::Result#success?` method. It returns true if responce was successful.
|
38
38
|
|
39
|
-
The bang version `fetch!` raises an error if
|
39
|
+
The bang version `fetch!` raises an `GoogleTimezone::Error` exception with error message if response from Google wasn't success.
|
40
40
|
|
41
41
|
|
42
42
|
## Contributing
|
data/lib/google_timezone.rb
CHANGED
data/lib/google_timezone/base.rb
CHANGED
@@ -6,7 +6,7 @@ module GoogleTimezone
|
|
6
6
|
class Error < StandardError; end
|
7
7
|
|
8
8
|
class Base
|
9
|
-
|
9
|
+
ALLOWED_PARAMS = [:language, :sensor, :timestamp, :client, :signature, :key]
|
10
10
|
|
11
11
|
def initialize(*args)
|
12
12
|
@lat, @lon = if args.first.is_a? Array
|
@@ -14,8 +14,9 @@ module GoogleTimezone
|
|
14
14
|
else
|
15
15
|
args[0..1]
|
16
16
|
end
|
17
|
+
|
17
18
|
@options = extract_options!(args)
|
18
|
-
@options.reject! { |key,
|
19
|
+
@options.reject! { |key, _| !ALLOWED_PARAMS.include?(key) }
|
19
20
|
end
|
20
21
|
|
21
22
|
def fetch
|
@@ -32,23 +33,21 @@ module GoogleTimezone
|
|
32
33
|
end
|
33
34
|
|
34
35
|
private
|
36
|
+
|
35
37
|
def hash_to_query(hash)
|
36
|
-
|
37
|
-
hash.collect{ |p|
|
38
|
-
p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '='
|
39
|
-
}.compact.sort * '&'
|
38
|
+
hash.collect { |key, val| "#{key}=#{val}" }.join('&')
|
40
39
|
end
|
41
40
|
|
42
41
|
def url(params)
|
43
42
|
"https://maps.googleapis.com/maps/api/timezone/json?#{hash_to_query(params)}"
|
44
43
|
end
|
45
44
|
|
46
|
-
def extract_options!(
|
47
|
-
args.last.is_a?(::Hash) ? pop : {}
|
45
|
+
def extract_options!(args)
|
46
|
+
args.last.is_a?(::Hash) ? args.pop : {}
|
48
47
|
end
|
49
48
|
|
50
49
|
def get_result(params)
|
51
50
|
open(url(params)) { |r| JSON.parse(r.read) }
|
52
51
|
end
|
53
52
|
end
|
54
|
-
end
|
53
|
+
end
|
@@ -3,11 +3,11 @@ require 'google_timezone/result'
|
|
3
3
|
describe GoogleTimezone::Result do
|
4
4
|
it 'should be success' do
|
5
5
|
result = GoogleTimezone::Result.new({ 'status' => 'OK' })
|
6
|
-
result.
|
6
|
+
expect(result).to be_success
|
7
7
|
end
|
8
8
|
|
9
9
|
it 'should not be succsess' do
|
10
10
|
result = GoogleTimezone::Result.new({ 'status' => 'ZERO_RESULTS' })
|
11
|
-
result.
|
11
|
+
expect(result).to_not be_success
|
12
12
|
end
|
13
|
-
end
|
13
|
+
end
|
@@ -1,30 +1,82 @@
|
|
1
1
|
require 'google_timezone'
|
2
2
|
|
3
3
|
describe GoogleTimezone do
|
4
|
-
describe '
|
5
|
-
|
4
|
+
describe '.fetch' do
|
5
|
+
context 'without any optional parameters' do
|
6
|
+
let(:base_instance) { spy }
|
7
|
+
let(:result) { GoogleTimezone::Result.new({}) }
|
8
|
+
|
9
|
+
before do
|
10
|
+
allow_any_instance_of(GoogleTimezone::Base).to(
|
11
|
+
receive(:new).with(any_args)
|
12
|
+
.and_return(base_instance)
|
13
|
+
)
|
14
|
+
|
15
|
+
allow(base_instance).to(
|
16
|
+
receive(:fetch).with(no_args)
|
17
|
+
.and_return(result)
|
18
|
+
)
|
19
|
+
end
|
6
20
|
|
7
|
-
|
8
|
-
|
9
|
-
|
21
|
+
it 'should accept separate lat/long paramenters' do
|
22
|
+
g = GoogleTimezone.fetch(0, 0)
|
23
|
+
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should accept a single lat/long array parameter' do
|
27
|
+
g = GoogleTimezone.fetch([0, 0])
|
28
|
+
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
29
|
+
end
|
10
30
|
end
|
11
31
|
|
12
|
-
|
13
|
-
|
14
|
-
|
32
|
+
context 'with optional parameters' do
|
33
|
+
let(:base_instance) { spy }
|
34
|
+
let(:result) { GoogleTimezone::Result.new(0) }
|
35
|
+
let(:valid_params) { { timestamp: 1331161200,
|
36
|
+
key: 'A24br8v2w' } }
|
37
|
+
let(:invalid_params) { { timestamp: 1331161200,
|
38
|
+
key: 'A24br8v2w',
|
39
|
+
unallowed_param: true } }
|
40
|
+
|
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
|
+
it 'should pass allowed parameters on to the query' do
|
54
|
+
g = GoogleTimezone.fetch(0, 0, valid_params)
|
55
|
+
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should reject unallowed parameters' do
|
59
|
+
g = GoogleTimezone.fetch(0, 0, invalid_params)
|
60
|
+
expect(g).to be_an_instance_of(GoogleTimezone::Result)
|
61
|
+
end
|
15
62
|
end
|
16
63
|
end
|
17
64
|
|
18
|
-
describe '
|
19
|
-
describe 'if result is not
|
65
|
+
describe '.fetch!' do
|
66
|
+
describe 'if result is not successful' do
|
20
67
|
let(:result) { GoogleTimezone::Result.new({}) }
|
68
|
+
let(:error) { GoogleTimezone::Error }
|
21
69
|
|
22
|
-
before { result.
|
23
|
-
before { result.
|
24
|
-
before {
|
70
|
+
before { allow(result).to receive(:success?).and_return(false) }
|
71
|
+
before { allow(result).to receive(:result).and_return('error message') }
|
72
|
+
before {
|
73
|
+
allow_any_instance_of(GoogleTimezone::Base).to(
|
74
|
+
receive(:fetch).and_return(result)
|
75
|
+
)
|
76
|
+
}
|
25
77
|
|
26
|
-
it 'should raise error' do
|
27
|
-
expect { GoogleTimezone.fetch!(0,0) }.to raise_error(
|
78
|
+
it 'should raise an error' do
|
79
|
+
expect { GoogleTimezone.fetch!(0,0) }.to raise_error(error)
|
28
80
|
end
|
29
81
|
end
|
30
82
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sck-v
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|