Class: Achoo::ICal

Inherits:
Object
  • Object
show all
Includes:
UI::ExceptionHandling
Defined in:
lib/achoo/ical.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UI::ExceptionHandling

#get_exception_reason, #handle_exception, #handle_fatal_exception

Constructor Details

#initialize(ics_str) ⇒ ICal

Returns a new instance of ICal.



34
35
36
# File 'lib/achoo/ical.rb', line 34

def initialize(ics_str)
  @calendar = RiCal.parse_string(ics_str).first
end

Class Method Details

.from_http_request(params) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/achoo/ical.rb', line 15

def self.from_http_request(params)
  return @@cache[params] if @@cache[params]
  
  url = URI.parse(params[:url])

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  ics = http.start do |http|
    request = Net::HTTP::Get.new(url.path)
    request.basic_auth(params[:user], params[:pass])
    response = http.request(request)
    raise response.message unless response.is_a?(Net::HTTPSuccess)
    response.body
  end

  @@cache[params] = self.new(ics)
end

Instance Method Details



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

def print_events(date, io=$stdout)
  arg_start = date
  arg_end   = date + 1

  @calendar.events.each do |e|
    begin
      if !e.x_properties['X-MICROSOFT-CDO-ALLDAYEVENT'].empty? && e.x_properties['X-MICROSOFT-CDO-ALLDAYEVENT'].first.value == 'TRUE'
        # FIX handle this
      elsif e.recurs?
        e.occurrences({:overlapping => [arg_start, arg_end]}).each do |o|
          print_event(o, io)
        end
      elsif e.dtstart >= arg_start && e.dtstart <= arg_end \
        || e.dtend  >= arg_start && e.dtend <= arg_end
        print_event(e, io)
      end
    rescue Exception => e
      handle_exception("Failed to process calendar event", e)
    end
  end
end