Skip to content

Commit 2020a8b

Browse files
committed
Basic gem for controlling timesleeps on Freeagent.
0 parents  commit 2020a8b

File tree

10 files changed

+404
-0
lines changed

10 files changed

+404
-0
lines changed

.github/workflows/main.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Ruby
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
pull_request:
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
name: Ruby ${{ matrix.ruby }}
14+
strategy:
15+
matrix:
16+
ruby:
17+
- '3.2.0'
18+
19+
steps:
20+
- uses: actions/checkout@v4
21+
- name: Set up Ruby
22+
uses: ruby/setup-ruby@v1
23+
with:
24+
ruby-version: ${{ matrix.ruby }}
25+
bundler-cache: true
26+
- name: Run the default task
27+
run: bundle exec rake

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.env
2+
.token.yml
3+
*.gem

Gemfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec

Gemfile.lock

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
PATH
2+
remote: .
3+
specs:
4+
freeagent-cli (0.1)
5+
oauth2 (~> 2)
6+
thor (~> 1.3)
7+
webrick (~> 1.9)
8+
9+
GEM
10+
remote: https://rubygems.org/
11+
specs:
12+
base64 (0.2.0)
13+
bigdecimal (3.1.9)
14+
faraday (2.12.2)
15+
faraday-net_http (>= 2.0, < 3.5)
16+
json
17+
logger
18+
faraday-net_http (3.4.0)
19+
net-http (>= 0.5.0)
20+
hashie (5.0.0)
21+
json (2.10.1)
22+
jwt (2.10.1)
23+
base64
24+
logger (1.6.6)
25+
multi_xml (0.7.1)
26+
bigdecimal (~> 3.1)
27+
net-http (0.6.0)
28+
uri
29+
oauth2 (2.0.9)
30+
faraday (>= 0.17.3, < 3.0)
31+
jwt (>= 1.0, < 3.0)
32+
multi_xml (~> 0.5)
33+
rack (>= 1.2, < 4)
34+
snaky_hash (~> 2.0)
35+
version_gem (~> 1.1)
36+
rack (3.1.10)
37+
rake (13.2.1)
38+
snaky_hash (2.0.1)
39+
hashie
40+
version_gem (~> 1.1, >= 1.1.1)
41+
thor (1.3.2)
42+
uri (1.0.2)
43+
version_gem (1.1.4)
44+
webrick (1.9.1)
45+
46+
PLATFORMS
47+
arm64-darwin-22
48+
ruby
49+
50+
DEPENDENCIES
51+
freeagent-cli!
52+
rake (~> 13)
53+
54+
BUNDLED WITH
55+
2.5.22

LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright © 2025 Register Dynamics Limited.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
freeagent-cli: Access and control Freeagent data from the command line.
2+
3+
This gem supplies a `fa` executable which can create, read, update and delete
4+
data using the Freeagent API.
5+
6+
You need to specify valid Freeagent app credentials. Generate a Freeagent app at
7+
https://dev.freeagent.com/apps. Ensure you set 'http://localhost:*/' as a valid
8+
OAuth redirect URI. Then export your ID and secret as environment variables
9+
FREEAGENT_APP_ID and FREEAGENT_APP_SECRET. On first run, `fa` will authenticate
10+
you via OAuth so watch out for an authentication URI printed to the console.

Rakefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# frozen_string_literal: true
2+
3+
require "bundler/gem_tasks"
4+
task default: %i[]

exe/fa

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'thor'
4+
require_relative '../lib/freeagent'
5+
6+
def match type, collection, needle, *values
7+
found = collection.select do |item|
8+
values.map(&:to_proc).any? {|v| v.call(item) == needle }
9+
end
10+
if found.none?
11+
possibles = collection.product(values).map {|item, value| value.to_proc.call(item).to_s }
12+
raise ArgumentError, "No #{type} matching '#{needle}' found, expecting one from #{possibles}"
13+
end
14+
unless found.one?
15+
raise ArgumentError, "Ambiguous #{type}: could be #{found.map(&:url).join(', ')}"
16+
end
17+
found.first
18+
end
19+
20+
module Freeagent
21+
class Timeslips < Thor
22+
no_commands do
23+
def api
24+
@api ||= API.new
25+
end
26+
end
27+
28+
desc 'list USER PROJECT TASK FROM TO', "List timeslips"
29+
def list user, project, task, from, to
30+
from = Date::parse from
31+
to = Date::parse to
32+
user = match :user, api.users, user, :first_name, :last_name, :email
33+
project = match :project, api.projects, project, :name
34+
task = match :task, api.tasks(project), task, :name
35+
36+
api.timeslips(user, project, task, from, to).each do |timeslip|
37+
puts [Date::parse(timeslip.dated_on).strftime('%a %d %b %Y'), user.email, project.name, task.name, timeslip.hours].join("\t")
38+
end
39+
end
40+
41+
desc 'create USER PROJECT TASK FROM TO', "Create timeslips"
42+
method_option :hours, aliases: '-h', type: :numeric, default: 8, desc: "Number of hours per day"
43+
method_option :weekends, aliases: '-w', type: :boolean, default: false, desc: "Create timeslips on Saturdays and Sundays"
44+
def create user, project, task, from, to
45+
from = Date::parse from
46+
to = Date::parse to
47+
user = match :user, api.users, user, :first_name, :last_name, :email
48+
project = match :project, api.projects, project, :name
49+
task = match :task, api.tasks(project), task, :name
50+
51+
puts "Creating timeslips for #{user.first_name} #{user.last_name} for task '#{task.name}' in project '#{project.name}' between #{from} and #{to} inclusive"
52+
timeslips = from.step(to).map do |date|
53+
next if (date.saturday? || date.sunday?) && !options[:weekends]
54+
{'task' => task.url, 'project' => project.url, 'user' => user.url, 'dated_on' => date.to_s, 'hours' => options[:hours]}
55+
end.reject(&:nil?)
56+
api.batch_create_timeslips timeslips
57+
end
58+
59+
desc 'delete USER PROJECT TASK FROM TO', 'Delete timeslips'
60+
def delete user, project, task, from, to
61+
from = Date::parse from
62+
to = Date::parse to
63+
user = match :user, api.users, user, :first_name, :last_name, :email
64+
project = match :project, api.projects, project, :name
65+
task = match :task, api.tasks(project), task, :name
66+
67+
puts "Deleting timeslips for #{user.first_name} #{user.last_name} for task '#{task.name}' in project '#{project.name}' between #{from} and #{to} inclusive"
68+
api.timeslips(user, project, task, from, to).each do |timeslip|
69+
api.delete_timeslip(timeslip)
70+
end
71+
end
72+
end
73+
74+
class Command < Thor
75+
desc "timeslips", "Create, read, update and delete timeslips"
76+
subcommand "timeslips", Timeslips
77+
end
78+
end
79+
80+
Freeagent::Command.start

freeagent-cli.gemspec

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
Gem::Specification.new do |spec|
4+
spec.name = 'freeagent-cli'
5+
spec.version = '0.1'
6+
spec.authors = ['Simon Worthington']
7+
spec.email = ['simon@register-dynamics.co.uk']
8+
9+
readme = File.read('README.txt').split("\n")
10+
spec.summary = readme.first
11+
spec.description = readme[1..].join("\n").strip
12+
spec.homepage = 'https://www.github.com/register-dynamics/freeagent-cli'
13+
spec.required_ruby_version = '>= 3.0.0'
14+
15+
spec.metadata['homepage_uri'] = spec.homepage
16+
spec.metadata['source_code_uri'] = spec.homepage
17+
spec.license = 'MIT'
18+
19+
# Specify which files should be added to the gem when it is released.
20+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21+
gemspec = File.basename(__FILE__)
22+
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
23+
ls.readlines("\x0", chomp: true).reject do |f|
24+
(f == gemspec) ||
25+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
26+
end
27+
end
28+
spec.bindir = 'exe'
29+
spec.executables = spec.files.grep(%r{\A#{spec.bindir}/}) { |f| File.basename(f) }
30+
spec.require_paths = ['lib']
31+
32+
# Uncomment to register a new dependency of your gem
33+
spec.add_dependency 'oauth2', '~> 2'
34+
spec.add_dependency 'webrick', '~> 1.9'
35+
spec.add_dependency 'thor', '~> 1.3'
36+
37+
spec.add_development_dependency 'rake', '~> 13'
38+
39+
# For more information and examples about making a new gem, check out our
40+
# guide at: https://bundler.io/guides/creating_gem.html
41+
end

0 commit comments

Comments
 (0)