Class: IceCube::IcalBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ice_cube/builders/ical_builder.rb

Constant Summary collapse

ICAL_DAYS =
['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIcalBuilder

Returns a new instance of IcalBuilder.



7
8
9
# File 'lib/ice_cube/builders/ical_builder.rb', line 7

def initialize
  @hash = {}
end

Class Method Details

.fixnum_to_ical_day(num) ⇒ Object



11
12
13
# File 'lib/ice_cube/builders/ical_builder.rb', line 11

def self.fixnum_to_ical_day(num)
  ICAL_DAYS[num]
end

.ical_duration(duration) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ice_cube/builders/ical_builder.rb', line 47

def self.ical_duration(duration)
  hours = duration / 3600; duration %= 3600
  minutes = duration / 60; duration %= 60
  repr = ''
  repr << "#{hours}H" if hours > 0
  repr << "#{minutes}M" if minutes > 0
  repr << "#{duration}S" if duration > 0
  "PT#{repr}"
end

.ical_format(time, force_utc) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/ice_cube/builders/ical_builder.rb', line 38

def self.ical_format(time, force_utc)
  time = time.dup.utc if force_utc
  if time.utc?
    ":#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time
  else
    ";TZID=#{time.strftime('%Z:%Y%m%dT%H%M%S')}" # local time specified
  end
end

.ical_utc_format(time) ⇒ Object



33
34
35
36
# File 'lib/ice_cube/builders/ical_builder.rb', line 33

def self.ical_utc_format(time)
  time = time.dup.utc
  "#{time.strftime('%Y%m%dT%H%M%SZ')}" # utc time
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/ice_cube/builders/ical_builder.rb', line 15

def [](key)
  @hash[key] ||= []
end

#to_sObject

Build for a single rule entry



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ice_cube/builders/ical_builder.rb', line 20

def to_s
  arr = []
  if freq = @hash.delete('FREQ')
    arr << "FREQ=#{freq.join(',')}"
  end
  arr.concat(@hash.map do |key, value|
    if value.is_a?(Array)
      "#{key}=#{value.join(',')}"
    end
  end.compact)
  arr.join(';')
end