Class: PrintableCalendar::PrintableCalendar

Inherits:
Object
  • Object
show all
Defined in:
lib/printable_calendar.rb

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ PrintableCalendar

Returns a new instance of PrintableCalendar.


11
12
13
# File 'lib/printable_calendar.rb', line 11

def initialize(settings)
  @settings = settings
end

Instance Method Details

#calendar_idsObject


39
40
41
# File 'lib/printable_calendar.rb', line 39

def calendar_ids
  @settings[:calendar_ids] || abort("No calendars specified")
end

#client_idObject


27
28
29
# File 'lib/printable_calendar.rb', line 27

def client_id
  @settings[:client_id] || ENV["PRINTABLE_CALENDAR_CLIENT_ID"] || abort("No Google client ID specified")
end

#client_secretObject


31
32
33
# File 'lib/printable_calendar.rb', line 31

def client_secret
  @settings[:client_secret] || ENV["PRINTABLE_CALENDAR_CLIENT_SECRET"] || abort("No Google client secret specified")
end

#generate_authObject


15
16
17
# File 'lib/printable_calendar.rb', line 15

def generate_auth
  Calendar.new(client_id, client_secret).auth
end

#launch_file(path) ⇒ Object


63
64
65
66
67
68
69
# File 'lib/printable_calendar.rb', line 63

def launch_file(path)
  if path
    Launchy.open("file://#{path}")
  else
    abort("Failed to generate file")
  end
end

#refresh_tokenObject


35
36
37
# File 'lib/printable_calendar.rb', line 35

def refresh_token
  @settings[:refresh_token] || ENV["PRINTABLE_CALENDAR_REFRESH_TOKEN"] || abort("No Google refresh token found")
end

#runObject


19
20
21
22
23
24
25
# File 'lib/printable_calendar.rb', line 19

def run
  starts, ends = Range.compute(@settings[:period] || "work_week", @settings[:starting_from] || Date.today)
  data = Calendar.new(client_id, client_secret).fetch(starts: starts, ends: ends, refresh_token: refresh_token, calendar_ids: calendar_ids)
  html = View.new(@settings, starts, ends, data).to_html
  tempfile = write_tempfile(html)
  launch_file(tempfile)
end

#write_tempfile(html) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/printable_calendar.rb', line 43

def write_tempfile(html)
  #there has to be a better way to do this
  path = nil
  begin
    temp = Tempfile.new("printable_calendar")
    path = temp.path
    temp.close
    temp.unlink

    File.open(path, 'w'){|f| f.write(html)}
    path
  ensure
    temp.close
    temp.unlink
  end

  path

end