google_timezone 0.0.1 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a58b018c388299fe698740b490d9d51bbb5ca56c8f8e37cbe7b32df1f72dabe9
4
+ data.tar.gz: 545603d1681053cb178989dc65a522c6abcb4f564a01353116fa104aac8f9f47
5
+ SHA512:
6
+ metadata.gz: 190a8ac37e5ac71de5134726dca3812f639191391751edfa9f5e4f13bf18ea61d185413145cafdfa1f81d284e750315c606d2a404d516169c63fd6a67483a6f7
7
+ data.tar.gz: 1e4177b6f3934d4d95ecf5244283e82cd07201b472d365a6c8d9306494409a8f26e0901fbb086a50ac4130bda1f3c6e15b3d5c820df431c5d1437d1c9d511b06
data/README.md CHANGED
@@ -18,16 +18,38 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
+ You can pass latitude and longitude to gem in several ways: with array or separated values.
22
+
23
+ GoogleTimezone.fetch([latitude, longitude])
24
+ GoogleTimezone.fetch(latitude, longitude)
25
+
26
+ Any other options which are described in google's documentation you can pass via options hash
27
+
28
+ GoogleTimezone.fetch(latitude, longitude, language: 'en', signature: 'key')
29
+
21
30
  It uses latitude and longitude to retrieve timezone info.
22
31
 
23
- GoogleTimezone.fetch(50.1196004, 8.679918299999999)
32
+ result = GoogleTimezone.fetch(50.1196004, 8.679918299999999)
33
+ result.time_zone_name => 'Europe/Berlin'
24
34
 
25
35
  It will get `GoogleTimezone::Result` object which maps major google api responce items named in snake case.
26
36
  More information [here](https://developers.google.com/maps/documentation/timezone/)
27
37
  Also there is `GoogleTimezone::Result#success?` method. It returns true if responce was successful.
28
38
 
29
- 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
+
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.:
30
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
+ )
31
53
 
32
54
  ## Contributing
33
55
 
@@ -3,13 +3,17 @@ require 'google_timezone/base'
3
3
  require 'google_timezone/result'
4
4
 
5
5
  module GoogleTimezone
6
- def fetch(*args)
7
- Base.new(args).fetch
8
- end
6
+ class << self
7
+ def fetch(*args)
8
+ Base.new(*args).fetch
9
+ end
9
10
 
10
- def fetch!(*args)
11
- Base.new(args).fetch!
12
- end
11
+ def fetch!(*args)
12
+ Base.new(*args).fetch!
13
+ end
13
14
 
14
- module_function :fetch, :fetch!
15
+ def set_default_stub(default_stub)
16
+ Base.default_stub = default_stub
17
+ end
18
+ end
15
19
  end
@@ -2,8 +2,15 @@ require 'json'
2
2
  require 'open-uri'
3
3
 
4
4
  module GoogleTimezone
5
+
6
+ class Error < StandardError; end
7
+
5
8
  class Base
6
- @allowed_params = [:language, :sensor, :timestamp, :client, :signature]
9
+ ALLOWED_PARAMS = [:language, :timestamp, :client, :signature, :key]
10
+
11
+ class << self
12
+ attr_accessor :default_stub
13
+ end
7
14
 
8
15
  def initialize(*args)
9
16
  @lat, @lon = if args.first.is_a? Array
@@ -11,41 +18,40 @@ module GoogleTimezone
11
18
  else
12
19
  args[0..1]
13
20
  end
21
+
14
22
  @options = extract_options!(args)
15
- @options.reject! { |key, value| !@allowed_params.include? key }
23
+ @options.reject! { |key, _| !ALLOWED_PARAMS.include?(key) }
16
24
  end
17
25
 
18
26
  def fetch
19
27
  location = [@lat, @lon].join(',')
20
- params = { location: location, sensor: false, timestamp: Time.now.to_i }.merge(@options)
28
+ params = { location: location, timestamp: Time.now.to_i }.merge(@options)
21
29
  result = get_result(params)
22
30
  Result.new(result)
23
31
  end
24
32
 
25
33
  def fetch!
26
- result = fetch
27
- raise_error(result.result) unless result.success?
28
- result
34
+ fetch.tap do |result|
35
+ raise(GoogleTimezone::Error.new(result.result)) unless result.success?
36
+ end
29
37
  end
30
38
 
31
39
  private
40
+
32
41
  def hash_to_query(hash)
33
- require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
34
- hash.collect{ |p|
35
- p[1].nil? ? nil : p.map{ |i| CGI.escape i.to_s } * '='
36
- }.compact.sort * '&'
42
+ hash.collect { |key, val| "#{key}=#{val}" }.join('&')
37
43
  end
38
44
 
39
45
  def url(params)
40
46
  "https://maps.googleapis.com/maps/api/timezone/json?#{hash_to_query(params)}"
41
47
  end
42
48
 
43
- def extract_options!(*args)
44
- args.last.is_a?(::Hash) ? pop : {}
49
+ def extract_options!(args)
50
+ args.last.is_a?(::Hash) ? args.pop : {}
45
51
  end
46
52
 
47
53
  def get_result(params)
48
- open(url(params)) { |r| JSON.parse(r.read) }
54
+ self.class.default_stub || URI.open(url(params)) { |r| JSON.parse(r.read) }
49
55
  end
50
56
  end
51
- end
57
+ end
@@ -1,3 +1,3 @@
1
1
  module GoogleTimezone
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.6'
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,15 +1,84 @@
1
1
  require 'google_timezone'
2
2
 
3
3
  describe GoogleTimezone do
4
- describe 'initialize' do
5
- it 'correct with separate lat and lon' do
6
- g = GoogleTimezone::Base.new(0,0)
7
- g.should be_an_instance_of(GoogleTimezone::Base)
4
+ def self.stub_remote_calls!
5
+ let(:raw_result) { {} }
6
+
7
+ before do
8
+ allow_any_instance_of(GoogleTimezone::Base).to(
9
+ receive(:get_result).and_return(raw_result)
10
+ )
11
+ end
12
+ end
13
+
14
+ describe '.fetch' do
15
+ stub_remote_calls!
16
+
17
+ context 'without any optional parameters' do
18
+ it 'should accept separate lat/long paramenters' do
19
+ g = GoogleTimezone.fetch(0, 0)
20
+ expect(g).to be_an_instance_of(GoogleTimezone::Result)
21
+ end
22
+
23
+ it 'should accept a single lat/long array parameter' do
24
+ g = GoogleTimezone.fetch([0, 0])
25
+ expect(g).to be_an_instance_of(GoogleTimezone::Result)
26
+ end
27
+ end
28
+
29
+ context 'with optional parameters' do
30
+ let(:valid_params) { { timestamp: 1331161200,
31
+ key: 'A24br8v2w' } }
32
+ let(:invalid_params) { { timestamp: 1331161200,
33
+ key: 'A24br8v2w',
34
+ unallowed_param: true } }
35
+
36
+ it 'should pass allowed parameters on to the query' do
37
+ g = GoogleTimezone.fetch(0, 0, valid_params)
38
+ expect(g).to be_an_instance_of(GoogleTimezone::Result)
39
+ end
40
+
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])
44
+ g = GoogleTimezone.fetch(0, 0, invalid_params)
45
+ expect(g).to be_an_instance_of(GoogleTimezone::Result)
46
+ end
47
+ end
48
+ end
49
+
50
+ describe '.fetch!' do
51
+ stub_remote_calls!
52
+
53
+ describe 'if result is not successful' do
54
+ it 'should raise an error' do
55
+ expect { GoogleTimezone.fetch!(0, 0) }.to raise_error(GoogleTimezone::Error)
56
+ end
57
+ end
58
+
59
+ describe 'if result is successul' do
60
+ let(:raw_result) { { 'status' => 'OK' } }
61
+
62
+ it 'should return a result' do
63
+ g = GoogleTimezone.fetch!(0, 0)
64
+ expect(g).to be_an_instance_of(GoogleTimezone::Result)
65
+ end
66
+ end
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)
8
76
  end
9
77
 
10
- it 'correct with array lat and lon' do
11
- g = GoogleTimezone::Base.new([0,0])
12
- g.should be_an_instance_of(GoogleTimezone::Base)
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')
13
82
  end
14
83
  end
15
84
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_timezone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.0.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - sck-v
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-15 00:00:00.000000000 Z
11
+ date: 2021-07-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec-mocks
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Get timezone info by known coordinates
@@ -50,7 +45,7 @@ executables: []
50
45
  extensions: []
51
46
  extra_rdoc_files: []
52
47
  files:
53
- - .gitignore
48
+ - ".gitignore"
54
49
  - Gemfile
55
50
  - LICENSE.txt
56
51
  - README
@@ -65,27 +60,25 @@ files:
65
60
  - spec/google_timezone_spec.rb
66
61
  homepage: ''
67
62
  licenses: []
63
+ metadata: {}
68
64
  post_install_message:
69
65
  rdoc_options: []
70
66
  require_paths:
71
67
  - lib
72
68
  required_ruby_version: !ruby/object:Gem::Requirement
73
- none: false
74
69
  requirements:
75
- - - ! '>='
70
+ - - ">="
76
71
  - !ruby/object:Gem::Version
77
72
  version: '0'
78
73
  required_rubygems_version: !ruby/object:Gem::Requirement
79
- none: false
80
74
  requirements:
81
- - - ! '>='
75
+ - - ">="
82
76
  - !ruby/object:Gem::Version
83
77
  version: '0'
84
78
  requirements: []
85
- rubyforge_project:
86
- rubygems_version: 1.8.24
79
+ rubygems_version: 3.1.2
87
80
  signing_key:
88
- specification_version: 3
81
+ specification_version: 4
89
82
  summary: Small gem to get timezone info by known coordinates using google timezone
90
83
  api. https://developers.google.com/maps/documentation/timezone/
91
84
  test_files: