Class: TerminalCal::Calendar
- Inherits:
-
Object
- Object
- TerminalCal::Calendar
- Defined in:
- lib/terminal_cal/calendar.rb
Constant Summary collapse
- DATE_FMT =
'%m/%d'.freeze
- TIME_FMT =
'%I:%M %p'.freeze
Instance Attribute Summary collapse
-
#cache_life_minutes ⇒ Object
readonly
Returns the value of attribute cache_life_minutes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #cache!(content) ⇒ Object
- #cache? ⇒ Boolean
- #cache_path ⇒ Object
- #events ⇒ Object
- #fetch_events_from_cache ⇒ Object
- #fetch_events_from_source ⇒ Object
-
#initialize(options = {}) ⇒ Calendar
constructor
A new instance of Calendar.
- #todays_events ⇒ Object
- #update_cache? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Calendar
Returns a new instance of Calendar.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/terminal_cal/calendar.rb', line 8 def initialize( = {}) @config = [:config] @parser = TerminalCal::CalendarParserFactory.new([: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 |
Instance Attribute Details
#cache_life_minutes ⇒ Object (readonly)
Returns the value of attribute cache_life_minutes.
6 7 8 |
# File 'lib/terminal_cal/calendar.rb', line 6 def cache_life_minutes @cache_life_minutes end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/terminal_cal/calendar.rb', line 6 def name @name end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/terminal_cal/calendar.rb', line 6 def source @source end |
Instance Method Details
#cache!(content) ⇒ Object
66 67 68 69 70 |
# File 'lib/terminal_cal/calendar.rb', line 66 def cache!(content) File.open(cache_path, 'w+') do |file| file.write(content) end end |
#cache? ⇒ Boolean
22 23 24 25 |
# File 'lib/terminal_cal/calendar.rb', line 22 def cache? return false unless [true, false].include?(@cache) @cache end |
#cache_path ⇒ Object
61 62 63 64 |
# 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 ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/terminal_cal/calendar.rb', line 57 def fetch_events_from_cache File.read(cache_path) end |
#fetch_events_from_source ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# 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. end |
#todays_events ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 |
# 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? ⇒ Boolean
27 28 29 30 31 |
# 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 |