class Toggl::Worktime::Merger

Time-entries merger

Constants

ONE_MINUTE_SECONDS

Attributes

total_time[R]

Public Class Methods

new(time_entries, config) click to toggle source
# File lib/toggl/worktime/merger.rb, line 13
def initialize(time_entries, config)
  @time_entries = time_entries
  @config = config
  @current_start = nil
  @current_stop = nil
  @continuing = true
  @last_stop = nil
  @total_time = 0
end

Public Instance Methods

continuing(start) click to toggle source
# File lib/toggl/worktime/merger.rb, line 67
def continuing(start)
  return true if @current_stop.nil?

  interval = (start - @current_stop) / ONE_MINUTE_SECONDS
  @continuing = interval < @config.working_interval_min
end
count_total_time() click to toggle source
# File lib/toggl/worktime/merger.rb, line 40
def count_total_time
  return if @current_start.nil? || @current_stop.nil?

  @total_time += @current_stop - @current_start
end
merge() click to toggle source
# File lib/toggl/worktime/merger.rb, line 23
def merge
  work_time = []
  time_entries_each do |start, stop|
    if continuing(start)
      @current_stop = stop
      next
    end
    work_time << [@current_start, @current_stop]
    count_total_time
    @current_start = start
    @current_stop = stop
  end
  work_time << [@current_start, @last_stop]
  count_total_time
  work_time
end
parse_date(date, zone_offset) click to toggle source
# File lib/toggl/worktime/merger.rb, line 61
def parse_date(date, zone_offset)
  return nil if date.nil?

  ::Time.parse(date).getlocal(zone_offset)
end
time_entries_each() { |start, stop| ... } click to toggle source
# File lib/toggl/worktime/merger.rb, line 46
def time_entries_each
  zone_offset = Toggl::Worktime::Time.zone_offset(@config.timezone)
  @time_entries.each do |te|
    start = parse_date(te['start'], zone_offset)
    stop = parse_date(te['stop'], zone_offset)
    @last_stop = stop
    @current_start = start if @current_start.nil?
    @current_stop = stop if @current_stop.nil?
    if start.nil? || stop.nil?
      warn 'start or stop time is nil: total time may be incomplete'
    end
    yield [start, stop]
  end
end