Class: Toggl::Worktime::Driver

Inherits:
Object
  • Object
show all
Defined in:
lib/toggl/worktime/driver.rb

Overview

Toggle API driver

Constant Summary collapse

ONE_DAY_SECONDS =
86_400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Driver

Returns a new instance of Driver.



12
13
14
15
16
17
18
# 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)
end

Instance Attribute Details

#togglObject (readonly)

Returns the value of attribute toggl.



9
10
11
# File 'lib/toggl/worktime/driver.rb', line 9

def toggl
  @toggl
end

#work_timeObject (readonly)

Returns the value of attribute work_time.



10
11
12
# File 'lib/toggl/worktime/driver.rb', line 10

def work_time
  @work_time
end

Instance Method Details

#filter_entries(entries) ⇒ Object

time_entries filter with @config.ignore_conditions



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/toggl/worktime/driver.rb', line 31

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

#meObject



45
46
47
# File 'lib/toggl/worktime/driver.rb', line 45

def me
  @toggl.me(true)
end

#merge!(year, month, day) ⇒ Object



49
50
51
52
53
54
# File 'lib/toggl/worktime/driver.rb', line 49

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) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/toggl/worktime/driver.rb', line 20

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) ⇒ Object



64
65
66
# File 'lib/toggl/worktime/driver.rb', line 64

def time_expr(time)
  time ? time.getlocal(@zone_offset).strftime('%F %T') : 'nil'
end

#total_timeObject



68
69
70
71
72
73
74
# File 'lib/toggl/worktime/driver.rb', line 68

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

#writeObject



56
57
58
59
60
61
62
# File 'lib/toggl/worktime/driver.rb', line 56

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