class IcalImporter::Parser

Constants

DEFAULT_TIMEOUT

Attributes

bare_feed[R]
feed[R]
name[R]
timeout[RW]
timezone[R]
url[R]

Public Class Methods

new(url, options={}) click to toggle source

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]
# 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

Public Instance Methods

all_events(&block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/ical_importer/parser.rb, line 45
def recurrence_events(&block)
  tap_and_each (@imported_recurrence_events || []), &block
end
should_parse?() click to toggle source
# File lib/ical_importer/parser.rb, line 29
def should_parse?
  bare_feed.present?
end
single_events(&block) click to toggle source
# File lib/ical_importer/parser.rb, line 41
def single_events(&block)
  tap_and_each (@imported_single_events || []), &block
end
worth_parsing?() click to toggle source
# File lib/ical_importer/parser.rb, line 33
def worth_parsing?
  should_parse? && feed.present? && feed.first.present?
end

Private Instance Methods

get_name() click to toggle source
# File lib/ical_importer/parser.rb, line 66
def get_name
  if feed.present? && feed.first.custom_properties["x_wr_calname"].present?
    feed.first.custom_properties["x_wr_calname"].first.value
  end
end
get_timezone() click to toggle source
# File lib/ical_importer/parser.rb, line 60
def get_timezone
  if feed.present? && feed.first.custom_properties["x_wr_timezone"].present?
    feed.first.custom_properties["x_wr_timezone"].first.value
  end
end
open_ical(protocol = 'http') click to toggle source
# File lib/ical_importer/parser.rb, line 80
def open_ical(protocol = 'http')
  raise ArgumentError, "Must be http or https" unless %w[http https].include? protocol
  begin
    ::Timeout::timeout(@timeout) do
      open prepped_uri(protocol), "r:UTF-8"
    end
  rescue StandardError
    return open_ical 'https' if protocol == 'http'
    nil
  end
end
prepped_uri(protocol) click to toggle source
# File lib/ical_importer/parser.rb, line 92
def prepped_uri(protocol)
  uri = url.strip.gsub(/\A([Ww]ebcal|https?):/, "#{protocol}:")
  uri = begin
          URI.unescape(uri)
        rescue URI::InvalidURIError
        end
  URI.escape(uri)
end
tap_and_each(list) { |event| ... } click to toggle source
# File lib/ical_importer/parser.rb, line 72
def tap_and_each(list)
  list.tap do |r|
    r.each do |event|
      yield event if block_given?
    end
  end
end