Skip to content

Instantly share code, notes, and snippets.

@amkisko
Last active October 15, 2023 08:44
Show Gist options
  • Save amkisko/1f29d99338daae74fb4a599d5fc26721 to your computer and use it in GitHub Desktop.
Save amkisko/1f29d99338daae74fb4a599d5fc26721 to your computer and use it in GitHub Desktop.
Noko entries importer
# AUTHOR: Andrei Makarov (github.com/amkisko)
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "noko"
gem "dotenv"
gem "active_support"
end
require "noko"
require "dotenv/load"
require "active_support/core_ext/date_time"
class FileDoesNotExist < StandardError; end
class Client
def initialize(dry_run: false)
@dry_run = dry_run
end
private def noko
@noko ||= Noko::Client.new(token: ENV.fetch("NOKO_API_TOKEN"))
end
def create_entry(date:, project_name:, minutes:, description:)
puts "#{date} | #{minutes} | #{project_name} | #{description}"
return if @dry_run
noko.create_entry({
date:,
project_name:,
minutes:,
description:
})
end
end
cli = Client.new(dry_run: false)
filename = ARGV.fetch(0, "")
if !File.exists?(filename)
raise FileDoesNotExist.new(filename)
end
date = nil
File.readlines(filename).each do |raw_value|
value = raw_value.strip
next if value.blank?
if value.match(/^[0-9]{4}\-[0-9]{2}\-[0-9]{2}$/)
date = Date.parse(value)
else
time, project_name, description = value.scan(/([^;]+);([^;]+);(.+)/)&.first
minutes = time.split(":").reverse.each_with_index.sum{_1.to_i*60**_2}
cli.create_entry(date:, minutes:, project_name:, description:)
end
end
2022-08-19
60;My project;Changes applied
2:30;My project;Release iteration
2022-08-20
90;My project;More changes applied
4:30;My project;Meeting with investor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment