harvest-ruby-v2 0.3.1 → 0.5.0
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 +4 -4
- data/CHANGELOG.rst +8 -0
- data/Gemfile +1 -0
- data/Rakefile +2 -0
- data/bin/console +1 -1
- data/lib/harvest.rb +27 -13
- data/lib/harvest/changers.rb +27 -0
- data/lib/harvest/creates.rb +2 -2
- data/lib/harvest/discovers.rb +3 -0
- data/lib/harvest/finders.rb +1 -1
- data/lib/harvest/httpclient.rb +42 -20
- data/lib/harvest/resourcefactory.rb +6 -1
- data/lib/harvest/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f341c06dfb732eeb0ab69f8a03d2d3a1d183c3afa1e200c14dba0cfe9e4bc81c
|
4
|
+
data.tar.gz: b10cb0684fb74ebb9a8e0dc87875b617a7b3c77af99ca5ed2dd75978c6fa7cf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bddbbd22c42beb25c7877adc7241b4d2feb61961c0f3ba61c5b774f34c9caf69a87239703a2e3eca0d7906a2b90e648b598492cc149c8d0752804d7f0bc58dd6
|
7
|
+
data.tar.gz: 8bdd8d98251fa443f0fbaf0b6665de237f64131aab1cc6d207c1305912f6891831bdd5eb8bf706e6d22696737209e9fccf158e8a9c3ba5625924254d5f8de086
|
data/CHANGELOG.rst
CHANGED
data/Gemfile
CHANGED
@@ -8,6 +8,7 @@ gemspec
|
|
8
8
|
gem 'activesupport', '~> 6.0'
|
9
9
|
gem 'configparser', '~> 0.1.7', group: :development
|
10
10
|
gem 'pry', '~> 0.13.1', group: :development
|
11
|
+
gem 'pry-stack_explorer', '~> 0.5.1', group: :development
|
11
12
|
gem 'rake', '~> 12.0', group: :development
|
12
13
|
gem 'redcarpet', '~> 3.5', group: :development
|
13
14
|
gem 'reek', '~> 6.0', group: :development
|
data/Rakefile
CHANGED
@@ -9,6 +9,8 @@ RSpec::Core::RakeTask.new(:spec)
|
|
9
9
|
RuboCop::RakeTask.new(:spec) do |rubocop|
|
10
10
|
rubocop.options << '--auto-correct'
|
11
11
|
rubocop.options << '--display-cop-names'
|
12
|
+
rubocop.options << '--extra-details'
|
13
|
+
rubocop.options << '--display-style-guide'
|
12
14
|
end
|
13
15
|
|
14
16
|
YARD::Rake::YardocTask.new do |t|
|
data/bin/console
CHANGED
data/lib/harvest.rb
CHANGED
@@ -11,6 +11,7 @@ require 'harvest/exceptions'
|
|
11
11
|
require 'harvest/finders'
|
12
12
|
require 'harvest/discovers'
|
13
13
|
require 'harvest/creates'
|
14
|
+
require 'harvest/changers'
|
14
15
|
|
15
16
|
# Conform to naming pattern of Finder, Discover, Creators.
|
16
17
|
# @param key [Symbol] symbol of state
|
@@ -38,14 +39,17 @@ module Harvest
|
|
38
39
|
@client = Harvest::HTTP::Api.new(**@config)
|
39
40
|
@factory = Harvest::ResourceFactory.new
|
40
41
|
@state = state
|
41
|
-
@
|
42
|
-
@admin_api = if @active_user.is_admin
|
42
|
+
@admin_api = if active_user.is_admin
|
43
43
|
config.admin_api
|
44
44
|
else
|
45
45
|
false
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
+
def active_user
|
50
|
+
@state[:active_user] ||= @factory.user(@client.api_call(@client.api_caller('/users/me')))
|
51
|
+
end
|
52
|
+
|
49
53
|
def allowed?(meth)
|
50
54
|
%i[
|
51
55
|
projects
|
@@ -78,7 +82,7 @@ module Harvest
|
|
78
82
|
super
|
79
83
|
end
|
80
84
|
rescue NoMethodError
|
81
|
-
# binding.pry
|
85
|
+
# require 'pry'; binding.pry
|
82
86
|
raise Harvest::Exceptions::BadState, "#{meth} is an invalid state change."
|
83
87
|
end
|
84
88
|
|
@@ -90,6 +94,16 @@ module Harvest
|
|
90
94
|
self
|
91
95
|
end
|
92
96
|
|
97
|
+
# Find single instance of resource
|
98
|
+
def change(**kwargs)
|
99
|
+
@state[@state[:active]].map do |obj|
|
100
|
+
Harvest::Changers.const_get(to_class_name(@state[:active])).new.change(
|
101
|
+
@factory, @client, active_user, @state, kwargs
|
102
|
+
)
|
103
|
+
end
|
104
|
+
self
|
105
|
+
end
|
106
|
+
|
93
107
|
# Discover resources
|
94
108
|
def discover(**params)
|
95
109
|
@state[@state[:active]] = Harvest::Discovers
|
@@ -101,6 +115,16 @@ module Harvest
|
|
101
115
|
self
|
102
116
|
end
|
103
117
|
|
118
|
+
# Create an instance of object based on state
|
119
|
+
def create(**kwargs)
|
120
|
+
@state[@state[:active]] = Harvest::Create.const_get(
|
121
|
+
to_class_name(@state[:active])
|
122
|
+
).new.create(
|
123
|
+
@factory, @client, active_user, @state, kwargs
|
124
|
+
)
|
125
|
+
self
|
126
|
+
end
|
127
|
+
|
104
128
|
# Select a subset of all items depending on state
|
105
129
|
def select(&block)
|
106
130
|
@state[@state[:active]] = @state[@state[:active]].select(&block)
|
@@ -116,15 +140,5 @@ module Harvest
|
|
116
140
|
def data
|
117
141
|
@state[@state[:active]]
|
118
142
|
end
|
119
|
-
|
120
|
-
# Create an instance of object based on state
|
121
|
-
def create(**kwargs)
|
122
|
-
@state[@state[:active]] = Harvest::Create.const_get(
|
123
|
-
to_class_name(@state[:active])
|
124
|
-
).new.create(
|
125
|
-
@factory, @client, active_user, @state, kwargs
|
126
|
-
)
|
127
|
-
self
|
128
|
-
end
|
129
143
|
end
|
130
144
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Harvest
|
4
|
+
module Changers
|
5
|
+
class TimeEntry
|
6
|
+
def change(factory, client, _active_user, state, kwargs)
|
7
|
+
# binding.pry
|
8
|
+
state[state[:active]].map do |te|
|
9
|
+
send(kwargs[:action].to_sym, factory, client, te)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def restart(factory, client, te)
|
16
|
+
# PATCH /v2/time_entries/{TIME_ENTRY_ID}/restart
|
17
|
+
# binding.pry
|
18
|
+
[factory.time_entry(client.api_call(client.api_caller("time_entries/#{te.id}/restart", http_method: 'patch')))]
|
19
|
+
end
|
20
|
+
|
21
|
+
def stop(factory, client, te)
|
22
|
+
# PATCH /v2/time_entries/{TIME_ENTRY_ID}/stop
|
23
|
+
[factory.time_entry(client.api_call(client.api_caller("time_entries/#{te.id}/stop", http_method: 'patch')))]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/harvest/creates.rb
CHANGED
@@ -34,8 +34,8 @@ module Harvest
|
|
34
34
|
|
35
35
|
# @api private
|
36
36
|
def time_entry_payload(kwargs)
|
37
|
-
possible_keys = %i[spent_date notes external_reference user_id]
|
38
|
-
payload = kwargs.
|
37
|
+
possible_keys = %i[spent_date notes external_reference user_id project_id task_id]
|
38
|
+
payload = kwargs.each { |k, v| [k, v] if possible_keys.include?(k) }.to_h
|
39
39
|
payload[:user_id] ||= @active_user.id
|
40
40
|
payload[:task_id] = @state[:project_tasks][0].task.id
|
41
41
|
payload[:project_id] = true_project(@state[:projects][0]).id
|
data/lib/harvest/discovers.rb
CHANGED
data/lib/harvest/finders.rb
CHANGED
data/lib/harvest/httpclient.rb
CHANGED
@@ -71,12 +71,11 @@ module Harvest
|
|
71
71
|
|
72
72
|
# Make a api call to an endpoint.
|
73
73
|
# @api public
|
74
|
-
# @param struct [
|
74
|
+
# @param struct [ApiCall]
|
75
75
|
def api_call(struct)
|
76
76
|
JSON.parse(
|
77
77
|
send(struct.http_method.to_sym, struct).tap do
|
78
|
-
require 'pry'
|
79
|
-
# binding.pry
|
78
|
+
# require 'pry'; binding.pry
|
80
79
|
end
|
81
80
|
)
|
82
81
|
end
|
@@ -86,7 +85,9 @@ module Harvest
|
|
86
85
|
# @param struct [Struct::Pagination]
|
87
86
|
def pagination(struct)
|
88
87
|
struct.param[:page] = struct.page_count
|
89
|
-
page = api_call(struct.to_api_call)
|
88
|
+
page = api_call(struct.to_api_call).tap do
|
89
|
+
# require 'pry'; binding.pry
|
90
|
+
end
|
90
91
|
struct.rows.concat(page[struct.data_key])
|
91
92
|
|
92
93
|
return struct.rows if struct.page_count >= page['total_pages']
|
@@ -109,11 +110,11 @@ module Harvest
|
|
109
110
|
end
|
110
111
|
|
111
112
|
def api_caller(path, http_method: 'get', param: {}, payload: nil, headers: {})
|
112
|
-
|
113
|
+
ApiCall.new(
|
113
114
|
{
|
114
115
|
path: path,
|
115
116
|
http_method: http_method.to_sym,
|
116
|
-
|
117
|
+
params: param,
|
117
118
|
payload: payload,
|
118
119
|
headers: headers
|
119
120
|
}
|
@@ -126,22 +127,43 @@ module Harvest
|
|
126
127
|
client[struct.path].get(struct.headers)
|
127
128
|
end
|
128
129
|
|
130
|
+
def delete(struct)
|
131
|
+
client[struct.path].delete(struct.headers)
|
132
|
+
end
|
133
|
+
|
129
134
|
def post(struct)
|
130
135
|
client[struct.path].post(struct.payload, struct.headers)
|
131
136
|
end
|
137
|
+
|
138
|
+
def patch(struct)
|
139
|
+
client[struct.path].patch(struct.payload, struct.headers)
|
140
|
+
end
|
132
141
|
end
|
133
142
|
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
:http_method,
|
138
|
-
|
139
|
-
:
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
143
|
+
class NoOptionProvided; end
|
144
|
+
|
145
|
+
class ApiCall
|
146
|
+
attr_accessor :path, :http_method, :payload, :headers
|
147
|
+
|
148
|
+
def initialize(path:, payload: {}, http_method: 'get', headers: {}, params: {})
|
149
|
+
@path = path
|
150
|
+
@http_method = http_method
|
151
|
+
@payload = payload
|
152
|
+
@headers = headers
|
153
|
+
param(params)
|
154
|
+
end
|
155
|
+
|
156
|
+
def param(params = NoOptionProvided)
|
157
|
+
if params == NoOptionProvided
|
158
|
+
@headers['params']
|
159
|
+
else
|
160
|
+
@headers['params'] = params
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def param=(params)
|
165
|
+
param(params)
|
166
|
+
self
|
145
167
|
end
|
146
168
|
end
|
147
169
|
|
@@ -158,11 +180,11 @@ module Harvest
|
|
158
180
|
keyword_init: true
|
159
181
|
) do
|
160
182
|
def to_api_call
|
161
|
-
|
162
|
-
{
|
183
|
+
ApiCall.new(
|
184
|
+
**{
|
163
185
|
path: path,
|
164
186
|
http_method: http_method,
|
165
|
-
|
187
|
+
params: param,
|
166
188
|
payload: payload,
|
167
189
|
headers: headers
|
168
190
|
}
|
@@ -208,11 +208,16 @@ module Harvest
|
|
208
208
|
end
|
209
209
|
|
210
210
|
def convert_dates(data)
|
211
|
-
%i[created_at updated_at sent_at accepted_at declined_at
|
211
|
+
%i[created_at updated_at sent_at accepted_at declined_at].each do |key|
|
212
212
|
if data.members.include?(key) && !data.method(key).call.nil?
|
213
213
|
data.method("#{key}=").call(DateTime.strptime(data.method(key).call))
|
214
214
|
end
|
215
215
|
end
|
216
|
+
%i[spent_date].each do |key|
|
217
|
+
if data.members.include?(key) && !data.method(key).call.nil?
|
218
|
+
data.method("#{key}=").call(Date.strptime(data.method(key).call))
|
219
|
+
end
|
220
|
+
end
|
216
221
|
data
|
217
222
|
end
|
218
223
|
end
|
data/lib/harvest/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: harvest-ruby-v2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Craig Davis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- bin/setup
|
62
62
|
- harvest-ruby-v2.gemspec
|
63
63
|
- lib/harvest.rb
|
64
|
+
- lib/harvest/changers.rb
|
64
65
|
- lib/harvest/client.rb
|
65
66
|
- lib/harvest/config.rb
|
66
67
|
- lib/harvest/creates.rb
|