class TerminalCal::Calendar

Constants

DATE_FMT
TIME_FMT

Attributes

cache_life_minutes[R]
name[R]
source[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/terminal_cal/calendar.rb, line 8
def initialize(options = {})
  @config = options[:config]
  @parser = TerminalCal::CalendarParserFactory.new(options[:parser]).parser

  @source = @config[:source]
  @name   = @config[:name].to_s
  @cache  = @config[:cache] || true
  @cache_life_minutes = @config[:cache_life_minutes] || 10
  @now    = Time.now
  @today  = @now.strftime(DATE_FMT)

  @content = ''
end

Public Instance Methods

cache!(content) click to toggle source
# File lib/terminal_cal/calendar.rb, line 66
def cache!(content)
  File.open(cache_path, 'w+') do |file|
    file.write(content)
  end
end
cache?() click to toggle source
# File lib/terminal_cal/calendar.rb, line 22
def cache?
  return false unless [true, false].include?(@cache)
  @cache
end
cache_path() click to toggle source
# File lib/terminal_cal/calendar.rb, line 61
def cache_path
  filename = "#{@config[:name]}-cached-.ics".downcase
  File.join(TerminalCal::Config::APP_DIR, filename)
end
events() click to toggle source
# File lib/terminal_cal/calendar.rb, line 33
def events
  content = 
    if update_cache?
      fetch_events_from_source
    else
      fetch_events_from_cache
    end

  @parser.events(content)
end
fetch_events_from_cache() click to toggle source
# File lib/terminal_cal/calendar.rb, line 57
def fetch_events_from_cache
  File.read(cache_path)
end
fetch_events_from_source() click to toggle source
# File lib/terminal_cal/calendar.rb, line 44
def fetch_events_from_source
  source = @config[:source]
  content = open(source) { |f| f.read } 

  if cache?
    cache!(content)
  end

  content
rescue ::SocketError => error
  raise TerminalCal::Errors::Error, error.message
end
todays_events() click to toggle source
# File lib/terminal_cal/calendar.rb, line 72
def todays_events
  events.sort_by { |e| e.starts_at.to_i }.map do |event|
    next unless event.starts_at.strftime(DATE_FMT) == @today
    next if event.ends_at < @now

    Event.new(date: event.starts_at.strftime(DATE_FMT),
              start_time: event.starts_at.strftime(TIME_FMT),
              end_time: event.ends_at.strftime(TIME_FMT),
              summary: event.summary)
  end.compact
end
update_cache?() click to toggle source
# File lib/terminal_cal/calendar.rb, line 27
def update_cache?
  return false unless cache?
  return true unless File.exist?(cache_path)
  (Time.now - File.atime(cache_path)) > (@cache_life_minutes * 60)
end