Class: Longleaf::ServiceDateHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/longleaf/helpers/service_date_helper.rb

Overview

Helper methods for interacting with dates/timestamps on services

Class Method Summary collapse

Class Method Details

.add_to_timestamp(timestamp, modifier) ⇒ String

Adds the amount of time from modifier to the provided timestamp “<quantity> <time unit>”, where quantity must be a positive whole number and time unit must be second, minute, hour, day, week, month or year (unit may be plural). Any info after a comma will be ignored.

Parameters:

  • timestamp (String)

    ISO-8601 timestamp string

  • modifier (String)

    amount of time to add to the timestamp. It must follow the syntax

Returns:

  • (String)

    the original timestamp in ISO-8601 format with the provided amount of time added.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/longleaf/helpers/service_date_helper.rb', line 13

def self.add_to_timestamp(timestamp, modifier)
  if modifier =~ /^(\d+) *(second|minute|hour|day|week|month|year)s?(,.*)?/
    value = $1.to_i
    unit = $2
  else
    raise ArgumentError.new("Cannot parse time modifier #{modifier}")
  end

  datetime = Time.iso8601(timestamp)
  case unit
  when 'second'
    unit_modifier = 1
  when 'minute'
    unit_modifier = 60
  when 'hour'
    unit_modifier = 3600
  when 'day'
    unit_modifier = 24 * 3600
  when 'week'
    unit_modifier = 7 * 24 * 3600
  when 'month'
    unit_modifier = 30 * 24 * 3600
  when 'year'
    unit_modifier = 365 * 24 * 3600
  end

  modified_time = datetime + (value * unit_modifier)
  modified_time.iso8601(3)
end

.formatted_timestamp(timestamp = Time.now) ⇒ String

Get a timestamp in the format expected for service timestamps.

Parameters:

  • timestamp (Time) (defaults to: Time.now)

    the time to format. Defaults to now.

Returns:

  • (String)

    the time formatted as iso8601



46
47
48
# File 'lib/longleaf/helpers/service_date_helper.rb', line 46

def self.formatted_timestamp(timestamp = Time.now)
  timestamp.utc.iso8601(3).to_s
end

.next_run_needed(md_rec, service_def) ⇒ String

Get the timestamp for the next time the provided service would need to be run for the object described by md_rec

Parameters:

Returns:

  • (String)

    iso8601 timestamp for the next time the service will need to run, or nil if the service does not need to run again.

Raises:

  • (ArgumentError)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/longleaf/helpers/service_date_helper.rb', line 56

def self.next_run_needed(md_rec, service_def)
  raise ArgumentError.new('Must provide a md_rec parameter') if md_rec.nil?
  raise ArgumentError.new('Must provide a service_def parameter') if service_def.nil?

  service_name = service_def.name
  service_rec = md_rec.service(service_name)

  if service_rec.nil? || service_rec.timestamp.nil?
    if service_def.delay.nil?
      return md_rec.registered
    else
      return ServiceDateHelper.add_to_timestamp(md_rec.registered, service_def.delay)
    end
  end

  if service_def.frequency.nil?
    return nil
  else
    return ServiceDateHelper.add_to_timestamp(service_rec.timestamp, service_def.frequency)
  end
end