google_timezone 0.0.3 → 0.0.4

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: 6a7922f88d3b6882b797df4a60ed586c5b84b3bb
4
- data.tar.gz: e629e85380e69c9d6e1a6fbeb19be2f9b974cc3e
3
+ metadata.gz: dea49ca959de6ba6012065a8aeff097548ac5b90
4
+ data.tar.gz: 5daf4c90ce40c1babbc0daa07810b51adb9b423d
5
5
  SHA512:
6
- metadata.gz: bba8f7376e924527ae559ff0f9b28b8569be50ec271229e13225e87885a9085e4d8d3981ab169ceb46a5e1be2ea0f2ad46a84de8da33d4ef2d481dc84e9eee49
7
- data.tar.gz: d46c902fade66c349023ea5df5993c93846b46292c9d79e6ec901168267355128098b408f0c157223b422cd4eade19e48fe3f8ece594f0e82e58ed72528bcc25
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 google responce is not ok.
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
@@ -5,11 +5,11 @@ require 'google_timezone/result'
5
5
  module GoogleTimezone
6
6
  class << self
7
7
  def fetch(*args)
8
- Base.new(args).fetch
8
+ Base.new(*args).fetch
9
9
  end
10
10
 
11
11
  def fetch!(*args)
12
- Base.new(args).fetch!
12
+ Base.new(*args).fetch!
13
13
  end
14
14
  end
15
15
  end
@@ -6,7 +6,7 @@ module GoogleTimezone
6
6
  class Error < StandardError; end
7
7
 
8
8
  class Base
9
- @allowed_params = [:language, :sensor, :timestamp, :client, :signature, :key]
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, value| !@allowed_params.include? 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
- require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
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!(*args)
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
@@ -1,3 +1,3 @@
1
1
  module GoogleTimezone
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  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.should be_success
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.should_not be_success
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 'initialize' do
5
- before { GoogleTimezone::Base.any_instance.should_receive(:fetch).with(no_args).and_return(GoogleTimezone::Result.new(0)) }
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
- it 'correct with separate lat and lon' do
8
- g = GoogleTimezone.fetch(0,0)
9
- g.should be_an_instance_of(GoogleTimezone::Result)
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
- it 'correct with array lat and lon' do
13
- g = GoogleTimezone.fetch([0,0])
14
- g.should be_an_instance_of(GoogleTimezone::Result)
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 '#fetch!' do
19
- describe 'if result is not success' do
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.stub!(:success?) { false } }
23
- before { result.stub!(:result) { 'error message' } }
24
- before { GoogleTimezone::Base.any_instance.stub(:fetch) { result } }
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(GoogleTimezone::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.3
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-04-22 00:00:00.000000000 Z
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec