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
19
# File 'lib/toggl/worktime/driver.rb', line 12

def initialize(config:)
  @toggl = TogglV9::API.new
  @config = config
  @merger = nil
  @work_time = nil
  @zone_offset = Toggl::Worktime::Time.zone_offset(@config.timezone)
  @calendar = nil
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

#calendar(week_begin, year, month) ⇒ Object



21
22
23
# 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) ⇒ Object

time_entries filter with @config.ignore_conditions



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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) }
        when 'project_id'
          cond[key].include?(entry['project_id'])
        end
      end
    end
  }
  entries.select { |e| pass_l.call(e) }
end

#meObject



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

def me
  @toggl.me(true)
end

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



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

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



25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



71
72
73
# File 'lib/toggl/worktime/driver.rb', line 71

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

#total_timeObject



75
76
77
78
79
80
81
# File 'lib/toggl/worktime/driver.rb', line 75

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



63
64
65
66
67
68
69
# File 'lib/toggl/worktime/driver.rb', line 63

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