Module: Sfctl::Toggl::Sync

Defined in:
lib/sfctl/toggl/sync.rb

Class Method Summary collapse

Class Method Details

.assignment_items(time_entries) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/sfctl/toggl/sync.rb', line 105

def self.assignment_items(time_entries)
  time_entries.map do |te|
    milliseconds = te['dur'] || 0
    {
      time_seconds: milliseconds.div(1000),
      date: Date.parse(te['start']).to_s,
      comment: te['description'],
      external_id: te['id'].to_s
    }
  end
end

.get_time_entries(connection, toggl_config, report_interval) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sfctl/toggl/sync.rb', line 44

def self.get_time_entries(connection, toggl_config, report_interval)
  entries_list = []
  error = nil

  page = 1
  loop do
    success, body = Toggl::Client.time_entries(
      toggl_config['access_token'],
      time_entries_params(connection, report_interval, page)
    )

    unless success
      error = body.fetch('message', body)
      break
    end

    entries_list << body['data']
    entries_list.flatten!
    entries_list.compact!

    break if entries_list.length >= body['total_count']

    page += 1
  end

  [entries_list, error]
end

.humanize_duration(milliseconds) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sfctl/toggl/sync.rb', line 87

def self.humanize_duration(milliseconds)
  return '0' if milliseconds.nil?

  seconds = milliseconds / 1000
  minutes = seconds / 60
  int = (minutes / 60).ceil
  dec = minutes % 60
  amount = (dec * 100) / 60
  amount = if dec.zero?
             ''
           elsif amount.to_s.length == 1
             ".0#{amount}"
           else
             ".#{amount}"
           end
  "#{int}#{amount}"
end

.load_data(output, connection, toggl_config, pastel, report_interval) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sfctl/toggl/sync.rb', line 9

def self.load_data(output, connection, toggl_config, pastel, report_interval)
  spinner = TTY::Spinner.new("Loaded data from #{Sfctl::Command::TOGGL_PROVIDER}: [:spinner]", format: :dots)
  spinner.auto_spin

  time_entries, error = get_time_entries(connection, toggl_config, report_interval)

  if error
    spinner.error
    output.puts pastel.red(error)
  else
    spinner.success(pastel.green('Done'))
  end

  table = TTY::Table.new %w[Date Comment Time], time_entries_table_rows(time_entries)
  output.puts
  output.print table.render(:unicode, padding: [0, 1], alignments: %i[left left right])
  output.puts
  output.puts

  time_entries
end

.time_entries_params(connection, report_interval, page = 1) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sfctl/toggl/sync.rb', line 72

def self.time_entries_params(connection, report_interval, page = 1)
  start_date, end_date = report_interval
  params = {
    workspace_id: connection['workspace_id'],
    project_ids: connection['project_ids'],
    billable: connection['billable'],
    rounding: connection['rounding'],
    since: start_date.to_s,
    until: end_date.to_s,
    page: page
  }
  params[:task_ids] = connection['task_ids'] if connection['task_ids'].length.positive?
  params
end

.time_entries_table_rows(time_entries) ⇒ Object



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

def self.time_entries_table_rows(time_entries)
  rows = time_entries.sort_by { |te| te['start'] }.map do |te|
    [
      Date.parse(te['start']).to_s,
      te['description'],
      "#{humanize_duration(te['dur'])}h"
    ]
  end
  total_grand = time_entries.sum { |te| te['dur'] }
  rows.push(['Total:', '', "#{humanize_duration(total_grand)}h"])
  rows
end