Class: GCal4Ruby::Recurrence
- Inherits:
-
Object
- Object
- GCal4Ruby::Recurrence
- Defined in:
- lib/gcal4ruby/recurrence.rb
Overview
The Recurrence class stores information on an Event’s recurrence. The class implements the RFC 2445 iCalendar recurrence description.
Instance Attribute Summary collapse
-
#all_day ⇒ Object
True if the event is all day (i.e. no start/end time).
-
#end_time ⇒ Object
The event end date/time.
-
#event ⇒ Object
the event reference.
-
#frequency ⇒ Object
The event frequency.
-
#repeat_until ⇒ Object
The date until which the event will be repeated.
-
#start_time ⇒ Object
The event start date/time.
Instance Method Summary collapse
-
#initialize(vars = {}) ⇒ Recurrence
constructor
Accepts an optional attributes hash or a string containing a properly formatted ISO 8601 recurrence rule.
-
#load(rec) ⇒ Object
Accepts a string containing a properly formatted ISO 8601 recurrence rule and loads it into the recurrence object.
-
#to_recurrence_string ⇒ Object
Returns a string with the correctly formatted ISO 8601 recurrence rule.
- #to_s ⇒ Object
Constructor Details
#initialize(vars = {}) ⇒ Recurrence
Accepts an optional attributes hash or a string containing a properly formatted ISO 8601 recurrence rule. Returns a new Recurrence object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/gcal4ruby/recurrence.rb', line 58 def initialize(vars = {}) if vars.is_a? Hash vars.each do |key, value| self.send("#{key}=", value) end elsif vars.is_a? String self.load(vars) end @all_day ||= false end |
Instance Attribute Details
#all_day ⇒ Object
True if the event is all day (i.e. no start/end time)
55 56 57 |
# File 'lib/gcal4ruby/recurrence.rb', line 55 def all_day @all_day end |
#end_time ⇒ Object
The event end date/time
47 48 49 |
# File 'lib/gcal4ruby/recurrence.rb', line 47 def end_time @end_time end |
#event ⇒ Object
the event reference
49 50 51 |
# File 'lib/gcal4ruby/recurrence.rb', line 49 def event @event end |
#frequency ⇒ Object
The event frequency
53 54 55 |
# File 'lib/gcal4ruby/recurrence.rb', line 53 def frequency @frequency end |
#repeat_until ⇒ Object
The date until which the event will be repeated
51 52 53 |
# File 'lib/gcal4ruby/recurrence.rb', line 51 def repeat_until @repeat_until end |
#start_time ⇒ Object
The event start date/time
45 46 47 |
# File 'lib/gcal4ruby/recurrence.rb', line 45 def start_time @start_time end |
Instance Method Details
#load(rec) ⇒ Object
Accepts a string containing a properly formatted ISO 8601 recurrence rule and loads it into the recurrence object.
Contributed by John Paul Narowski.
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/gcal4ruby/recurrence.rb', line 71 def load(rec) @frequency = {} attrs = rec.split("\n") attrs.each do |val| break if val == "BEGIN:VTIMEZONE" # Ignoring the time zone for now key, value = val.split(":") if key == 'RRULE' args = {} value.split(";").each do |rr| rr_key, rr_value = rr.split("=") rr_key = rr_key.downcase.to_sym unless @frequency.has_key?(rr_key) if rr_key == :until @repeat_until = Time.parse_complete(rr_value) else args[rr_key] = rr_value end end end case args[:freq] when 'DAILY' @frequency['daily'] = true when 'WEEKLY' @frequency['weekly'] = args[:byday] && args[:byday].split(',') when 'MONTHLY' if args[:byday] @frequency['monthly'] = args[:byday] || "" @frequency[:day_of_week] = true else @frequency['monthly'] = args[:bymonthday] && args[:bymonthday].to_i end when 'YEARLY' @frequency['yearly'] = args[:byyearday] && args[:byyearday].to_i end elsif key == 'INTERVAL' @frequency[:interval] = value.to_i unless value.nil? || value.empty? elsif key.include?('DTSTART;VALUE=DATE') @start_time ||= Time.parse(value) @all_day = true elsif key.include?("DTSTART;TZID") or key.include?("DTSTART") or key.include?('DTSTART;VALUE=DATE-TIME') @start_time ||= Time.parse_complete(value) elsif key.include?('DTEND;VALUE=DATE') @end_time ||= Time.parse(value) elsif key.include?("DTEND;TZID") or key.include?("DTEND") or key.include?('DTEND;VALUE=DATE-TIME') @end_time ||= Time.parse_complete(value) end end @frequency[:interval] = 1 unless @frequency[:interval] && @frequency[:interval].to_i > 0 end |
#to_recurrence_string ⇒ Object
Returns a string with the correctly formatted ISO 8601 recurrence rule
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/gcal4ruby/recurrence.rb', line 166 def to_recurrence_string output = '' if @all_day output += "DTSTART;VALUE=DATE:#{@start_time.utc.strftime("%Y%m%d")}\n" else output += "DTSTART;VALUE=DATE-TIME:#{@start_time.complete}\n" end if @all_day output += "DTEND;VALUE=DATE:#{@end_time.utc.strftime("%Y%m%d")}\n" else output += "DTEND;VALUE=DATE-TIME:#{@end_time.complete}\n" end output += "RRULE:" if @frequency f = 'FREQ=' i = '' by = '' day_of_week = @frequency.delete(:day_of_week) @frequency.each do |key, v| if v.is_a?(Array) if v.size > 0 value = v.join(",") else value = nil end else value = v end f += "#{key.to_s.upcase};" if key.to_s.downcase != 'interval' case key.to_s.downcase when "secondly" by += "BYSECOND=#{value};" when "minutely" by += "BYMINUTE=#{value};" when "hourly" by += "BYHOUR=#{value};" when "weekly" by += "BYDAY=#{value};" if value when "monthly" if day_of_week by += "BYDAY=#{value};" else by += "BYMONTHDAY=#{value};" end when "yearly" by += "BYYEARDAY=#{value};" when 'interval' i += "INTERVAL=#{value};" end end output += f+by+i end if @repeat_until output += "UNTIL=#{@repeat_until.strftime("%Y%m%d")}" end output += "\n" end |
#to_s ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/gcal4ruby/recurrence.rb', line 121 def to_s output = '' if @frequency f = '' i = '' by = '' @frequency.each do |key, v| key = key.to_s.downcase if v.is_a?(Array) if v.size > 0 value = v.join(",") else value = nil end else value = v end f += "#{key}" if key != 'interval' case key when "secondly" by += " every #{value} second" when "minutely" by += " every #{value} minute" when "hourly" by += " every #{value} hour" when "weekly" by += " on #{value}" if value when "monthly" by += " on #{value}" when "yearly" by += " on the #{value} day of the year" when 'interval' i += " for #{value} times" end end output += f+i+by end if @repeat_until output += " and repeats until #{@repeat_until.strftime("%m/%d/%Y")}" end output end |