Skip to content

Commit

Permalink
Merge pull request #152 from zendesk/craig/hours-handling
Browse files Browse the repository at this point in the history
Raise an exception when timestamp is malformed
  • Loading branch information
craiglittle authored Feb 21, 2023
2 parents 277d0d5 + 332f4ba commit d6196cd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Biz.configure do |config|
end
```

Configured timestamps must be in either `HH:MM` or `HH:MM:SS` format.

Shifts act as exceptions to the hours configured for a particular date; that is,
if a date is configured with both hours-based intervals and shifts, the shifts
are in force and the intervals are disregarded.
Expand Down
5 changes: 4 additions & 1 deletion lib/biz/day_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def from_timestamp(timestamp)
(match[:minute].to_i * Time.minute_seconds) +
match[:second].to_i
)
}
} or fail(
Error::Configuration,
'invalid timestamp: must be in `HH:MM` or `HH:MM:SS` format'
)
end

def midnight
Expand Down
66 changes: 48 additions & 18 deletions spec/day_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,63 @@
end

describe '.from_timestamp' do
context 'when the timestamp is malformed' do
context 'when the timestamp is not a timestamp' do
let(:timestamp) { 'timestamp' }

it 'returns nil' do
expect(described_class.from_timestamp(timestamp)).to eq nil
it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'when the timestamp is in `H:MM` format' do
let(:timestamp) { '5:35' }

it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'when the timestamp is well formed' do
context 'without seconds' do
let(:timestamp) { '21:43' }
context 'when the timestamp is in `HH:M` format' do
let(:timestamp) { '12:3' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 21, min: 43))
)
end
it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'with seconds' do
let(:timestamp) { '10:55:23' }
context 'when the timestamp is in `HH:MM:S` format' do
let(:timestamp) { '11:35:3' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 10, min: 55, sec: 23))
)
end
it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'when the timestamp is in `HH:MM` format' do
let(:timestamp) { '21:43' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 21, min: 43))
)
end
end

context 'when the timestamp is in `HH:MM:SS` format' do
let(:timestamp) { '10:55:23' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 10, min: 55, sec: 23))
)
end
end
end
Expand Down

0 comments on commit d6196cd

Please sign in to comment.