allscripts_unity_client 2.1.6 → 2.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f780daefb7aced7b5f887ff5cbe0338d3fbbc15
4
- data.tar.gz: 3f1ce4fe75714faa52fbfa768b52185dbbd176fb
3
+ metadata.gz: 259866bfe6fed08cf45b403f0b6d84c7a3149413
4
+ data.tar.gz: bcdf989b15bb39140e4b724904272f4d23a34125
5
5
  SHA512:
6
- metadata.gz: a0d36d96dae75e62fe1e4505af37f6255cd3cece14a91c3eea9209c200c95839548c297dc936ef0b2b2936261ab7a5670c696aef05bb7bcad5082627328d6557
7
- data.tar.gz: 28729d3cef4439338ad5a83b9887416de96f50eaaa92d2d8da24d3332222a3fd9b103457726eb373a643055777db243d32fa765cb4ff742e3aa969c3f0a7698a
6
+ metadata.gz: 323d5275934799ce108a744bca885a3dbcccd1a523e1a5eb5a83d5a07e7bf3fc868d8203748a39f40d1d7b30a41e93e7643f1b2f1324cfcb9d310d8c8f05bbc5
7
+ data.tar.gz: 6d845cd670527219ee24476349cd2581e886969d4fd8d41a636ac39d1a5879b1d8667024ba3e55ee29756598e4e22b01e547d89784f12f19d8a55b0b739d4fe9
@@ -25,7 +25,7 @@ Gem::Specification.new do |gem|
25
25
  gem.add_runtime_dependency 'savon', '~> 2.6.0'
26
26
  gem.add_runtime_dependency 'faraday', '~> 0.9.0'
27
27
  gem.add_runtime_dependency 'em-http-request', '~> 1.1.2'
28
- gem.add_runtime_dependency 'tzinfo', '>= 0'
28
+ gem.add_runtime_dependency 'activesupport', '>= 0'
29
29
  gem.add_runtime_dependency 'nokogiri', '< 1.6', '>= 1.4.0'
30
30
  gem.add_runtime_dependency 'nori', '~> 2.4.0'
31
31
  gem.add_runtime_dependency 'american_date', '~> 1.1.0'
@@ -1,5 +1,5 @@
1
+ require 'active_support/time'
1
2
  require 'allscripts_unity_client/utilities'
2
- require 'allscripts_unity_client/timezone'
3
3
  require 'allscripts_unity_client/unity_request'
4
4
  require 'allscripts_unity_client/json_unity_request'
5
5
  require 'allscripts_unity_client/unity_response'
@@ -26,8 +26,7 @@ module AllscriptsUnityClient
26
26
  client_driver = SOAPClientDriver.new(options)
27
27
  end
28
28
 
29
- client = Client.new(client_driver)
30
- client
29
+ Client.new(client_driver)
31
30
  end
32
31
 
33
32
  private
@@ -35,5 +34,4 @@ module AllscriptsUnityClient
35
34
  def self.raise_if_options_invalid(options)
36
35
  raise ArgumentError, ':mode must be :json or :soap' unless [:json, :soap].include?(options[:mode])
37
36
  end
38
- end
39
-
37
+ end
@@ -54,9 +54,9 @@ module AllscriptsUnityClient
54
54
 
55
55
  def timezone=(timezone)
56
56
  if !timezone.nil?
57
- @timezone = Timezone.new(timezone)
57
+ @timezone = ActiveSupport::TimeZone[timezone]
58
58
  else
59
- @timezone = Timezone.new('UTC')
59
+ @timezone = ActiveSupport::TimeZone['Etc/UTC']
60
60
  end
61
61
  end
62
62
 
@@ -47,17 +47,19 @@ module AllscriptsUnityClient
47
47
  protected
48
48
 
49
49
  def process_date(value)
50
- if value.nil?
51
- return nil
50
+ if value && (value.is_a?(Time) || value.is_a?(DateTime) || value.is_a?(ActiveSupport::TimeWithZone))
51
+ return value.in_time_zone(@timezone).iso8601
52
52
  end
53
53
 
54
- result = Utilities::try_to_encode_as_date(value)
54
+ date = Utilities::try_to_encode_as_date(ActiveSupport::TimeZone['Etc/UTC'], value)
55
55
 
56
- if result.instance_of?(Time) || result.instance_of?(Date) || result.instance_of?(DateTime)
57
- return @timezone.utc_to_local(result)
56
+ if date && date.is_a?(ActiveSupport::TimeWithZone)
57
+ date.in_time_zone(@timezone).iso8601
58
+ elsif date && date.is_a?(Date)
59
+ date.iso8601
60
+ else
61
+ value
58
62
  end
59
-
60
- value
61
63
  end
62
64
  end
63
65
  end
@@ -96,11 +96,11 @@ module AllscriptsUnityClient
96
96
  end
97
97
 
98
98
  # Attempt to parse a Date or DateTime from a string
99
- response = Utilities::try_to_encode_as_date(response)
99
+ response = Utilities::try_to_encode_as_date(@timezone, response)
100
100
 
101
101
  # Base case: convert date object to UTC
102
- if response.instance_of?(Time) || response.instance_of?(DateTime) || response.instance_of?(Date)
103
- return @timezone.local_to_utc(response)
102
+ if response.instance_of?(ActiveSupport::TimeWithZone)
103
+ return response.utc
104
104
  end
105
105
 
106
106
  # Base case: value is not a date
@@ -7,7 +7,7 @@ module AllscriptsUnityClient
7
7
  DATETIME_REGEX = /^((\d{1,2}[-\/]\d{1,2}[-\/]\d{4})|(\d{4}[-\/]\d{1,2}[-\/]\d{1,2})|(\d{1,2}-[A-Za-z]{3,4}-\d{4})|([A-Za-z]{3,4} +\d{1,2} \d{2,4}))(T| +)(\d{1,2}:\d{2}(:\d{2})?(\.\d+)? ?(PM|AM|pm|am)?((-|\+)\d{2}:?\d{2})?Z?)$/
8
8
  DATE_REGEX = /^((\d{1,2}[-\/]\d{1,2}[-\/]\d{4})|(\d{4}[-\/]\d{1,2}[-\/]\d{1,2})|(\d{1,2}-[A-Za-z]{3,4}-\d{4})|([A-Za-z]{3,4} +\d{1,2} \d{2,4}))$/
9
9
 
10
- def self.try_to_encode_as_date(possible_date)
10
+ def self.try_to_encode_as_date(timezone, possible_date)
11
11
  if possible_date.nil?
12
12
  return nil
13
13
  end
@@ -17,7 +17,7 @@ module AllscriptsUnityClient
17
17
  end
18
18
 
19
19
  if possible_date.is_a?(String) && possible_date =~ DATETIME_REGEX
20
- return DateTime.parse(possible_date)
20
+ return timezone.parse(possible_date)
21
21
  end
22
22
 
23
23
  possible_date
@@ -1,3 +1,3 @@
1
1
  module AllscriptsUnityClient
2
- VERSION = '2.1.6'
2
+ VERSION = '2.2.0'
3
3
  end
@@ -5,9 +5,9 @@ describe AllscriptsUnityClient::ClientOptions do
5
5
 
6
6
  let(:url_with_slash) { 'http://www.example.com/' }
7
7
  let(:url_without_slash) { 'http://www.example.com' }
8
- let(:utc_timezone) { AllscriptsUnityClient::Timezone.new('UTC') }
8
+ let(:utc_timezone) { ActiveSupport::TimeZone['Etc/UTC'] }
9
9
  let(:america_los_angeles) { 'America/Los_Angeles' }
10
- let(:america_los_angeles_timezone) { AllscriptsUnityClient::Timezone.new('America/Los_Angeles') }
10
+ let(:america_los_angeles_timezone) { ActiveSupport::TimeZone['America/Los_Angeles'] }
11
11
  let(:client_options_hash) { { base_unity_url: 'http://www.example.com', username: 'username', password: 'password', appname: 'appname', proxy: 'proxy', timezone: 'UTC', logger: nil } }
12
12
 
13
13
  describe '#validate_options' do
@@ -3,10 +3,9 @@ FactoryGirl.define do
3
3
  initialize_with { new(parameters, timezone, appname, security_token) }
4
4
 
5
5
  parameters { build(:magic_request) }
6
- timezone { build(:timezone) }
6
+ timezone ActiveSupport::TimeZone['Etc/UTC']
7
7
  appname Faker::Name.name
8
8
  security_token SecureRandom.uuid
9
-
10
9
  factory :json_unity_request, class: AllscriptsUnityClient::JSONUnityRequest
11
10
  end
12
11
  end
@@ -3,7 +3,7 @@ FactoryGirl.define do
3
3
  initialize_with { new(response, timezone) }
4
4
 
5
5
  response Hash.new
6
- timezone { build(:timezone) }
6
+ timezone ActiveSupport::TimeZone['Etc/UTC']
7
7
 
8
8
  factory :json_unity_response, class: AllscriptsUnityClient::JSONUnityResponse
9
9
  end
@@ -54,7 +54,7 @@ describe AllscriptsUnityClient::JSONClientDriver do
54
54
 
55
55
  it 'should serialize DateTime to iso8601 when given' do
56
56
  subject.magic(parameter1: DateTime.now)
57
- expect(WebMock).to have_requested(:post, 'http://www.example.com/Unity/UnityService.svc/json/MagicJson').with(body: /\{"Action":(null|"[^"]*"),"AppUserID":(null|"[^"]*"),"Appname":(null|"[^"]*"),"PatientID":(null|"[^"]*"),"Token":(null|"[^"]*"),"Parameter1":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(-|\+)\d{2}:\d{2}","Parameter2":(null|"[^"]*"),"Parameter3":(null|"[^"]*"),"Parameter4":(null|"[^"]*"),"Parameter5":(null|"[^"]*"),"Parameter6":(null|"[^"]*"),"Data":(null|"[^"]*")\}/, headers: { 'Content-Type' => 'application/json' })
57
+ expect(WebMock).to have_requested(:post, 'http://www.example.com/Unity/UnityService.svc/json/MagicJson').with(body: /\{"Action":(null|"[^"]*"),"AppUserID":(null|"[^"]*"),"Appname":(null|"[^"]*"),"PatientID":(null|"[^"]*"),"Token":(null|"[^"]*"),"Parameter1":"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(-|\+)\d{2}:\d{2}","Parameter2":(null|"[^"]*"),"Parameter3":(null|"[^"]*"),"Parameter4":(null|"[^"]*"),"Parameter5":(null|"[^"]*"),"Parameter6":(null|"[^"]*"),"Data":(null|"[^"]*")\}/, headers: { 'Content-Type' => 'application/json' })
58
58
  end
59
59
 
60
60
  it 'should call start_timer' do
@@ -73,22 +73,41 @@ shared_examples 'a unity request' do
73
73
  end
74
74
 
75
75
  context 'when given a date string' do
76
- it do
77
- date = '2013-10-24'
78
- expect(subject.send(:process_date, date)).to be_instance_of(Date)
76
+ it 'returns an ISO8601 string' do
77
+ date = '10/24/2013'
78
+ expect(subject.send(:process_date, date)).to eq('2013-10-24')
79
+ end
80
+ end
81
+
82
+ context 'when given a UTC date time string' do
83
+ it 'returns an ISO8601 string' do
84
+ now_iso8601 = DateTime.now.utc.iso8601
85
+ expect(subject.send(:process_date, now_iso8601)).to eq(now_iso8601)
79
86
  end
80
87
  end
81
88
 
82
89
  context 'when given a Date' do
83
- it { expect(subject.send(:process_date, Date.today)).to be_instance_of(Date) }
90
+ it 'returns an ISO8601 string' do
91
+ today = Date.today
92
+ today_iso8601 = Date.today.iso8601
93
+ expect(subject.send(:process_date, today)).to eq(today_iso8601)
94
+ end
84
95
  end
85
96
 
86
- context 'when given a Time' do
87
- it { expect(subject.send(:process_date, Time.now)).to be_instance_of(Time) }
97
+ context 'when given a UTC Time' do
98
+ it 'returns an ISO8601 string' do
99
+ now = Time.now.utc
100
+ now_iso8601 = now.strftime('%Y-%m-%dT%H:%M:%S+00:00')
101
+ expect(subject.send(:process_date, now)).to eq(now_iso8601)
102
+ end
88
103
  end
89
104
 
90
- context 'when given a DateTime' do
91
- it { expect(subject.send(:process_date, DateTime.now)).to be_instance_of(DateTime) }
105
+ context 'when given a UTC DateTime' do
106
+ it 'returns an ISO8601 string' do
107
+ now = DateTime.now.utc
108
+ now_iso8601 = now.iso8601
109
+ expect(subject.send(:process_date, now)).to eq(now_iso8601)
110
+ end
92
111
  end
93
112
  end
94
113
  end
@@ -6,7 +6,8 @@ describe AllscriptsUnityClient::Utilities do
6
6
  let(:date_string) { '2013-02-15' }
7
7
  let(:date) { Date.parse(date_string) }
8
8
  let(:datetime_string) { '2013-02-15T00:00:00Z' }
9
- let(:datetime) { DateTime.parse(datetime_string) }
9
+ let(:datetime) { timezone.parse(datetime_string) }
10
+ let(:timezone) { ActiveSupport::TimeZone['Etc/UTC'] }
10
11
 
11
12
  let(:string) { 'string' }
12
13
  let(:string_array) { ['string'] }
@@ -19,10 +20,10 @@ describe AllscriptsUnityClient::Utilities do
19
20
  let(:datetime_string_two) { 'Feb 28 2013 1:34PM' }
20
21
  let(:datetime_string_three) { '12/25/2013 12:37 PM' }
21
22
  let(:datetime_string_four) { 'Nov 1 2011 11:31AM' }
22
- let(:datetime_one) { DateTime.parse(datetime_string_one) }
23
- let(:datetime_two) { DateTime.parse(datetime_string_two) }
24
- let(:datetime_three) { DateTime.parse(datetime_string_three) }
25
- let(:datetime_four) { DateTime.parse(datetime_string_four) }
23
+ let(:datetime_one) { timezone.parse(datetime_string_one) }
24
+ let(:datetime_two) { timezone.parse(datetime_string_two) }
25
+ let(:datetime_three) { timezone.parse(datetime_string_three) }
26
+ let(:datetime_four) { timezone.parse(datetime_string_four) }
26
27
  let(:date_string_one) { '20-Jul-2014' }
27
28
  let(:date_string_two) { '12/25/2013' }
28
29
  let(:date_string_three) { 'Nov 1 2011' }
@@ -32,66 +33,66 @@ describe AllscriptsUnityClient::Utilities do
32
33
 
33
34
  describe '.try_to_encode_as_date' do
34
35
  context 'when given nil' do
35
- it { expect(subject.try_to_encode_as_date(nil)).to be_nil }
36
+ it { expect(subject.try_to_encode_as_date(timezone, nil)).to be_nil }
36
37
  end
37
38
 
38
39
  context 'when given date string' do
39
40
  it 'returns the string as a Date' do
40
- expect(subject.try_to_encode_as_date(date_string)).to eq(date)
41
+ expect(subject.try_to_encode_as_date(timezone, date_string)).to eq(date)
41
42
  end
42
43
  end
43
44
 
44
45
  context 'when given date time string' do
45
46
  it 'returns the string as a DateTime' do
46
- expect(subject.try_to_encode_as_date(datetime_string)).to eq(datetime)
47
+ expect(subject.try_to_encode_as_date(timezone, datetime_string)).to eq(datetime)
47
48
  end
48
49
  end
49
50
 
50
51
  context 'when given datetime_string_one' do
51
52
  it 'returns the string as a DateTime' do
52
- expect(subject.try_to_encode_as_date(datetime_string_one)).to eq(datetime_one)
53
+ expect(subject.try_to_encode_as_date(timezone, datetime_string_one)).to eq(datetime_one)
53
54
  end
54
55
  end
55
56
 
56
57
  context 'when given datetime_string_two' do
57
58
  it 'returns the string as a DateTime' do
58
- expect(subject.try_to_encode_as_date(datetime_string_two)).to eq(datetime_two)
59
+ expect(subject.try_to_encode_as_date(timezone, datetime_string_two)).to eq(datetime_two)
59
60
  end
60
61
  end
61
62
 
62
63
  context 'when given datetime_string_three' do
63
64
  it 'returns the string as a DateTime' do
64
- expect(subject.try_to_encode_as_date(datetime_string_three)).to eq(datetime_three)
65
+ expect(subject.try_to_encode_as_date(timezone, datetime_string_three)).to eq(datetime_three)
65
66
  end
66
67
  end
67
68
 
68
69
  context 'when given datetime_string_four' do
69
70
  it 'returns the string as a DateTime' do
70
- expect(subject.try_to_encode_as_date(datetime_string_four)).to eq(datetime_four)
71
+ expect(subject.try_to_encode_as_date(timezone, datetime_string_four)).to eq(datetime_four)
71
72
  end
72
73
  end
73
74
 
74
75
  context 'when given date_string_one' do
75
76
  it 'returns the string as a Date' do
76
- expect(subject.try_to_encode_as_date(date_string_one)).to eq(date_one)
77
+ expect(subject.try_to_encode_as_date(timezone, date_string_one)).to eq(date_one)
77
78
  end
78
79
  end
79
80
 
80
81
  context 'when given date_string_two' do
81
82
  it 'returns the string as a Date' do
82
- expect(subject.try_to_encode_as_date(date_string_two)).to eq(date_two)
83
+ expect(subject.try_to_encode_as_date(timezone, date_string_two)).to eq(date_two)
83
84
  end
84
85
  end
85
86
 
86
87
  context 'when given date_string_three' do
87
88
  it 'returns the string as a Date' do
88
- expect(subject.try_to_encode_as_date(date_string_three)).to eq(date_three)
89
+ expect(subject.try_to_encode_as_date(timezone, date_string_three)).to eq(date_three)
89
90
  end
90
91
  end
91
92
 
92
93
  context 'when given a non-date string' do
93
94
  it 'returns that string' do
94
- expect(subject.try_to_encode_as_date(string)).to eq(string)
95
+ expect(subject.try_to_encode_as_date(timezone, string)).to eq(string)
95
96
  end
96
97
  end
97
98
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: allscripts_unity_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.6
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ash Gupta
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-25 00:00:00.000000000 Z
12
+ date: 2014-09-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: savon
@@ -54,7 +54,7 @@ dependencies:
54
54
  - !ruby/object:Gem::Version
55
55
  version: 1.1.2
56
56
  - !ruby/object:Gem::Dependency
57
- name: tzinfo
57
+ name: activesupport
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
@@ -267,7 +267,6 @@ files:
267
267
  - lib/allscripts_unity_client/json_unity_response.rb
268
268
  - lib/allscripts_unity_client/new_relic_support.rb
269
269
  - lib/allscripts_unity_client/soap_client_driver.rb
270
- - lib/allscripts_unity_client/timezone.rb
271
270
  - lib/allscripts_unity_client/unity_request.rb
272
271
  - lib/allscripts_unity_client/unity_response.rb
273
272
  - lib/allscripts_unity_client/utilities.rb
@@ -281,7 +280,6 @@ files:
281
280
  - spec/factories/client_factory.rb
282
281
  - spec/factories/client_options.rb
283
282
  - spec/factories/magic_request_factory.rb
284
- - spec/factories/timezone_factory.rb
285
283
  - spec/factories/unity_request_factory.rb
286
284
  - spec/factories/unity_response_factory.rb
287
285
  - spec/fixtures/attributes_hash.yml
@@ -316,7 +314,6 @@ files:
316
314
  - spec/support/shared_examples_for_client_driver.rb
317
315
  - spec/support/shared_examples_for_unity_request.rb
318
316
  - spec/support/shared_examples_for_unity_response.rb
319
- - spec/timezone_spec.rb
320
317
  - spec/unity_request_spec.rb
321
318
  - spec/unity_response_spec.rb
322
319
  - spec/utilities_spec.rb
@@ -1,104 +0,0 @@
1
- require 'date'
2
- require 'tzinfo'
3
-
4
- module AllscriptsUnityClient
5
- class Timezone
6
- attr_accessor :tzinfo
7
-
8
- def initialize(zone_identifier)
9
- raise ArgumentError, 'zone_identifier can not be nil' if zone_identifier.nil?
10
-
11
- @tzinfo = TZInfo::Timezone.get(zone_identifier)
12
- end
13
-
14
- # Use TZInfo to convert a given UTC datetime into
15
- # a local
16
- def local_to_utc(datetime)
17
- convert_with_timezone(:local_to_utc, datetime)
18
- end
19
-
20
- def utc_to_local(datetime = nil)
21
- convert_with_timezone(:utc_to_local, datetime)
22
- end
23
-
24
- def ==(timezone)
25
- return false if !timezone.is_a?(Timezone)
26
- @tzinfo == timezone.tzinfo
27
- end
28
-
29
- private
30
-
31
- # Direction can be :utc_to_local or :local_to_utc
32
- def convert_with_timezone(direction, datetime = nil)
33
- if datetime.nil?
34
- return nil
35
- end
36
-
37
- # Dates have no time information and so timezone conversion
38
- # doesn't make sense. Just return the date in this case.
39
- if datetime.instance_of?(Date)
40
- return datetime
41
- end
42
-
43
- if datetime.instance_of?(String)
44
- datetime = DateTime.parse(datetime.to_s)
45
- end
46
-
47
- is_datetime = datetime.instance_of?(DateTime)
48
-
49
- if direction == :local_to_utc
50
- if is_datetime
51
- # DateTime can do UTC conversions reliably, so use that instead of
52
- # TZInfo
53
- datetime = datetime.new_offset(0)
54
- else
55
- datetime = @tzinfo.local_to_utc(datetime)
56
- end
57
-
58
- if is_datetime
59
- # Convert to a DateTime with a UTC offset
60
- datetime = DateTime.parse("#{datetime.strftime('%FT%T')}Z")
61
- end
62
- end
63
-
64
- if direction == :utc_to_local
65
- datetime = @tzinfo.utc_to_local(datetime)
66
-
67
- if is_datetime
68
- # Convert to a DateTime with the correct timezone offset
69
- datetime = DateTime.parse(iso8601_with_offset(datetime))
70
- end
71
- end
72
-
73
- datetime
74
- end
75
-
76
- # TZInfo does not correctly update a DateTime's
77
- # offset, so we manually format a ISO8601 with
78
- # the correct format
79
- def iso8601_with_offset(datetime)
80
- if datetime.nil?
81
- return nil
82
- end
83
-
84
- offset = @tzinfo.current_period.utc_offset
85
- negative_offset = false
86
- datetime_string = datetime.strftime('%FT%T')
87
-
88
- if offset < 0
89
- offset *= -1
90
- negative_offset = true
91
- end
92
-
93
- if offset == 0
94
- offset_string = 'Z'
95
- else
96
- offset_string = Time.at(offset).utc.strftime('%H:%M')
97
- offset_string = '-' + offset_string if negative_offset
98
- offset_string = '+' + offset_string unless negative_offset
99
- end
100
-
101
- "#{datetime_string}#{offset_string}"
102
- end
103
- end
104
- end
@@ -1,7 +0,0 @@
1
- FactoryGirl.define do
2
- factory :timezone, class: AllscriptsUnityClient::Timezone do
3
- initialize_with { new(zone_identifier) }
4
-
5
- zone_identifier 'America/Phoenix'
6
- end
7
- end
@@ -1,181 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe AllscriptsUnityClient::Timezone do
4
- subject { build(:timezone) }
5
-
6
- let(:positive_timezone) { build(:timezone, zone_identifier: 'Asia/Jakarta') }
7
- let(:utc_timezone) { build(:timezone, zone_identifier: 'UTC') }
8
- let(:local_string) { '2013-10-19T17:00:00-07:00' }
9
- let(:date) { Date.parse(local_string) }
10
- let(:local_time) { Time.parse('2013-10-19T17:00:00') }
11
- let(:local_datetime) { DateTime.parse(local_string) }
12
- let(:utc_string) { '2013-10-20T00:00:00+00:00' }
13
- let(:utc_time) { Time.parse('2013-10-20T00:00:00') }
14
- let(:utc_datetime) { DateTime.parse(utc_string) }
15
-
16
- def time_to_s(time)
17
- time.strftime('%FT%T')
18
- end
19
-
20
- describe '#initialize' do
21
- context 'when nil is given' do
22
- it 'raises ArgumentError' do
23
- expect { build(:timezone, zone_identifier: nil) }.to raise_error(ArgumentError)
24
- end
25
- end
26
-
27
- context 'when an invalid TZInfo zone is given' do
28
- it 'rasies TZInfo::InvalidTimezoneIdentifier' do
29
- expect { build(:timezone, zone_identifier: '') }.to raise_error(TZInfo::InvalidTimezoneIdentifier)
30
- end
31
- end
32
- end
33
-
34
- describe '#local_to_utc' do
35
- it 'calls #convert_with_timezone with :local_to_utc' do
36
- allow(subject).to receive(:convert_with_timezone).and_return(local_string)
37
- subject.local_to_utc(local_string)
38
- expect(subject).to have_received(:convert_with_timezone).with(:local_to_utc, local_string)
39
- end
40
- end
41
-
42
- describe '#utc_to_local' do
43
- it 'calls #convert_with_timezone with :utc_to_local' do
44
- allow(subject).to receive(:convert_with_timezone).and_return(local_string)
45
- subject.utc_to_local(local_string)
46
- expect(subject).to have_received(:convert_with_timezone).with(:utc_to_local, local_string)
47
- end
48
- end
49
-
50
- describe '#==' do
51
- context 'when given an object other than Timezone' do
52
- it 'returns false' do
53
- expect(subject == 'not a timezone').to be_falsey
54
- end
55
- end
56
-
57
- context 'when given a Timezone that has an equivalent @tzinfo' do
58
- it 'returns true' do
59
- expect(utc_timezone == utc_timezone).to be_truthy
60
- end
61
- end
62
-
63
- context 'when given a Timezone that has a different @tzinfo' do
64
- it 'returns false' do
65
- expect(subject == utc_timezone).to be_falsey
66
- end
67
- end
68
- end
69
-
70
- describe '#convert_with_timezone' do
71
- context 'when given nil' do
72
- it { expect(subject.send(:convert_with_timezone, nil)).to be_nil }
73
- end
74
-
75
- context 'when given :local_to_utc and a Date' do
76
- it 'returns a Date' do
77
- expect(subject.send(:convert_with_timezone, :local_to_utc, date)).to be_an_instance_of(Date)
78
- end
79
- end
80
-
81
- context 'when given :local_to_utc and a Time' do
82
- it 'returns a Date' do
83
- expect(subject.send(:convert_with_timezone, :local_to_utc, local_time)).to be_an_instance_of(Time)
84
- end
85
- end
86
-
87
- context 'when given :local_to_utc and a DateTime' do
88
- it 'returns a DateTime' do
89
- expect(subject.send(:convert_with_timezone, :local_to_utc, local_datetime)).to be_an_instance_of(DateTime)
90
- end
91
- end
92
-
93
- context 'when given :local_to_utc and a String' do
94
- it 'returns a DateTime' do
95
- expect(subject.send(:convert_with_timezone, :local_to_utc, local_string)).to be_an_instance_of(DateTime)
96
- end
97
- end
98
-
99
- context 'when given :utc_to_local and a Date' do
100
- it 'returns a Date' do
101
- expect(subject.send(:convert_with_timezone, :utc_to_local, date)).to be_an_instance_of(Date)
102
- end
103
- end
104
-
105
- context 'when given :utc_to_local and a Time' do
106
- it 'returns a Date' do
107
- expect(subject.send(:convert_with_timezone, :utc_to_local, utc_time)).to be_an_instance_of(Time)
108
- end
109
- end
110
-
111
- context 'when given :utc_to_local and a DateTime' do
112
- it 'returns a DateTime' do
113
- expect(subject.send(:convert_with_timezone, :utc_to_local, utc_datetime)).to be_an_instance_of(DateTime)
114
- end
115
- end
116
-
117
- context 'when given :utc_to_local and a String' do
118
- it 'returns a DateTime' do
119
- expect(subject.send(:convert_with_timezone, :utc_to_local, utc_string)).to be_an_instance_of(DateTime)
120
- end
121
- end
122
-
123
- describe 'converts local time to UTC' do
124
- context 'when given an ISO8601 date string' do
125
- it { expect(subject.send(:convert_with_timezone, :local_to_utc, local_string)).to eq(utc_datetime) }
126
- end
127
-
128
- context 'when given a Date' do
129
- it { expect(subject.send(:convert_with_timezone, :local_to_utc, date)).to eq(date) }
130
- end
131
-
132
- context 'when given a local Time' do
133
- it { expect(time_to_s(subject.send(:convert_with_timezone, :local_to_utc, local_time))).to eq(time_to_s(utc_time)) }
134
- end
135
-
136
- context 'when given a local DateTime' do
137
- it { expect(subject.send(:convert_with_timezone, :local_to_utc, local_datetime)).to eq(utc_datetime) }
138
- end
139
- end
140
-
141
- describe 'converts UTC time to local' do
142
- context 'when given an ISO8601 date string' do
143
- it { expect(subject.send(:convert_with_timezone, :utc_to_local, utc_string)).to eq(local_datetime) }
144
- end
145
-
146
- context 'when given a Date' do
147
- it { expect(subject.send(:convert_with_timezone, :utc_to_local, date)).to eq(date) }
148
- end
149
-
150
- context 'when given a local Time' do
151
- it { expect(time_to_s(subject.send(:convert_with_timezone, :utc_to_local, utc_time))).to eq(time_to_s(local_time)) }
152
- end
153
-
154
- context 'when given a local DateTime' do
155
- it { expect(subject.send(:convert_with_timezone, :utc_to_local, utc_datetime)).to eq(local_datetime) }
156
- end
157
- end
158
- end
159
-
160
- describe '#iso8601_with_offset' do
161
- context 'when given nil' do
162
- it 'returns nil' do
163
- expect(subject.send(:iso8601_with_offset, nil)).to be_nil
164
- end
165
- end
166
-
167
- describe 'appends correct ISO8601 timezone offset string' do
168
- context 'when UTC' do
169
- it { expect(utc_timezone.send(:iso8601_with_offset, utc_datetime)).to match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/) }
170
- end
171
-
172
- context 'when timezone offset is negative' do
173
- it { expect(subject.send(:iso8601_with_offset, utc_datetime)).to match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-07:00$/) }
174
- end
175
-
176
- context 'when timezone offset is positive' do
177
- it { expect(positive_timezone.send(:iso8601_with_offset, utc_datetime)).to match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+07:00$/) }
178
- end
179
- end
180
- end
181
- end