Class: Tilia::VObject::BirthdayCalendarGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/tilia/v_object/birthday_calendar_generator.rb

Overview

This class generates birthday calendars.

Constant Summary collapse

DEFAULT_YEAR =

Default year. Used for dates without a year.

2000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects = nil) ⇒ BirthdayCalendarGenerator

Creates the generator.

Check the setTimeRange and setObjects methods for details about the arguments.

Parameters:

  • objects (defaults to: nil)


25
26
27
28
29
30
# File 'lib/tilia/v_object/birthday_calendar_generator.rb', line 25

def initialize(objects = nil)
  @objects = []
  @format = '%1s\'s Birthday'

  self.objects = objects if objects
end

Instance Attribute Details

#format=(value) ⇒ void (writeonly)

This method returns an undefined value.

Sets the output format for the SUMMARY

Parameters:

  • format (String)


65
66
67
# File 'lib/tilia/v_object/birthday_calendar_generator.rb', line 65

def format=(value)
  @format = value
end

Instance Method Details

#objects=(objects) ⇒ void

This method returns an undefined value.

Sets the input objects.

You must either supply a vCard as a string or as a Component/VCard object. It’s also possible to supply an array of strings or objects.

Parameters:

  • objects


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tilia/v_object/birthday_calendar_generator.rb', line 40

def objects=(objects)
  objects = [objects] unless objects.is_a?(Array)

  @objects = []
  objects.each do |object|
    if object.is_a?(String)
      v_obj = Reader.read(object)
      unless v_obj.is_a?(Component::VCard)
        fail ArgumentError, 'String could not be parsed as Component::VCard by setObjects'
      end

      @objects << v_obj
    elsif object.is_a?(Component::VCard)
      @objects << object
    else
      fail ArgumentError, 'You can only pass strings or Component::VCard arguments to setObjects'
    end
  end
end

#resultComponent/VCalendar

Parses the input data and returns a VCALENDAR.

Returns:



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
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/tilia/v_object/birthday_calendar_generator.rb', line 70

def result
  calendar = Component::VCalendar.new

  @objects.each do |object|
    # Skip if there is no BDAY property.
    next if object.select('BDAY').empty?

    # We've seen clients (ez-vcard) putting "BDAY:" properties
    # without a value into vCards. If we come across those, we'll
    # skip them.
    next if object['BDAY'].value.blank?

    # We're always converting to vCard 4.0 so we can rely on the
    # VCardConverter handling the X-APPLE-OMIT-YEAR property for us.
    object = object.convert(Document::VCARD40)

    # Skip if the card has no FN property.
    next unless object.key?('FN')

    # Skip if the BDAY property is not of the right type.
    next unless object['BDAY'].is_a?(Property::VCard::DateAndOrTime)

    # Skip if we can't parse the BDAY value.
    begin
      date_parts = DateTimeParser.parse_v_card_date_time(object['BDAY'].value)
    rescue InvalidDataException
      next
    end

    # Set a year if it's not set.
    unknown_year = false

    unless date_parts['year']
      object['BDAY'] = "#{DEFAULT_YEAR}-#{date_parts['month']}-#{date_parts['date']}"
      unknown_year = true
    end

    # Create event.
    event = calendar.add(
      'VEVENT',
      'SUMMARY'      => format(@format, object['FN'].value),
      'DTSTART'      => Time.zone.parse(object['BDAY'].value),
      'RRULE'        => 'FREQ=YEARLY',
      'TRANSP'       => 'TRANSPARENT'
    )

    # add VALUE=date
    event['DTSTART']['VALUE'] = 'DATE'

    # Add X-SABRE-BDAY property.
    if unknown_year
      event.add(
        'X-SABRE-BDAY',
        'BDAY',
        'X-SABRE-VCARD-UID' => object['UID'].value,
        'X-SABRE-VCARD-FN'  => object['FN'].value,
        'X-SABRE-OMIT-YEAR' => DEFAULT_YEAR
      )
    else
      event.add(
        'X-SABRE-BDAY',
        'BDAY',
        'X-SABRE-VCARD-UID' => object['UID'].value,
        'X-SABRE-VCARD-FN'  => object['FN'].value
      )
    end
  end

  calendar
end