Module: Cronex::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/cronex/utils.rb

Instance Method Summary collapse

Instance Method Details

#day_of_week_name(number) ⇒ Object



25
26
27
# File 'lib/cronex/utils.rb', line 25

def day_of_week_name(number)
  Date::DAYNAMES[number.to_i % 7]
end

#format_minutes(minute_expression) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/cronex/utils.rb', line 29

def format_minutes(minute_expression)
  if minute_expression.include?(',')
    minute_expression.split(',').map { |m| format('%02d', integer(m)) }.join(',')
  else
    format('%02d', integer(minute_expression))
  end
end

#format_time(hour_expression, minute_expression, second_expression = '', timezone = 'UTC') ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/cronex/utils.rb', line 37

def format_time(hour_expression, minute_expression, second_expression = '', timezone = 'UTC')
  hour = integer(hour_expression)
  minute = integer(minute_expression)
  second = second_expression.to_s.empty? ? 0 : integer(second_expression)
  tz = TZInfo::Timezone.get(timezone)
  time = tz.utc_to_local(Date.today.to_time + hour * 60 * 60 + minute * 60 + second)
  format = present?(second_expression) ? '%l:%M:%S %p' : '%l:%M %p'
  time.strftime(format).lstrip
end

#include_any?(str, chars) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/cronex/utils.rb', line 11

def include_any?(str, chars)
  chars.any? { |char| str.include?(char) }
end

#integer(str) ⇒ Object



19
20
21
22
23
# File 'lib/cronex/utils.rb', line 19

def integer(str)
  # strip leading zeros in numbers to prevent '08', '09'
  # from being treated as invalid octals
  Integer(str.sub(/^0*(\d+)/, '\1'))
end

#number?(str) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/cronex/utils.rb', line 15

def number?(str)
  integer(str) rescue nil
end

#present?(str) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/cronex/utils.rb', line 7

def present?(str)
  !str.to_s.strip.empty?
end