Class: IcalImporter::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ical_importer/parser.rb

Constant Summary collapse

DEFAULT_TIMEOUT =
8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Parser

Get a new Parser object

url - URL where we download the ical feed options - Options hash

:timeout  - Custom timeout for downloading ical feed [Default: 8]


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ical_importer/parser.rb', line 13

def initialize(url, options={})
  @url = url
  @timeout = options[:timeout] || DEFAULT_TIMEOUT
  @bare_feed = open_ical
  if should_parse?
    @bare_feed.pos = 0
    begin
      @feed = Icalendar.parse @bare_feed
    rescue Exception => e
      # I know, I'm dirty, fix this to log to a config'd log
    end
  end
  @timezone = get_timezone
  @name = get_name
end

Instance Attribute Details

#bare_feedObject (readonly)

Returns the value of attribute bare_feed.



3
4
5
# File 'lib/ical_importer/parser.rb', line 3

def bare_feed
  @bare_feed
end

#feedObject (readonly)

Returns the value of attribute feed.



3
4
5
# File 'lib/ical_importer/parser.rb', line 3

def feed
  @feed
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ical_importer/parser.rb', line 3

def name
  @name
end

#timeoutObject

Returns the value of attribute timeout.



4
5
6
# File 'lib/ical_importer/parser.rb', line 4

def timeout
  @timeout
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



3
4
5
# File 'lib/ical_importer/parser.rb', line 3

def timezone
  @timezone
end

#urlObject (readonly)

Returns the value of attribute url.



3
4
5
# File 'lib/ical_importer/parser.rb', line 3

def url
  @url
end

Instance Method Details

#all_events(&block) ⇒ Object



37
38
39
# File 'lib/ical_importer/parser.rb', line 37

def all_events(&block)
  tap_and_each (@imported_single_events || []) + (@imported_recurrence_events || []), &block
end

#parse(&block) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/ical_importer/parser.rb', line 49

def parse(&block)
  if worth_parsing?
    collected = Collector.new(feed.first.events).collect
    @imported_single_events = collected.single_events
    @imported_recurrence_events = collected.recurrence_events
    tap_and_each (@imported_single_events + @imported_recurrence_events), &block
  end
end

#recurrence_events(&block) ⇒ Object



45
46
47
# File 'lib/ical_importer/parser.rb', line 45

def recurrence_events(&block)
  tap_and_each (@imported_recurrence_events || []), &block
end

#should_parse?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/ical_importer/parser.rb', line 29

def should_parse?
  bare_feed.present?
end

#single_events(&block) ⇒ Object



41
42
43
# File 'lib/ical_importer/parser.rb', line 41

def single_events(&block)
  tap_and_each (@imported_single_events || []), &block
end

#worth_parsing?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ical_importer/parser.rb', line 33

def worth_parsing?
  should_parse? && feed.present? && feed.first.present?
end