spok 1.0.0 → 1.1.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.
@@ -1,3 +1,3 @@
1
1
  class Spok
2
- VERSION = "1.0.0"
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -9,7 +9,20 @@ class Spok
9
9
  # module.
10
10
  module Workday
11
11
  # Public: Array of available calendars.
12
- CALENDARS = %i(brasil bovespa)
12
+
13
+ CALENDARS = %i(
14
+ brasil
15
+ bovespa
16
+ canada
17
+ costa_rica
18
+ indonesia
19
+ mexico
20
+ netherlands
21
+ poland
22
+ portugal
23
+ spain
24
+ vietnam
25
+ )
13
26
 
14
27
  # Public: Hash containing all holidays for each available calendar.
15
28
  HOLIDAYS = CALENDARS.map do |calendar|
@@ -18,6 +31,17 @@ class Spok
18
31
  [calendar, Set.new(holidays[calendar.to_s])]
19
32
  end.to_h
20
33
 
34
+ # Public: Hash containing all weekdays.
35
+ WEEKDAYS = {
36
+ sunday: 0,
37
+ monday: 1,
38
+ tuesday: 2,
39
+ wednesday: 3,
40
+ thrusday: 4,
41
+ friday: 5,
42
+ saturday: 6
43
+ }.freeze
44
+
21
45
  # Public: Checks if a given day is a restday.
22
46
  #
23
47
  # date - The Date to be checked.
@@ -30,7 +54,7 @@ class Spok
30
54
  # # => false
31
55
  #
32
56
  # Returns a boolean.
33
- def self.restday?(date, calendar: :brasil)
57
+ def self.restday?(date, calendar: Spok.default_calendar)
34
58
  self.weekend?(date) || self.holiday?(date, calendar: calendar)
35
59
  end
36
60
 
@@ -46,7 +70,7 @@ class Spok
46
70
  # # => true
47
71
  #
48
72
  # Returns a boolean.
49
- def self.workday?(date, calendar: :brasil)
73
+ def self.workday?(date, calendar: Spok.default_calendar)
50
74
  !restday?(date, calendar: calendar)
51
75
  end
52
76
 
@@ -63,7 +87,7 @@ class Spok
63
87
  def self.weekend?(date)
64
88
  weekday = date.wday
65
89
 
66
- weekday == 0 || weekday == 6
90
+ [WEEKDAYS[:saturday], WEEKDAYS[:sunday]].include? weekday
67
91
  end
68
92
 
69
93
  # Public: Checks if a given Date is on a holiday.
@@ -78,7 +102,7 @@ class Spok
78
102
  # # => true
79
103
  #
80
104
  # Returns a boolean.
81
- def self.holiday?(date, calendar: :brasil)
105
+ def self.holiday?(date, calendar: Spok.default_calendar)
82
106
  HOLIDAYS[calendar].include?(date.to_date)
83
107
  end
84
108
 
@@ -92,7 +116,7 @@ class Spok
92
116
  # Examples
93
117
  # Spok::Workday.last_workday(Date.new(2012, 10, 21))
94
118
  # # => #<Date: 2012-10-19 ((2456220j,0s,0n),+0s,2299161j)>
95
- def self.last_workday(date, calendar: :brasil)
119
+ def self.last_workday(date, calendar: Spok.default_calendar)
96
120
  return date if workday?(date, calendar: calendar)
97
121
 
98
122
  last_workday((date - 1.day), calendar: calendar)
@@ -108,7 +132,7 @@ class Spok
108
132
  # Examples
109
133
  # Spok::Workday.next_workday(Date.new(2012, 10, 21))
110
134
  # # => #<Date: 2012-10-19 ((2456220j,0s,0n),+0s,2299161j)>
111
- def self.next_workday(date, calendar: :brasil)
135
+ def self.next_workday(date, calendar: Spok.default_calendar)
112
136
  return date if workday?(date, calendar: calendar)
113
137
 
114
138
  next_workday((date + 1.day), calendar: calendar)
Binary file
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ require 'spok/workday'
3
+
4
+ describe 'Spok Calendars' do
5
+ Spok::Workday::CALENDARS.each do |calendar|
6
+ it "has valid dates for #{calendar}" do
7
+ file = File.new(File.join(File.dirname(__FILE__), "../../../lib/spok/config/#{calendar}.yml"))
8
+ last_day = Date.strptime(file.readlines[-1], '- %Y-%m-%d')
9
+ valid_calendar = last_day - 365 > Date.today
10
+
11
+ expect(valid_calendar).to eq(true)
12
+ end
13
+ end
14
+ end
@@ -38,6 +38,33 @@ describe Spok::Workday do
38
38
  end
39
39
  end
40
40
 
41
+ context 'days using spanish calendar' do
42
+ it 'is not a workday' do
43
+ ['2009-01-06', '2009-04-10', '2009-04-12'].each do |holiday|
44
+ expect(described_class.workday?(Date.parse(holiday), calendar: :spain)).to eq(false)
45
+ end
46
+ end
47
+
48
+ it 'is a workday' do
49
+ ['2009-01-07', '2019-01-14', '2020-04-22'].each do |workday|
50
+ expect(described_class.workday?(Date.parse(workday), calendar: :spain)).to eq(true)
51
+ end
52
+ end
53
+ end
54
+
55
+ context 'days using dutch calendar' do
56
+ it 'is not a workday' do
57
+ ['2019-06-10', '2019-12-25', '2019-12-26'].each do |holiday|
58
+ expect(described_class.workday?(Date.parse(holiday), calendar: :netherlands)).to eq(false)
59
+ end
60
+ end
61
+
62
+ it 'is a workday' do
63
+ ['2010-06-10', '2009-09-11', '2019-12-02'].each do |workday|
64
+ expect(described_class.workday?(Date.parse(workday), calendar: :netherlands)).to eq(true)
65
+ end
66
+ end
67
+ end
41
68
  end
42
69
 
43
70
  describe '#restday?' do
@@ -206,4 +206,14 @@ describe Spok do
206
206
  subject { Spok.new(sunday, tuesday).to_range }
207
207
  it { is_expected.to eq((sunday..tuesday)) }
208
208
  end
209
+
210
+ describe '#default_calendar' do
211
+ it 'changes default_calendar' do
212
+ default_calendar_was = Spok.default_calendar
213
+ spok = double("spok", :default_calendar => :bovespa)
214
+
215
+ expect(default_calendar_was).not_to eq(spok.default_calendar)
216
+ expect(default_calendar_was).to eq(:brasil)
217
+ end
218
+ end
209
219
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spok
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnetis Staff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-14 00:00:00.000000000 Z
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -84,9 +84,19 @@ files:
84
84
  - lib/spok.rb
85
85
  - lib/spok/config/bovespa.yml
86
86
  - lib/spok/config/brasil.yml
87
+ - lib/spok/config/canada.yml
88
+ - lib/spok/config/costa_rica.yml
89
+ - lib/spok/config/indonesia.yml
90
+ - lib/spok/config/mexico.yml
91
+ - lib/spok/config/netherlands.yml
92
+ - lib/spok/config/poland.yml
93
+ - lib/spok/config/portugal.yml
94
+ - lib/spok/config/spain.yml
95
+ - lib/spok/config/vietnam.yml
87
96
  - lib/spok/version.rb
88
97
  - lib/spok/workday.rb
89
98
  - pkg/spok-1.0.0.gem
99
+ - spec/lib/spok/calendars_spec.rb
90
100
  - spec/lib/spok/workday_spec.rb
91
101
  - spec/lib/spok_spec.rb
92
102
  - spec/spec_helper.rb
@@ -116,6 +126,7 @@ signing_key:
116
126
  specification_version: 4
117
127
  summary: A gem to work with periods of dates
118
128
  test_files:
129
+ - spec/lib/spok/calendars_spec.rb
119
130
  - spec/lib/spok/workday_spec.rb
120
131
  - spec/lib/spok_spec.rb
121
132
  - spec/spec_helper.rb