class Luxafor::Toggl::Client

Attributes

luxafor[R]
state_file[R]
toggl[R]

Public Class Methods

new(toggl: Luxafor::Toggl.toggl_client, luxafor: Luxafor::Toggl.luxafor_client, state: Luxafor::Toggl::state_file) click to toggle source
# File lib/luxafor/toggl/client.rb, line 4
def initialize(toggl: Luxafor::Toggl.toggl_client, luxafor: Luxafor::Toggl.luxafor_client, state: Luxafor::Toggl::state_file)
  @toggl      = toggl
  @luxafor    = luxafor
  @state_file = state
end

Public Instance Methods

execute!() click to toggle source
# File lib/luxafor/toggl/client.rb, line 10
def execute!
  return false unless current_state.sufficiently_different_from?(stored_state)

  luxafor.on(current_state.colour)

  current_state.store!(state_file)

  true
end

Private Instance Methods

current_state() click to toggle source
# File lib/luxafor/toggl/client.rb, line 29
def current_state
  @_current_state ||= State.new_from_task(latest_task)
end
latest_task() click to toggle source
# File lib/luxafor/toggl/client.rb, line 24
def latest_task
  start         = DateTime.now - 1
  @_latest_task = toggl.get_time_entries(start_date: start).last
end
stored_state() click to toggle source
# File lib/luxafor/toggl/client.rb, line 33
def stored_state
  return State.new unless File.exist?(state_file)

  file_contents = File.new(state_file).read.to_s
  state_json    = Oj.load(file_contents)

  State.new(state: state_json['state'], as_of: state_json['as_of'])
end