Skip to content

Commit

Permalink
Support setting retires_on and retirement_warn for services from dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcculloug committed Jul 30, 2018
1 parent e00b17b commit bf9aead
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
62 changes: 61 additions & 1 deletion app/models/service/dialog_properties.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Service
class DialogProperties
RETIREMENT_FIELD_NAMES = %w(retires_on retires_in_hours retires_in_days retire_on_date).freeze
RETIREMENT_WARN_FIELD_NAMES = %w(warn_on warn_in_days warn_in_hours warn_offset_days warn_offset_hours).freeze

def initialize(options)
@attributes = {}
@options = options || {}
Expand All @@ -16,7 +19,7 @@ def parse
private

def parse_options
%w(name description).each do |field_name|
(%w(name description) + RETIREMENT_FIELD_NAMES).each do |field_name|
dialog_field_name = "dialog_service_#{field_name}"
dialog_field_value = @options[dialog_field_name]
parse_value(field_name, dialog_field_name, dialog_field_value) if @options.key?(dialog_field_name)
Expand All @@ -36,5 +39,62 @@ def name(value)
def description(value)
@attributes[:description] = value if value.present?
end

def retires_on(value)
self.retire_on_date = Time.zone.parse(value)
end

def retires_in_hours(value)
retires_in_duration(value, :hours)
end

def retires_in_days(value)
retires_in_duration(value, :days)
end

def retires_in_duration(value, modifier)
self.retire_on_date = Time.zone.now + offset_set(value, modifier)
end

def offset_set(value, modifier)
value.to_i.send(modifier).tap do |offset|
raise "Offset cannot be a zero or negative value" if offset.zero? || offset.negative?
end
end

def retire_on_date=(value)
@attributes[:retires_on] = value
retirement_warning
end

def retirement_warning
warn_value = parse_retirement_warn
@attributes[:retirement_warn] = warn_value if warn_value
end

def parse_retirement_warn
warn_key, value = retirement_warn_properties

case warn_key
when 'warn_on'
Time.zone.parse(value)
when 'warn_in_days'
Time.zone.now + offset_set(value, :days)
when 'warn_in_hours'
Time.zone.now + offset_set(value, :hours)
when 'warn_offset_days'
@attributes[:retires_on] - offset_set(value, :days)
when 'warn_offset_hours'
@attributes[:retires_on] - offset_set(value, :hours)
end
end

def retirement_warn_properties
RETIREMENT_WARN_FIELD_NAMES.each do |field_name|
dialog_field_name = "dialog_service_retirement_#{field_name}"

return field_name, @options[dialog_field_name] if @options[dialog_field_name].present?
end
end
end
end
42 changes: 42 additions & 0 deletions spec/models/service/dialog_properties_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,46 @@
expect(described_class.parse(options)).to eq(:description => 'test description from dialog')
end
end

context 'retires_on' do
it 'with invalid time' do
options = {'dialog_service_retires_on' => 'xyz'}
parsed_results = described_class.parse(options)

expect(parsed_results[:retires_on]).to be_nil
expect(parsed_results[:retirement_warn]).to be_nil
end

it 'with valid time' do
time = Time.new(2018, 7, 21, 12, 20, 0, 0)
Timecop.freeze(time) do
options = {'dialog_service_retires_on' => time.to_s}
parsed_results = described_class.parse(options)

expect(parsed_results[:retires_on]).to eq(time)
expect(parsed_results[:retirement_warn]).to be_nil
end
end
end

context 'retires_in_hours' do
it 'with invalid time' do
options = {'dialog_service_retires_in_hours' => 'xyz'}
parsed_results = described_class.parse(options)

expect(parsed_results[:retires_on]).to be_nil
expect(parsed_results[:retirement_warn]).to be_nil
end

it 'with valid time' do
time = Time.new(2018, 7, 21, 12, 20, 0, 0)
Timecop.freeze(time) do
options = {'dialog_service_retires_in_hours' => 5}
parsed_results = described_class.parse(options)

expect(parsed_results[:retires_on]).to eq(time + 5.hours)
expect(parsed_results[:retirement_warn]).to be_nil
end
end
end
end

0 comments on commit bf9aead

Please sign in to comment.