freeagent-cli 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +9 -0
- data/README.txt +10 -0
- data/Rakefile +4 -0
- data/exe/fa +80 -0
- data/lib/freeagent.rb +172 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6aa92b15fccb2ce8b889419652ee23e76455f9dea0f84afef73dbb2d33af023
|
4
|
+
data.tar.gz: 3329b71e37fcea5d277b3e05ba61972dd85f204c49bbe264568d555b15184d44
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cdfb521afa51bb920e46965da8e965b9bb5d32b1a659a91746fda0801b790f9cac07e563dd8f5caefca026e3df35bc2345974d47d4a85ff694a33fe5518c8818
|
7
|
+
data.tar.gz: 3c3a1be9095e29c94289320b3c471444166047fb5a766bb1daf88fa5ae83e35d22aed735363c5e07b17a81bb11ec28bd92f394cc47ab362fa2c3b3846346cfb6
|
data/LICENSE
ADDED
@@ -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.
|
data/README.txt
ADDED
@@ -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.
|
data/Rakefile
ADDED
data/exe/fa
ADDED
@@ -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
|
data/lib/freeagent.rb
ADDED
@@ -0,0 +1,172 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
require 'webrick'
|
3
|
+
|
4
|
+
FREEAGENT_APP_ID = ENV['FREEAGENT_APP_ID']
|
5
|
+
FREEAGENT_APP_SECRET = ENV['FREEAGENT_APP_SECRET']
|
6
|
+
|
7
|
+
TOKEN_FILE = './.token.yml'
|
8
|
+
|
9
|
+
module Freeagent
|
10
|
+
class API
|
11
|
+
def initialize
|
12
|
+
if FREEAGENT_APP_ID.nil? || FREEAGENT_APP_ID.empty?
|
13
|
+
raise ArgumentError, 'FREEAGENT_APP_ID is unset'
|
14
|
+
end
|
15
|
+
if FREEAGENT_APP_SECRET.nil? || FREEAGENT_APP_SECRET.empty?
|
16
|
+
raise ArgumentError, 'FREEAGENT_APP_SECRET is unset'
|
17
|
+
end
|
18
|
+
|
19
|
+
@token = reload || authorize
|
20
|
+
File.write TOKEN_FILE, @token.to_hash.to_hash.to_yaml
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def reload
|
25
|
+
if File.exist? TOKEN_FILE
|
26
|
+
OAuth2::AccessToken.from_hash(client, YAML.unsafe_load_file(TOKEN_FILE)).refresh
|
27
|
+
end
|
28
|
+
rescue OAuth2::Error
|
29
|
+
nil
|
30
|
+
end
|
31
|
+
|
32
|
+
def client
|
33
|
+
@client ||= OAuth2::Client.new(FREEAGENT_APP_ID, FREEAGENT_APP_SECRET,
|
34
|
+
site: 'https://api.freeagent.com/v2/',
|
35
|
+
authorize_url: 'approve_app',
|
36
|
+
token_url: 'token_endpoint'
|
37
|
+
)
|
38
|
+
end
|
39
|
+
|
40
|
+
def authorize
|
41
|
+
receiver = WEBrick::HTTPServer.new(Port: 0)
|
42
|
+
receiver_url = "http://localhost:#{receiver.config[:Port]}/"
|
43
|
+
url = client.auth_code.authorize_url(redirect_uri: receiver_url)
|
44
|
+
puts "Go and authorize at #{url}, I'm waiting..."
|
45
|
+
|
46
|
+
access_code = nil
|
47
|
+
receiver.mount_proc '/' do |req, res|
|
48
|
+
# TODO: parse redirect response...
|
49
|
+
access_code = req.query['code']
|
50
|
+
receiver.shutdown
|
51
|
+
end
|
52
|
+
|
53
|
+
Signal.trap('INT') do
|
54
|
+
receiver.shutdown
|
55
|
+
end
|
56
|
+
|
57
|
+
receiver.start
|
58
|
+
raise if access_code.nil?
|
59
|
+
|
60
|
+
client.auth_code.get_token(access_code, redirect_uri: receiver_url)
|
61
|
+
end
|
62
|
+
|
63
|
+
def get *parts, **params
|
64
|
+
relatives = parts.map(&:to_s).join('/')
|
65
|
+
uri = URI.parse(relatives)
|
66
|
+
puts "GET #{uri}"
|
67
|
+
res = @token.get(uri, params: params, headers: {'Accept': 'application/json'})
|
68
|
+
data = res.parsed
|
69
|
+
if res.headers.include? 'Link'
|
70
|
+
data._links = res.headers['Link'].split(", ").map do |link|
|
71
|
+
url, rel = link.split('; rel=')
|
72
|
+
[rel.gsub(/^'|'$/, ''), URI::parse(url.gsub(/^<|>$/, ''))]
|
73
|
+
end.to_h
|
74
|
+
end
|
75
|
+
data
|
76
|
+
rescue => err
|
77
|
+
if err.response.status == 429
|
78
|
+
retry_after = err.response.headers['retry-after'].to_i
|
79
|
+
STDERR.puts "Rate limited; sleeping for #{retry_after}"
|
80
|
+
sleep retry_after
|
81
|
+
retry
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def get_pages *path, **params
|
86
|
+
Enumerator.new do |yielder|
|
87
|
+
loop do
|
88
|
+
res = get(*path, **{per_page: 100}.update(params))
|
89
|
+
yielder << res
|
90
|
+
break if res._links.nil? || res._links.next.nil?
|
91
|
+
params = URI::decode_www_form(res._links.next.query).to_h
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def post *parts, **params
|
97
|
+
data = parts.pop
|
98
|
+
relatives = parts.map(&:to_s).join('/')
|
99
|
+
uri = URI.parse(relatives)
|
100
|
+
puts "POST #{uri}"
|
101
|
+
body = data.to_json
|
102
|
+
@token.post(uri, body: body, params: params, headers: {'Content-Type': 'application/json', 'Accept': 'application/json'}).parsed
|
103
|
+
end
|
104
|
+
|
105
|
+
def delete *parts, **params
|
106
|
+
relatives = parts.map(&:to_s).join('/')
|
107
|
+
uri = URI.parse(relatives)
|
108
|
+
puts "DELETE #{uri}"
|
109
|
+
@token.delete(uri, params: params, headers: {'Accept': 'application/json'})
|
110
|
+
end
|
111
|
+
|
112
|
+
public
|
113
|
+
def first_accounting_year_end
|
114
|
+
Date.parse get('company').company.first_accounting_year_end
|
115
|
+
end
|
116
|
+
|
117
|
+
def periods year
|
118
|
+
get('payroll', year).periods
|
119
|
+
end
|
120
|
+
|
121
|
+
def payslips year, period
|
122
|
+
get('payroll', year, period).period.payslips
|
123
|
+
end
|
124
|
+
|
125
|
+
def profiles year
|
126
|
+
get('payroll_profiles', year).profiles
|
127
|
+
end
|
128
|
+
|
129
|
+
def projects
|
130
|
+
get_pages('projects').flat_map(&:projects)
|
131
|
+
end
|
132
|
+
|
133
|
+
def tasks project
|
134
|
+
get_pages('tasks', project: project.url).flat_map(&:tasks)
|
135
|
+
end
|
136
|
+
|
137
|
+
def timeslips user, project, task, from, to
|
138
|
+
get_pages('timeslips', user: user.url, project: project.url, task: task.url, from_date: from.to_s, to_date: to.to_s).flat_map(&:timeslips)
|
139
|
+
end
|
140
|
+
|
141
|
+
def create_timeslip user, project, task, dated, hours
|
142
|
+
post('timeslips', {
|
143
|
+
'task' => task.url,
|
144
|
+
'project' => project.url,
|
145
|
+
'user' => user.url,
|
146
|
+
'dated_on' => dated.to_s,
|
147
|
+
'hours' => hours,
|
148
|
+
})
|
149
|
+
end
|
150
|
+
|
151
|
+
def batch_create_timeslips timeslips
|
152
|
+
post('timeslips', {'timeslips' => timeslips})
|
153
|
+
end
|
154
|
+
|
155
|
+
def delete_timeslip timeslip
|
156
|
+
id = timeslip.url.split('/').last
|
157
|
+
delete('timeslips', id)
|
158
|
+
end
|
159
|
+
|
160
|
+
def users
|
161
|
+
get_pages('users').flat_map(&:users)
|
162
|
+
end
|
163
|
+
|
164
|
+
def user id
|
165
|
+
get('users', id).user
|
166
|
+
end
|
167
|
+
|
168
|
+
def profile year, user
|
169
|
+
get('payroll_profiles', year, user: user)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: freeagent-cli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Worthington
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-05-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: oauth2
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: webrick
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13'
|
69
|
+
description: |-
|
70
|
+
This gem supplies a `fa` executable which can create, read, update and delete
|
71
|
+
data using the Freeagent API.
|
72
|
+
|
73
|
+
You need to specify valid Freeagent app credentials. Generate a Freeagent app at
|
74
|
+
https://dev.freeagent.com/apps. Ensure you set 'http://localhost:*/' as a valid
|
75
|
+
OAuth redirect URI. Then export your ID and secret as environment variables
|
76
|
+
FREEAGENT_APP_ID and FREEAGENT_APP_SECRET. On first run, `fa` will authenticate
|
77
|
+
you via OAuth so watch out for an authentication URI printed to the console.
|
78
|
+
email:
|
79
|
+
- simon@register-dynamics.co.uk
|
80
|
+
executables:
|
81
|
+
- fa
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- LICENSE
|
86
|
+
- README.txt
|
87
|
+
- Rakefile
|
88
|
+
- exe/fa
|
89
|
+
- lib/freeagent.rb
|
90
|
+
homepage: https://www.github.com/register-dynamics/freeagent-cli
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata:
|
94
|
+
homepage_uri: https://www.github.com/register-dynamics/freeagent-cli
|
95
|
+
source_code_uri: https://www.github.com/register-dynamics/freeagent-cli
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: 3.0.0
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubygems_version: 3.4.1
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: 'freeagent-cli: Access and control Freeagent data from the command line.'
|
115
|
+
test_files: []
|