Class: Omise::Scheduler
- Inherits:
-
Object
- Object
- Omise::Scheduler
- 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
- #days ⇒ Object (also: #day)
- #end_date(date) ⇒ Object
- #every(n = 1) ⇒ Object
-
#initialize(type, attributes = {}, options = {}) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #months(on:) ⇒ Object (also: #month)
- #parse(str) ⇒ Object
- #start ⇒ Object
- #to_attributes ⇒ Object
- #type ⇒ Object
- #weeks(on:) ⇒ Object (also: #week)
Constructor Details
#initialize(type, attributes = {}, options = {}) ⇒ Scheduler
Returns a new instance of Scheduler.
13 14 15 16 17 18 19 20 |
# File 'lib/omise/scheduler.rb', line 13 def initialize(type, attributes = {}, = {}) @type = type @attributes = attributes @every = [:every] @period = [:period] @on = [:on] @end_date = [:end_date] end |
Instance Method Details
#days ⇒ Object Also known as: day
34 35 36 |
# File 'lib/omise/scheduler.rb', line 34 def days renew(period: "day", on: nil) end |
#end_date(date) ⇒ Object
62 63 64 65 |
# File 'lib/omise/scheduler.rb', line 62 def end_date(date) date = Date.parse(date.to_s) renew(end_date: date.iso8601) end |
#every(n = 1) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/omise/scheduler.rb', line 26 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
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/omise/scheduler.rb', line 50 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
67 68 69 70 71 72 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 |
# File 'lib/omise/scheduler.rb', line 67 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 |
#start ⇒ Object
106 107 108 |
# File 'lib/omise/scheduler.rb', line 106 def start Schedule.create(to_attributes) end |
#to_attributes ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/omise/scheduler.rb', line 110 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 end end |
#type ⇒ Object
22 23 24 |
# File 'lib/omise/scheduler.rb', line 22 def type @type.to_s end |
#weeks(on:) ⇒ Object Also known as: week
39 40 41 42 43 44 45 46 47 |
# File 'lib/omise/scheduler.rb', line 39 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 |