Class: GCal4Ruby::Recurrence

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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



31
32
33
34
35
36
37
38
39
40
# File 'lib/gcal4ruby/recurrence.rb', line 31

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_dayObject

True if the event is all day (i.e. no start/end time)



28
29
30
# File 'lib/gcal4ruby/recurrence.rb', line 28

def all_day
  @all_day
end

#endObject

The event end date/time



20
21
22
# File 'lib/gcal4ruby/recurrence.rb', line 20

def end
  @end
end

#eventObject

the event reference



22
23
24
# File 'lib/gcal4ruby/recurrence.rb', line 22

def event
  @event
end

#frequencyObject

The event frequency



26
27
28
# File 'lib/gcal4ruby/recurrence.rb', line 26

def frequency
  @frequency
end

#repeat_untilObject

The date until which the event will be repeated



24
25
26
# File 'lib/gcal4ruby/recurrence.rb', line 24

def repeat_until
  @repeat_until
end

#startObject

The event start date/time



18
19
20
# File 'lib/gcal4ruby/recurrence.rb', line 18

def start
  @start
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



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
# File 'lib/gcal4ruby/recurrence.rb', line 43

def load(rec)
  attrs = rec.split("\n")
  attrs.each do |val|
    key, value = val.split(":")
    case key
      when 'DTSTART'
        @start = Time.parse_complete(value)
      when 'DTSTART;VALUE=DATE'
        @start = Time.parse(value)
        @all_day = true
      when 'DTSTART;VALUE=DATE-TIME'
        @start = Time.parse_complete(value)
      when 'DTEND'
        @end = Time.parse_complete(value)
      when 'DTEND;VALUE=DATE'
        @end = Time.parse(value)
      when 'DTEND;VALUE=DATE-TIME'
        @end = Time.parse_complete(value)
      when 'RRULE'
        vals = value.split(";")
        key = ''
        by = ''
        int = nil
        vals.each do |rr|
          a, h = rr.split("=")
          case a 
            when 'FREQ'
              key = h.downcase.capitalize
            when 'INTERVAL'
              int = h
            when 'UNTIL'
              @repeat_until = Time.parse(value)
            else
              by = h.split(",")
          end
        end
        @frequency = {key => by}
        @frequency.merge({'interval' => int}) if int
    end
  end
end

#to_sObject

Returns a string with the correctly formatted ISO 8601 recurrence rule



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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/gcal4ruby/recurrence.rb', line 86

def to_s
  
  output = ''
  if @all_day
    output += "DTSTART;VALUE=DATE:#{@start.utc.strftime("%Y%m%d")}\n"
  else
    output += "DTSTART;VALUE=DATE-TIME:#{@start.complete}\n"
  end
  if @all_day
    output += "DTEND;VALUE=DATE:#{@end.utc.strftime("%Y%m%d")}\n"
  else
    output += "DTEND;VALUE=DATE-TIME:#{@end.complete}\n"
  end
  output += "RRULE:"
  if @frequency
    f = 'FREQ='
    i = ''
    by = ''
    @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.upcase};" if key != 'interval'
      case key.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"
          by += "BYDAY=#{value};"
        when "yearly"
          by += "BYYEARDAY=#{value};"
        when 'interval'
          i += "INTERVAL=#{value};"
      end
    end
    output += f+i+by
  end      
  if @repeat_until
    output += "UNTIL=#{@repeat_until.strftime("%Y%m%d")}"
  end
  
  output += "\n"
end