class Toggl::Worktime::Driver

Toggle API driver

Constants

ONE_DAY_SECONDS

Attributes

toggl[R]
work_time[R]

Public Class Methods

new(config:) click to toggle source
# File lib/toggl/worktime/driver.rb, line 12
def initialize(config:)
  @toggl = TogglV8::API.new
  @config = config
  @merger = nil
  @work_time = nil
  @zone_offset = Toggl::Worktime::Time.zone_offset(@config.timezone)
  @calendar = nil
end

Public Instance Methods

calendar(week_begin, year, month) click to toggle source
# File lib/toggl/worktime/driver.rb, line 21
def calendar(week_begin, year, month)
  @calendar = Toggl::Worktime::Calendar.new(self, @zone_offset, week_begin, year, month)
end
filter_entries(entries) click to toggle source

time_entries filter with @config.ignore_conditions

# File lib/toggl/worktime/driver.rb, line 36
def filter_entries(entries)
  pass_l = lambda { |entry|
    @config.ignore_conditions.none? do |cond|
      cond.keys.all? do |key|
        case key
        when 'tags'
          entry['tags']&.any? { |t| cond[key].include?(t) }
        end
      end
    end
  }
  entries.select { |e| pass_l.call(e) }
end
me() click to toggle source
# File lib/toggl/worktime/driver.rb, line 50
def me
  @toggl.me(true)
end
merge!(year, month, day) click to toggle source
# File lib/toggl/worktime/driver.rb, line 54
def merge!(year, month, day)
  time_entries = time_entries(year, month, day)
  time_entries = filter_entries(time_entries)
  @merger = Toggl::Worktime::Merger.new(time_entries, @config)
  @work_time = @merger.merge
end
time_entries(year, month, day) click to toggle source
# File lib/toggl/worktime/driver.rb, line 25
def time_entries(year, month, day)
  beginning_day = ::Time.new(
    year, month, day, @config.day_begin_hour, 0, 0, @zone_offset
  )
  ending_day = beginning_day + ONE_DAY_SECONDS
  start_iso = beginning_day.strftime('%FT%T%:z')
  end_iso = ending_day.strftime('%FT%T%:z')
  toggl.get_time_entries(start_date: start_iso, end_date: end_iso)
end
time_expr(time) click to toggle source
# File lib/toggl/worktime/driver.rb, line 69
def time_expr(time)
  time ? time.getlocal(@zone_offset).strftime('%F %T') : 'nil'
end
total_time() click to toggle source
# File lib/toggl/worktime/driver.rb, line 73
def total_time
  total_seconds = @merger.total_time.to_i
  hours = total_seconds / (60 * 60)
  minutes = (total_seconds - (hours * 60 * 60)) / 60
  seconds = total_seconds % 60
  format('%02d:%02d:%02d', hours, minutes, seconds)
end
write() click to toggle source
# File lib/toggl/worktime/driver.rb, line 61
def write
  @work_time.each do |span|
    begin_s = time_expr(span[0])
    end_s = time_expr(span[1])
    puts "#{begin_s} - #{end_s}"
  end
end