Sha256: d23c9363eb196e5fd9c8242a4c12da7dfc594e398c8aa060c857e0eed4d50321

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module Luxafor
  module Toggl
    class Client
      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

      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

      attr_reader :toggl, :luxafor, :state_file

      def latest_task
        start           = DateTime.now - 1
        @_latest_task ||= toggl.get_time_entries(start_date: start).max_by { |task| Time.parse(task["start"]) }
      end

      def current_state
        @_current_state ||= State.new_from_task(latest_task)
      end

      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
luxafor-toggl-1.0.3 lib/luxafor/toggl/client.rb