harvest-ruby-v2 0.3.2 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d5ed655f022d6e69111845443dfe35b98532367865787c1e58300b0910d9625c
4
- data.tar.gz: 3ed5a264fc80566e21eb2942148ed16a91adbec338315a0923b842867814c55c
3
+ metadata.gz: 58350245891bc951a4eaf534edd5adf454686668aecd9fc18e9607bbdbb5ce36
4
+ data.tar.gz: f1053f1810e4297cfb8d0400c064d2db2875359dd0a5440c1327ca0db50300ab
5
5
  SHA512:
6
- metadata.gz: 71d7d18d7f7b91e887f3fb694c2cc60636b10dd607f2a2bd4b44e75e20e74fcc6dbe55d4d52f062993692fd8bbebba91b51e099fc0eb4515fa3e49cb81949aae
7
- data.tar.gz: 3f3af9a5c64d7a0c92b532968c9a66254048b01cde403dc33bf8bfc054f228b7a335c1aec1051ad01fb0dd68552d5ead215dd45f26e2f41da308fd9b6e38b7e7
6
+ metadata.gz: e07110f419334d96fcb0c54f7394adecfc33cb5f1ed82e2ccadefdacc646534b24200f1081d53f1d119361368454b4b955eead82e817ca43b76c83b5fbf3067b
7
+ data.tar.gz: e9557a89a104a7a358dd37a58b865422b5fba6b9d107af88941f1d47921251b7c34613945b19ff3f26a0e90b999ec3811bf3db096b63fdbc36de0a4117faa47a
data/CHANGELOG.rst CHANGED
@@ -0,0 +1,13 @@
1
+ ##########################
2
+ Changelog for harvest-ruby
3
+ ##########################
4
+
5
+ 0.5.1
6
+ ^^^^^
7
+
8
+ * Harvest API external_reference on time entries now contains account_id, updated Struct
9
+
10
+ 0.5.0
11
+ ^^^^^
12
+
13
+ * Fixed bug with ApiCall which caused query params to not be passed to the underlying call.
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
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'pry'
4
+ require 'pry-stack_explorer'
5
5
  require 'configparser'
6
6
  require 'date'
7
7
  require "bundler/setup"
data/lib/harvest.rb CHANGED
@@ -11,9 +11,11 @@ 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
18
+ # @return [Symbol] Symbol to use in class lookups
17
19
  def to_class_name(key)
18
20
  key.to_s.split('_').map(&:capitalize).join.to_sym
19
21
  end
@@ -38,14 +40,17 @@ module Harvest
38
40
  @client = Harvest::HTTP::Api.new(**@config)
39
41
  @factory = Harvest::ResourceFactory.new
40
42
  @state = state
41
- @active_user = @factory.user(@client.api_call(@client.api_caller('/users/me')))
42
- @admin_api = if @active_user.is_admin
43
+ @admin_api = if active_user.is_admin
43
44
  config.admin_api
44
45
  else
45
46
  false
46
47
  end
47
48
  end
48
49
 
50
+ def active_user
51
+ @state[:active_user] ||= @factory.user(@client.api_call(@client.api_caller('/users/me')))
52
+ end
53
+
49
54
  def allowed?(meth)
50
55
  %i[
51
56
  projects
@@ -78,7 +83,7 @@ module Harvest
78
83
  super
79
84
  end
80
85
  rescue NoMethodError
81
- # binding.pry
86
+ # require 'pry'; binding.pry
82
87
  raise Harvest::Exceptions::BadState, "#{meth} is an invalid state change."
83
88
  end
84
89
 
@@ -90,6 +95,16 @@ module Harvest
90
95
  self
91
96
  end
92
97
 
98
+ # Find single instance of resource
99
+ def change(**kwargs)
100
+ @state[@state[:active]].map do |_obj|
101
+ Harvest::Changers.const_get(to_class_name(@state[:active])).new.change(
102
+ @factory, @client, active_user, @state, kwargs
103
+ )
104
+ end
105
+ self
106
+ end
107
+
93
108
  # Discover resources
94
109
  def discover(**params)
95
110
  @state[@state[:active]] = Harvest::Discovers
@@ -101,6 +116,16 @@ module Harvest
101
116
  self
102
117
  end
103
118
 
119
+ # Create an instance of object based on state
120
+ def create(**kwargs)
121
+ @state[@state[:active]] = Harvest::Create.const_get(
122
+ to_class_name(@state[:active])
123
+ ).new.create(
124
+ @factory, @client, active_user, @state, kwargs
125
+ )
126
+ self
127
+ end
128
+
104
129
  # Select a subset of all items depending on state
105
130
  def select(&block)
106
131
  @state[@state[:active]] = @state[@state[:active]].select(&block)
@@ -116,15 +141,5 @@ module Harvest
116
141
  def data
117
142
  @state[@state[:active]]
118
143
  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
144
  end
130
145
  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
@@ -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.map { |k, v| [k, v] if possible_keys.include?(k) }.to_h
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
@@ -43,6 +43,9 @@ module Harvest
43
43
  paginator.path = 'time_entries'
44
44
  paginator.data_key = 'time_entries'
45
45
  paginator.param = params
46
+
47
+ # require 'pry'; binding.pry
48
+
46
49
  client.pagination(paginator).map do |time_entry|
47
50
  factory.time_entry(time_entry)
48
51
  end
@@ -10,7 +10,7 @@ module Harvest
10
10
 
11
11
  class TimeEntry
12
12
  def find(factory, client, id)
13
- [factory.time_entry(client.api_call(client.api_caller("time_entry/#{id}")))]
13
+ [factory.time_entry(client.api_call(client.api_caller("time_entries/#{id}")))]
14
14
  end
15
15
  end
16
16
  end
@@ -71,12 +71,11 @@ module Harvest
71
71
 
72
72
  # Make a api call to an endpoint.
73
73
  # @api public
74
- # @param struct [Struct::ApiCall]
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
- Struct::ApiCall.new(
113
+ ApiCall.new(
113
114
  {
114
115
  path: path,
115
116
  http_method: http_method.to_sym,
116
- param: param,
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
- Struct.new(
135
- 'ApiCall',
136
- :path,
137
- :http_method,
138
- :param,
139
- :payload,
140
- :headers,
141
- keyword_init: true
142
- ) do
143
- def param(params)
144
- headers['params'] = params
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
- Struct::ApiCall.new(
162
- {
183
+ ApiCall.new(
184
+ **{
163
185
  path: path,
164
186
  http_method: http_method,
165
- param: param,
187
+ params: param,
166
188
  payload: payload,
167
189
  headers: headers
168
190
  }
@@ -95,6 +95,7 @@ module Harvest
95
95
  TimeEntryExternalReference = Struct.new(
96
96
  'TimeEntryExternalReference',
97
97
  :id,
98
+ :account_id,
98
99
  :group_id,
99
100
  :permalink,
100
101
  :service,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Harvest
4
- VERSION = '0.3.2'
4
+ VERSION = '0.5.1'
5
5
  end
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.3.2
4
+ version: 0.5.1
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-10-20 00:00:00.000000000 Z
11
+ date: 2021-06-07 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