Class: ICS::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/ics/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Event

Given a hash of attributes, define a method for each key that returns the value. Attributes stored in instance variable.



9
10
11
12
13
14
15
16
17
18
# File 'lib/ics/event.rb', line 9

def initialize(attributes = {})
  @attributes = attributes
  attributes.each do |key, val|
    unless respond_to?(key)
      self.class.send :define_method, key do
        @attributes[key]
      end
    end
  end
end

Class Method Details

.file(file) ⇒ Object

Given an exported ical file, parse it and create events.



35
36
37
38
39
40
41
42
43
# File 'lib/ics/event.rb', line 35

def file(file)
  # format line endings.
  content = file.readlines.map(&:chomp).join($/)
  line_ending = $/
  content.split("BEGIN:VEVENT#{line_ending}")[1..-1].map do |data_string|
    data_string = data_string.split("END:VEVENT#{line_ending}").first
    new parse(data_string)
  end
end

.parse(str) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/ics/event.rb', line 45

def parse(str)
  str.split($/).inject({}) do |hash, line|
    key, value = line.split(':', 2)
    next hash if key =~ /^BEGIN/ # Ignore any other book ends.
    value = value.chomp if value
    key = key.split(';', 2).first # Ignore extra data other than just the name of the attribute.
    hash[key.downcase.to_sym] = value
    hash
  end
end

Instance Method Details

#dtstartObject Also known as: started_at

Return time object. Assumes time in UTC.



22
23
24
25
# File 'lib/ics/event.rb', line 22

def dtstart
  return nil unless @attributes[:dtstart]
  DateTime.parse(@attributes[:dtstart]).to_time.utc
end

#started_onObject



28
29
30
# File 'lib/ics/event.rb', line 28

def started_on
  dtstart.to_date if dtstart
end