Sha256: 8917e69faa00b9379f2fa525302b4806d9a706d56e5b45a66fc69351221c9dc3

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'nokogiri'
require 'net/http'
require 'uri'

module LotusNotesCalendar
  class Calendar
    attr_reader :url

    def initialize(url)
      @url = url
      @results = {}
    end

    def find(id)
      url = "#{@url}/#{id}?OpenDocument"
      return @results[url] if @results.key?(url)
      doc = Nokogiri::HTML(open_url(url))
      event = Event.from_html(self, id, doc)
      @results[url] = event
      event
    end

    def all
      where
    end

    def where(options = {})
      url = build_url(options)
      return @results[url] if @results.key?(url)
      doc = Nokogiri::XML(open_url(url))
      events = []
      doc.xpath('//viewentries/viewentry').each do |event_xml|
        next if event_xml.has_attribute?('conflict') and event_xml['conflict'] == 'true'
        events << Event.from_xml(self, event_xml)
      end
      @results[url] = events
      return events
    end

    private

      def build_url(options = {})
        url = "#{@url}?ReadViewEntries&PreFormat"
        if options[:start] and options[:end]
          url += '&KeyType=time'
          url += "&StartKey=#{options[:start]}"
          url += "&UntilKey=#{options[:end]}"
          url += '&Count=999'
        elsif options[:date]
          url += "&Date=#{options[:date]}"
        end
        url
      end

      def open_url(url)
        uri = URI.parse(url)
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = uri.is_a? URI::HTTPS
        # http.ca_file = "/etc/ssl/certs/ca-certificate.crt"
        request = Net::HTTP::Get.new(uri.request_uri)
        # puts uri.request_uri
        http.request(request).body
      rescue Errno::ETIMEDOUT
        ''
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lotus_notes_calendar-0.0.2 lib/lotus_notes_calendar/calendar.rb