Class: Omise::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/omise/scheduler.rb

Constant Summary collapse

WEEKDAYS =
Date::DAYNAMES.map(&:downcase).freeze
MONTH_WEEKDAYS =
%w[first second third fourth last]
.product(WEEKDAYS)
.map { |d| d.join("_") }
.freeze

Instance Method Summary collapse

Constructor Details

#initialize(type, attributes = {}, options = {}) ⇒ Scheduler

Returns a new instance of Scheduler.



13
14
15
16
17
18
19
20
21
# File 'lib/omise/scheduler.rb', line 13

def initialize(type, attributes = {}, options = {})
  @type       = type
  @attributes = attributes
  @every      = options[:every]
  @period     = options[:period]
  @on         = options[:on]
  @end_date   = options[:end_date]
  @start_date = options[:start_date]
end

Instance Method Details

#daysObject Also known as: day



35
36
37
# File 'lib/omise/scheduler.rb', line 35

def days
  renew(period: "day", on: nil)
end

#end_date(date) ⇒ Object



68
69
70
71
# File 'lib/omise/scheduler.rb', line 68

def end_date(date)
  date = Date.parse(date.to_s)
  renew(end_date: date.iso8601)
end

#every(n = 1) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/omise/scheduler.rb', line 27

def every(n = 1)
  unless n.is_a?(Integer)
    raise TypeError, "#{n} is not an Integer"
  end

  renew(every: n)
end

#months(on:) ⇒ Object Also known as: month



51
52
53
54
55
56
57
58
59
60
# File 'lib/omise/scheduler.rb', line 51

def months(on:)
  case on
  when Array
    month_with_days(on)
  when String
    month_with_weekday(on)
  else
    raise TypeError, "#{on.inspect} must be an Array or a String, not a #{on.class}"
  end
end

#parse(str) ⇒ Object

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/omise/scheduler.rb', line 73

def parse(str)
  str = str.downcase
    .gsub(/ +/, " ")
    .gsub("every ", "")
    .gsub("the ", "")
    .gsub(" and ", ",")
    .gsub(/,+/, ",")
    .gsub(/ ?, ?/, ",")
    .gsub(",", " ")

  leftover, end_date = str.split(" until ", 2)
  period, on         = leftover.split(" on ", 2)
  on                 = on.to_s.strip.split(" ")
  every              = period.to_i
  every              = 1 if every.zero?
  month_weekday      = on.join("_")
  month_days         = on.map { |d| d.to_i }

  schedule = self.end_date(end_date).every(every)

  if period.include?("day")
    return schedule.days
  end

  if period.include?("week")
    return schedule.weeks(on: on.uniq)
  end

  if period.include?("month") && MONTH_WEEKDAYS.include?(month_weekday)
    return schedule.months(on: month_weekday)
  end

  if period.include?("month")
    return schedule.months(on: month_days.uniq)
  end

  raise ArgumentError, "invalid schedule: #{src}"
end

#startObject



112
113
114
# File 'lib/omise/scheduler.rb', line 112

def start
  Schedule.create(to_attributes)
end

#start_date(date) ⇒ Object



63
64
65
66
# File 'lib/omise/scheduler.rb', line 63

def start_date(date)
  date = Date.parse(date.to_s)
  renew(start_date: date.iso8601)
end

#to_attributesObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/omise/scheduler.rb', line 116

def to_attributes
  {}.tap do |a|
    a[@type]       = @attributes
    a[:every]      = @every if @every
    a[:period]     = @period if @period
    a[:on]         = @on if @on
    a[:end_date]   = @end_date if @end_date
    a[:start_date] = @start_date if @start_date
  end
end

#typeObject



23
24
25
# File 'lib/omise/scheduler.rb', line 23

def type
  @type.to_s
end

#weeks(on:) ⇒ Object Also known as: week



40
41
42
43
44
45
46
47
48
# File 'lib/omise/scheduler.rb', line 40

def weeks(on:)
  on.each do |day|
    unless WEEKDAYS.include?(day)
      raise "#{day} is not one of #{WEEKDAYS}"
    end
  end

  renew(period: "week", on: { weekdays: on })
end