timezone 0.6.0 → 0.99.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +99 -1
  4. data/.travis.yml +6 -3
  5. data/CHANGES.markdown +10 -0
  6. data/Gemfile +1 -1
  7. data/README.markdown +129 -89
  8. data/Rakefile +7 -4
  9. data/benchmark.rb +13 -13
  10. data/lib/timezone/active_support.rb +147 -134
  11. data/lib/timezone/configure.rb +131 -110
  12. data/lib/timezone/deprecate.rb +32 -0
  13. data/lib/timezone/error.rb +16 -5
  14. data/lib/timezone/loader.rb +19 -16
  15. data/lib/timezone/lookup/basic.rb +24 -2
  16. data/lib/timezone/lookup/geonames.rb +9 -5
  17. data/lib/timezone/lookup/google.rb +24 -15
  18. data/lib/timezone/lookup/test.rb +8 -8
  19. data/lib/timezone/lookup.rb +68 -0
  20. data/lib/timezone/net_http_client.rb +23 -9
  21. data/lib/timezone/nil_zone.rb +32 -0
  22. data/lib/timezone/parser.rb +10 -5
  23. data/lib/timezone/version.rb +2 -1
  24. data/lib/timezone/zone.rb +230 -99
  25. data/lib/timezone.rb +75 -1
  26. data/test/basic_lookup_test.rb +2 -2
  27. data/test/geonames_lookup_test.rb +13 -6
  28. data/test/google_lookup_test.rb +34 -24
  29. data/test/http_test_client.rb +7 -6
  30. data/test/test_lookup_test.rb +3 -1
  31. data/test/test_timezone.rb +59 -0
  32. data/test/timezone/lookup/test_geonames.rb +59 -0
  33. data/test/timezone/lookup/test_google.rb +94 -0
  34. data/test/timezone/lookup/test_test.rb +24 -0
  35. data/test/timezone/test_deprecate.rb +20 -0
  36. data/test/timezone/test_loader.rb +32 -0
  37. data/test/timezone/test_lookup.rb +53 -0
  38. data/test/timezone/test_nil_zone.rb +26 -0
  39. data/test/timezone/test_zone.rb +49 -0
  40. data/test/timezone_test.rb +64 -63
  41. data/timezone.gemspec +16 -15
  42. metadata +39 -38
  43. data/.rubocop_todo.yml +0 -235
@@ -6,10 +6,7 @@ require_relative 'http_test_client'
6
6
 
7
7
  class GoogleLookupTest < ::Minitest::Unit::TestCase
8
8
  def setup
9
- Timezone::Configure.begin do |c|
10
- c.http_client = HTTPTestClient
11
- c.google_api_key = 'MTIzYWJj'
12
- end
9
+ config { |c| c.google_api_key = 'MTIzYWJj' }
13
10
  end
14
11
 
15
12
  def coordinates
@@ -17,50 +14,63 @@ class GoogleLookupTest < ::Minitest::Unit::TestCase
17
14
  end
18
15
 
19
16
  def lookup
20
- ::Timezone::Lookup::Google.new(Timezone::Configure)
17
+ ::Timezone::Configure.lookup
21
18
  end
22
19
 
23
- def test_missing_api_key
24
- Timezone::Configure.begin{ |c| c.google_api_key = nil }
25
- assert_raises(::Timezone::Error::InvalidConfig){ lookup }
26
- ensure
27
- Timezone::Configure.begin{ |c| c.google_api_key = 'MTIzYWJj' }
20
+ def config
21
+ Timezone::Configure.instance_variable_set(:@lookup, nil)
22
+ Timezone::Configure.instance_variable_set(:@geonames_lookup, nil)
23
+ Timezone::Configure.instance_variable_set(:@google_lookup, nil)
24
+
25
+ Timezone::Configure.begin do |c|
26
+ c.http_client = HTTPTestClient
27
+ yield(c)
28
+ end
28
29
  end
29
30
 
30
31
  def test_google_using_lat_lon_coordinates
31
- HTTPTestClient.body = File.open(mock_path + '/google_lat_lon_coords.txt').read
32
+ lookup.client.body =
33
+ File.open(mock_path + '/google_lat_lon_coords.txt').read
32
34
 
33
35
  assert_equal 'Australia/Adelaide', lookup.lookup(*coordinates)
34
36
  end
35
37
 
36
38
  def test_google_request_denied_read_lat_lon_coordinates
37
- HTTPTestClient.body = nil
38
- assert_raises Timezone::Error::Google, 'The provided API key is invalid.' do
39
+ lookup.client.body = nil
40
+ assert_raises Timezone::Error::Google do
39
41
  lookup.lookup(*coordinates)
40
42
  end
41
43
  end
42
44
 
43
45
  def test_url_non_enterprise
44
- Timecop.freeze(Time.at(1433347661)) do
46
+ Timecop.freeze(Time.at(1_433_347_661)) do
45
47
  result = lookup.send(:url, '123', '123')
46
- assert_equal "/maps/api/timezone/json?location=123%2C123&timestamp=1433347661&key=MTIzYWJj", result
48
+ params = {
49
+ 'location' => '123%2C123',
50
+ 'timestamp' => '1433347661',
51
+ 'key' => 'MTIzYWJj'
52
+ }.map { |k, v| "#{k}=#{v}" }
53
+
54
+ assert_equal "/maps/api/timezone/json?#{params.join('&')}", result
47
55
  end
48
56
  end
49
57
 
50
58
  def test_url_enterprise
51
- Timezone::Configure.begin do |c|
52
- c.google_client_id = '123&asdf'
53
- end
59
+ config { |c| c.google_client_id = '123&asdf' }
54
60
 
55
- Timecop.freeze(Time.at(1433347661)) do
61
+ Timecop.freeze(Time.at(1_433_347_661)) do
56
62
  result = lookup.send(:url, '123', '123')
57
- assert_equal '/maps/api/timezone/json?location=123%2C123&timestamp=1433347661&client=123%26asdf&signature=B1TNSSvIw9Wvf_ZjjW5uRzGm4F4=', result
58
- end
63
+ params = {
64
+ 'location' => '123%2C123',
65
+ 'timestamp' => '1433347661',
66
+ 'client' => '123%26asdf',
67
+ 'signature' => 'B1TNSSvIw9Wvf_ZjjW5uRzGm4F4='
68
+ }.map { |k, v| "#{k}=#{v}" }
59
69
 
60
- ensure
61
- Timezone::Configure.begin do |c|
62
- c.google_client_id = nil
70
+ assert_equal "/maps/api/timezone/json?#{params.join('&')}", result
63
71
  end
72
+ ensure
73
+ config { |c| c.google_client_id = nil }
64
74
  end
65
75
 
66
76
  private
@@ -1,14 +1,15 @@
1
1
  class HTTPTestClient
2
- class << self ; attr_accessor :body ; end
3
-
4
2
  Response = Struct.new(:body) do
5
- def code ; '200' ; end
3
+ def code; '200'; end
6
4
  end
7
5
 
8
- def initialize(protocol, host)
6
+ attr_accessor :body
7
+
8
+ # TODO: Modify once on 1.0.0
9
+ def initialize(_protocol, _url = nil)
9
10
  end
10
11
 
11
- def get(url)
12
- HTTPTestClient::Response.new(self.class.body)
12
+ def get(_url)
13
+ HTTPTestClient::Response.new(body)
13
14
  end
14
15
  end
@@ -34,6 +34,8 @@ class TestLookupTest < ::Minitest::Unit::TestCase
34
34
  end
35
35
 
36
36
  def teardown
37
- Timezone::Configure.begin{ |c| c.lookup = nil }
37
+ Timezone::Configure.instance_variable_set(:@lookup, nil)
38
+ Timezone::Configure.instance_variable_set(:@google_lookup, nil)
39
+ Timezone::Configure.instance_variable_set(:@geonames_lookup, nil)
38
40
  end
39
41
  end
@@ -0,0 +1,59 @@
1
+ require 'timezone'
2
+ require 'minitest/autorun'
3
+
4
+ class TestTimezone < ::Minitest::Test
5
+ parallelize_me!
6
+
7
+ def test_names
8
+ assert Timezone.names.is_a?(Array)
9
+ refute Timezone.names.empty?
10
+ assert Timezone.names.include?('Australia/Sydney')
11
+ assert Timezone.names.include?('America/Los_Angeles')
12
+ end
13
+
14
+ def test_get
15
+ assert Timezone['Australia/Sydney'].valid?
16
+ refute Timezone['foo/bar'].valid?
17
+ end
18
+
19
+ def test_fetch
20
+ assert Timezone.fetch('Australia/Sydney').valid?
21
+ assert_equal 'foo', Timezone.fetch('foo/bar') { 'foo' }
22
+ assert_raises Timezone::Error::InvalidZone do
23
+ Timezone.fetch('foo/bar')
24
+ end
25
+ assert_equal 'foo', Timezone.fetch('foo/bar', 'foo')
26
+
27
+ Timezone.stub(:warn, nil) do
28
+ assert_equal 'b', Timezone.fetch('foo/bar', 'a') { 'b' }
29
+ end
30
+ end
31
+
32
+ def test_fetch_warning
33
+ warning = false
34
+
35
+ Timezone.stub(:warn, ->(_) { warning = true }) do
36
+ Timezone.fetch('foo/bar', 'a') { 'b' }
37
+ end
38
+
39
+ assert warning, 'warning was not issued'
40
+ end
41
+
42
+ def test_lookup
43
+ Timezone::Lookup.config(:test)
44
+
45
+ Timezone::Lookup.lookup.stub(-10, 10, 'America/Los_Angeles')
46
+ Timezone::Lookup.lookup.stub(-20, 20, 'foos')
47
+
48
+ assert_equal Timezone['America/Los_Angeles'], Timezone.lookup(-10, 10)
49
+ assert_equal 'foo', Timezone.lookup(-20, 20) { 'foo' }
50
+ assert_raises Timezone::Error::InvalidZone do
51
+ Timezone.lookup(-20, 20)
52
+ end
53
+ assert_equal 'foo', Timezone.lookup(-20, 20, 'foo')
54
+
55
+ Timezone.stub(:warn, nil) do
56
+ assert_equal 'b', Timezone.lookup(-20, 20, 'a') { 'b' }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,59 @@
1
+ require 'timezone/lookup/geonames'
2
+ require 'minitest/autorun'
3
+ require_relative '../../http_test_client'
4
+
5
+ class TestGeonames < ::Minitest::Test
6
+ parallelize_me!
7
+
8
+ def coordinates
9
+ [-34.92771808058, 138.477041423321]
10
+ end
11
+
12
+ def lookup
13
+ config = OpenStruct.new
14
+ config.username = 'timezone'
15
+ config.request_handler = HTTPTestClient
16
+
17
+ Timezone::Lookup::Geonames.new(config)
18
+ end
19
+
20
+ def test_default_config
21
+ assert_equal 'http', lookup.config.protocol
22
+ assert_equal 'api.geonames.org', lookup.config.url
23
+ end
24
+
25
+ def test_missing_username
26
+ assert_raises(::Timezone::Error::InvalidConfig) do
27
+ Timezone::Lookup::Geonames.new(OpenStruct.new)
28
+ end
29
+ end
30
+
31
+ def test_lookup
32
+ mine = lookup
33
+ mine.client.body = File.open(mock_path + '/lat_lon_coords.txt').read
34
+
35
+ assert_equal 'Australia/Adelaide', mine.lookup(*coordinates)
36
+ end
37
+
38
+ def test_api_limit
39
+ mine = lookup
40
+ mine.client.body = File.open(mock_path + '/api_limit_reached.txt').read
41
+
42
+ assert_raises Timezone::Error::GeoNames, 'api limit reached' do
43
+ mine.lookup(*coordinates)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def mock_path
50
+ File.expand_path(
51
+ File.join(
52
+ File.dirname(__FILE__),
53
+ '..',
54
+ '..',
55
+ 'mocks'
56
+ )
57
+ )
58
+ end
59
+ end
@@ -0,0 +1,94 @@
1
+ require 'timezone/lookup/google'
2
+ require 'minitest/autorun'
3
+ require 'timecop'
4
+ require_relative '../../http_test_client'
5
+
6
+ class TestGoogle < ::Minitest::Test
7
+ parallelize_me!
8
+
9
+ def coordinates
10
+ [-34.92771808058, 138.477041423321]
11
+ end
12
+
13
+ def config
14
+ config = OpenStruct.new
15
+ config.request_handler = HTTPTestClient
16
+ config.api_key = 'MTIzYWJj'
17
+ config
18
+ end
19
+
20
+ def lookup
21
+ Timezone::Lookup::Google.new(config)
22
+ end
23
+
24
+ def test_default_config
25
+ assert_equal 'https', lookup.config.protocol
26
+ assert_equal 'maps.googleapis.com', lookup.config.url
27
+ end
28
+
29
+ def test_missing_api_key
30
+ assert_raises(::Timezone::Error::InvalidConfig) do
31
+ Timezone::Lookup::Google.new(OpenStruct.new)
32
+ end
33
+ end
34
+
35
+ def test_google_using_lat_long_coordinates
36
+ mine = lookup
37
+ mine.client.body = File.open(mock_path + '/google_lat_lon_coords.txt').read
38
+
39
+ assert_equal 'Australia/Adelaide', mine.lookup(*coordinates)
40
+ end
41
+
42
+ def test_google_request_denied_read_lat_long_coordinates
43
+ mine = lookup
44
+ mine.client.body = nil
45
+ assert_raises Timezone::Error::Google, 'The provided API key is invalid.' do
46
+ mine.lookup(*coordinates)
47
+ end
48
+ end
49
+
50
+ def test_url_non_enterprise
51
+ Timecop.freeze(Time.at(1_433_347_661)) do
52
+ result = lookup.send(:url, '123', '123')
53
+ params = {
54
+ 'location' => '123%2C123',
55
+ 'timestamp' => '1433347661',
56
+ 'key' => 'MTIzYWJj'
57
+ }.map { |k, v| "#{k}=#{v}" }
58
+
59
+ assert_equal "/maps/api/timezone/json?#{params.join('&')}", result
60
+ end
61
+ end
62
+
63
+ def test_url_enterprise
64
+ econfig = config
65
+ econfig.client_id = '123&asdf'
66
+
67
+ enterprise = Timezone::Lookup::Google.new(econfig)
68
+
69
+ Timecop.freeze(Time.at(1_433_347_661)) do
70
+ result = enterprise.send(:url, '123', '123')
71
+ params = {
72
+ 'location' => '123%2C123',
73
+ 'timestamp' => '1433347661',
74
+ 'client' => '123%26asdf',
75
+ 'signature' => 'B1TNSSvIw9Wvf_ZjjW5uRzGm4F4='
76
+ }.map { |k, v| "#{k}=#{v}" }
77
+
78
+ assert_equal "/maps/api/timezone/json?#{params.join('&')}", result
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ def mock_path
85
+ File.expand_path(
86
+ File.join(
87
+ File.dirname(__FILE__),
88
+ '..',
89
+ '..',
90
+ 'mocks'
91
+ )
92
+ )
93
+ end
94
+ end
@@ -0,0 +1,24 @@
1
+ require 'timezone/lookup/test'
2
+ require 'timezone'
3
+ require 'minitest/autorun'
4
+
5
+ class TestTest < ::Minitest::Test
6
+ parallelize_me!
7
+
8
+ def lookup
9
+ Timezone::Lookup::Test.new(OpenStruct.new)
10
+ end
11
+
12
+ def test_simple_stub
13
+ mine = lookup
14
+ mine.stub(-10, 10, 'America/Los_Angeles')
15
+
16
+ assert_equal 'America/Los_Angeles', mine.lookup(-10, 10)
17
+ end
18
+
19
+ def test_missing_stub
20
+ assert_raises(::Timezone::Error::Test) do
21
+ lookup.lookup(100, 100)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ require 'timezone/deprecate'
2
+ require 'minitest/autorun'
3
+
4
+ class TestDeprecate < ::Minitest::Test
5
+ # This test should not be parallelized because it tests the result
6
+ # of a single class-level attribute.
7
+ def test_callback
8
+ values = []
9
+
10
+ Timezone::Deprecate.callback = lambda do |klass, method, message|
11
+ values = [klass, method, message]
12
+ end
13
+
14
+ Timezone::Deprecate.call(self, :test_message, 'foo')
15
+
16
+ assert_equal [self, :test_message, 'foo'], values
17
+ ensure
18
+ Timezone::Deprecate.callback = nil
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require 'timezone/loader'
2
+ require 'minitest/autorun'
3
+
4
+ module Timezone
5
+ class TestLoader < ::Minitest::Test
6
+ parallelize_me!
7
+
8
+ def test_load
9
+ assert_equal(
10
+ [[1_435_708_800, 'GMT', false, 0]],
11
+ Loader.load('GMT')
12
+ )
13
+
14
+ assert_raises ::Timezone::Error::InvalidZone do
15
+ Loader.load('foo/bar')
16
+ end
17
+ end
18
+
19
+ def test_names
20
+ assert Loader.names.include?('GMT')
21
+ assert Loader.names.include?('Europe/Paris')
22
+ refute Loader.names.include?('foo/bar')
23
+ end
24
+
25
+ def test_valid?
26
+ assert Loader.valid?('America/Los_Angeles')
27
+ assert Loader.valid?('Europe/Paris')
28
+ refute Loader.valid?('foo/bar')
29
+ refute Loader.valid?(nil)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,53 @@
1
+ require 'timezone/lookup'
2
+ require 'minitest/autorun'
3
+
4
+ class TestLookup < ::Minitest::Test
5
+ def test_test_config
6
+ Timezone::Lookup.config(:test)
7
+
8
+ assert_equal Timezone::Lookup::Test,
9
+ Timezone::Lookup.lookup.class
10
+ end
11
+
12
+ def test_geonames_config
13
+ Timezone::Lookup.config(:geonames) do |c|
14
+ c.username = 'foo'
15
+ end
16
+
17
+ assert_equal Timezone::Lookup::Geonames,
18
+ Timezone::Lookup.lookup.class
19
+
20
+ assert_equal Timezone::NetHTTPClient,
21
+ Timezone::Lookup.lookup.config.request_handler
22
+ end
23
+
24
+ def test_google_config
25
+ Timezone::Lookup.config(:google) do |c|
26
+ c.api_key = 'foo'
27
+ end
28
+
29
+ assert_equal Timezone::Lookup::Google,
30
+ Timezone::Lookup.lookup.class
31
+
32
+ assert_equal Timezone::NetHTTPClient,
33
+ Timezone::Lookup.lookup.config.request_handler
34
+ end
35
+
36
+ def test_custom_config
37
+ custom_lookup = Class.new do
38
+ def initialize(config); end
39
+ end
40
+
41
+ Timezone::Lookup.config(custom_lookup)
42
+
43
+ assert_equal custom_lookup, Timezone::Lookup.lookup.class
44
+ end
45
+
46
+ def test_missing_config
47
+ Timezone::Lookup.instance_variable_set(:@lookup, nil)
48
+
49
+ assert_raises Timezone::Error::InvalidConfig do
50
+ Timezone::Lookup.lookup
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,26 @@
1
+ require 'timezone/nil_zone'
2
+ require 'minitest/autorun'
3
+
4
+ class TestNilZone < ::Minitest::Test
5
+ parallelize_me!
6
+
7
+ def setup
8
+ @zone = Timezone::NilZone.new
9
+ end
10
+
11
+ def test_name
12
+ assert_equal nil, @zone.name
13
+ end
14
+
15
+ def test_to_s
16
+ assert_equal 'NilZone', @zone.to_s
17
+ end
18
+
19
+ def test_inspect
20
+ assert '#<Timezone::NilZone>', @zone.inspect
21
+ end
22
+
23
+ def test_valid?
24
+ refute @zone.valid?
25
+ end
26
+ end
@@ -0,0 +1,49 @@
1
+ require 'timezone/zone'
2
+ require 'minitest/autorun'
3
+
4
+ class TestZone < ::Minitest::Test
5
+ parallelize_me!
6
+
7
+ def la
8
+ @la ||= Timezone::Zone.new('America/Los_Angeles')
9
+ end
10
+
11
+ def paris
12
+ @paris ||= Timezone::Zone.new('Europe/Paris')
13
+ end
14
+
15
+ def test_name
16
+ assert_equal 'America/Los_Angeles', la.name
17
+ assert_equal 'Europe/Paris', paris.name
18
+ end
19
+
20
+ def test_valid?
21
+ assert la.valid?
22
+ assert paris.valid?
23
+ end
24
+
25
+ def test_to_s
26
+ assert_equal 'America/Los_Angeles', la.to_s
27
+ assert_equal 'Europe/Paris', paris.to_s
28
+ end
29
+
30
+ def test_inspect
31
+ assert_equal(
32
+ '#<Timezone::Zone name: "America/Los_Angeles">',
33
+ la.inspect
34
+ )
35
+
36
+ assert_equal(
37
+ '#<Timezone::Zone name: "Europe/Paris">',
38
+ paris.inspect
39
+ )
40
+ end
41
+
42
+ def test_comparable
43
+ assert paris > la
44
+ assert la < paris
45
+ assert la == Timezone::Zone.new('America/Los_Angeles')
46
+
47
+ assert_equal nil, paris <=> 8
48
+ end
49
+ end