29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/ice_cube/parsers/ical_parser.rb', line 29
def self.rule_from_ical(ical)
raise ArgumentError, "empty ical rule" if ical.nil?
validations = {}
params = {validations: validations, interval: 1}
ical.split(";").each do |rule|
(name, value) = rule.split("=")
raise ArgumentError, "Invalid iCal rule component" if value.nil?
value.strip!
case name
when "FREQ"
params[:rule_type] = "IceCube::#{value[0]}#{value.downcase[1..]}Rule"
when "INTERVAL"
params[:interval] = value.to_i
when "COUNT"
params[:count] = value.to_i
when "UNTIL"
params[:until] = TimeUtil.deserialize_time(value).utc
when "WKST"
params[:week_start] = TimeUtil.ical_day_to_symbol(value)
when "BYSECOND"
validations[:second_of_minute] = value.split(",").map(&:to_i)
when "BYMINUTE"
validations[:minute_of_hour] = value.split(",").map(&:to_i)
when "BYHOUR"
validations[:hour_of_day] = value.split(",").map(&:to_i)
when "BYDAY"
dows = {}
days = []
value.split(",").each do |expr|
day = TimeUtil.ical_day_to_symbol(expr.strip[-2..])
if expr.strip.length > 2 occ = expr[0..-3].to_i
dows[day].nil? ? dows[day] = [occ] : dows[day].push(occ)
days.delete(TimeUtil.sym_to_wday(day))
elsif dows[day].nil?
days.push TimeUtil.sym_to_wday(day)
end
end
validations[:day_of_week] = dows unless dows.empty?
validations[:day] = days unless days.empty?
when "BYMONTHDAY"
validations[:day_of_month] = value.split(",").map(&:to_i)
when "BYMONTH"
validations[:month_of_year] = value.split(",").map(&:to_i)
when "BYYEARDAY"
validations[:day_of_year] = value.split(",").map(&:to_i)
when "BYSETPOS"
else
validations[name] = nil end
end
Rule.from_hash(params)
end
|