lib/renuo/cli/app/toggl_redmine_comparator.rb in renuo-cli-4.5.0 vs lib/renuo/cli/app/toggl_redmine_comparator.rb in renuo-cli-4.5.1
- old
+ new
@@ -1,25 +1,25 @@
# frozen_string_literal: true
-require 'commander'
-require 'csv'
-require_relative 'environments'
-require_relative 'fetch_emails'
-require 'renuo/cli/app/toggl/workspace'
-require 'renuo/cli/app/toggl/detail'
-require 'renuo/cli/app/toggl/user'
-require 'terminal-table'
-require 'colorize'
-require 'active_support/core_ext/numeric/time'
+require "commander"
+require "csv"
+require_relative "environments"
+require_relative "fetch_emails"
+require "renuo/cli/app/toggl/workspace"
+require "renuo/cli/app/toggl/detail"
+require "renuo/cli/app/toggl/user"
+require "terminal-table"
+require "colorize"
+require "active_support/core_ext/numeric/time"
# rubocop:disable Metrics/ClassLength
class TogglRedmineComparator
class << self
def call(days_behind = 7)
report = {}
- since_date = days_behind.days.before(Date.yesterday).strftime('%F')
- until_date = Date.yesterday.strftime('%F')
+ since_date = days_behind.days.before(Date.yesterday).strftime("%F")
+ until_date = Date.yesterday.strftime("%F")
extract_redmine(report, since_date, until_date)
extract_toggl(report, since_date, until_date)
report = report.sort.reverse.to_h
print_table(report)
report
@@ -34,16 +34,16 @@
rows << :separator
end
rows.pop
table = Terminal::Table.new headings: %w[Day Redmine Toggl].map(&:cyan), rows:,
style: { padding_left: 2, padding_right: 2,
- border_x: '-'.blue, border_y: '|'.blue, border_i: '+'.blue }
+ border_x: "-".blue, border_y: "|".blue, border_i: "+".blue }
puts table
end
def colorize_table_row(date, value)
- printed_day = date.strftime('%F %a')
+ printed_day = date.strftime("%F %a")
printed_redmine = to_time(value[:redmine])
printed_toggl = to_time(value[:toggl])
colorize_method = colorization_for_value(value)
[printed_day, printed_redmine, printed_toggl].map { |v| v.colorize(colorize_method) }
end
@@ -66,44 +66,44 @@
report[Date.parse(date)][:redmine] = to_seconds(entry) if entry.present?
end
end
def convert_redmine_csv(encoded_body)
- separated_csv_entries = CSV.parse(encoded_body, col_sep: ',')
+ separated_csv_entries = CSV.parse(encoded_body, col_sep: ",")
keys = separated_csv_entries.shift[1..-2]
entries = separated_csv_entries.shift[1..-2]
keys.zip(entries)
end
def perform_redmine_call(since_date)
query = generate_redmine_query(since_date)
url = URI("https://redmine.renuo.ch/time_entries/report.csv?#{query}")
req = Net::HTTP::Get.new(url)
- req['X-Redmine-API-Key'] = RenuoCliConfig.redmine_api_key
+ req["X-Redmine-API-Key"] = RenuoCliConfig.redmine_api_key
response = Net::HTTP.start(url.hostname, url.port, use_ssl: true) { |http| http.request(req) }
- response.body.force_encoding('ISO-8859-1').encode('UTF-8')
+ response.body.force_encoding("ISO-8859-1").encode("UTF-8")
end
def generate_redmine_query(since_date)
URI.encode_www_form(
- [['utf8', '✓'], ['criteria[]', 'user'],
- ['f[]', 'spent_on'], ['f[]', 'user_id'],
- ['op[spent_on]', '>='], ['op[user_id]', '='],
- ['v[spent_on][]', since_date], ['v[user_id][]', 'me'],
- ['f[]', ''],
- ['c[]', 'project'], ['c[]', 'spent_on'], ['c[]', 'user'], ['c[]', 'activity'], ['c[]', 'issue'],
- ['c[]', 'comments'], ['c[]', 'hours'], %w[columns day], ['criteria[]', '']]
+ [["utf8", "✓"], ["criteria[]", "user"],
+ ["f[]", "spent_on"], ["f[]", "user_id"],
+ ["op[spent_on]", ">="], ["op[user_id]", "="],
+ ["v[spent_on][]", since_date], ["v[user_id][]", "me"],
+ ["f[]", ""],
+ ["c[]", "project"], ["c[]", "spent_on"], ["c[]", "user"], ["c[]", "activity"], ["c[]", "issue"],
+ ["c[]", "comments"], ["c[]", "hours"], %w[columns day], ["criteria[]", ""]]
)
end
def extract_toggl(report, since_date, until_date)
user_id = Toggl::User.me.id
workspace_ids = Toggl::Workspace.all.map(&:id)
workspace_ids.each do |workspace_id|
time_entries = Toggl::Detail.where(since: since_date, until: until_date,
- user_agent: 'renuo-cli', workspace_id:, user_ids: user_id)
+ user_agent: "renuo-cli", workspace_id:, user_ids: user_id)
parse_toggl_entries(report, time_entries)
end
end
def parse_toggl_entries(report, time_entries)
@@ -122,10 +122,10 @@
def to_time(value)
sec = value / 1000.0
min, _sec = sec.divmod(60.0)
hour, min = min.divmod(60.0)
- format('%<hour>02d:%<min>02d', hour:, min:)
+ format("%<hour>02d:%<min>02d", hour:, min:)
end
def to_seconds(value)
hours, minutes = value.to_d.divmod(1.0)
(hours * 60 * 60 * 1000) + (minutes * 60 * 60 * 1000)