ecoportal-api 0.5.1 → 0.5.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7169c71eba9b00510c284c1561fea403cdecb456cf929b4039f27d758a47801c
4
- data.tar.gz: e160bb67d93052c68be9cfb33b3cdc9e582aceaa0685309021ffa24d1af14cfd
3
+ metadata.gz: a89994e76cf541159975756271aa254c709696347e1a5eb9b2181dbea8b54d25
4
+ data.tar.gz: 4ef1d5c9ad068faad4922fac5dae85f6fc6e629271b0680c1ee5010a4aadf424
5
5
  SHA512:
6
- metadata.gz: 6e2c2aa49ba1f1a1b1492bce6933df97a142ad3b05f102697607b58307361d2349cbb076bf5e2ffbb734cc6582e83effc897497a549746c8e1ddb653d95d2df1
7
- data.tar.gz: d414cf5182137cecf1e82a7289af347ad1329ea4c917abe14879672788359189a496c8896e9049ad2e18fb504920f945c849e1b0021bb5cdb21d0d9745f071c7
6
+ metadata.gz: 21db065f98ef0aa1afe59b5b4766474691eaad98c78d0175f7d1161585f8f8bfa07426ee9edd4f056439e5dffc1d94bd608a2124411bd68fc051537b11048aa5
7
+ data.tar.gz: 32b98b96710d3147ab2fde5f32b982e2d8a3ea234cd8c2c1eb707bdb98cf46a62a9f905caae2cfeb2889b62793bb1285c5c8215598da917f7ff944f951328828
@@ -0,0 +1,55 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5.1
3
+ Exclude:
4
+ - 'config/routes.rb'
5
+
6
+ Metrics/LineLength:
7
+ Enabled: false
8
+ Metrics/BlockLength:
9
+ ExcludedMethods: [context, describe]
10
+ Max: 50
11
+ Metrics/MethodLength:
12
+ Max: 50
13
+ Metrics/ClassLength:
14
+ Max: 200
15
+ Metrics/AbcSize:
16
+ Max: 30
17
+
18
+ ParameterLists:
19
+ Max: 5
20
+ CountKeywordArgs: false
21
+
22
+ Style/StringLiterals:
23
+ Enabled: false
24
+ Style/FrozenStringLiteralComment:
25
+ Enabled: false
26
+ Style/CommentedKeyword:
27
+ Enabled: false
28
+ Style/MultilineBlockChain:
29
+ Enabled: false
30
+ Style/Documentation:
31
+ Enabled: false
32
+ Style/StringLiteralsInInterpolation:
33
+ Enabled: false
34
+ Style/AndOr:
35
+ Enabled: false
36
+
37
+ Layout/SpaceInsideHashLiteralBraces:
38
+ Enabled: false
39
+ Layout/SpaceInsideBlockBraces:
40
+ Enabled: false
41
+ Layout/SpaceAroundOperators:
42
+ Enabled: false
43
+ Layout/ExtraSpacing:
44
+ AllowForAlignment: true
45
+ Layout/AccessModifierIndentation:
46
+ EnforcedStyle: indent
47
+ Layout/DotPosition:
48
+ EnforcedStyle: trailing
49
+ Layout/MultilineMethodCallIndentation:
50
+ EnforcedStyle: indented
51
+ Layout/IndentHash:
52
+ Enabled: false
53
+
54
+ Naming/VariableNumber:
55
+ EnforcedStyle: snake_case
@@ -10,5 +10,5 @@ require "ecoportal/api"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
14
- IRB.start(__FILE__)
13
+ require "pry"
14
+ Pry.start(__FILE__)
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "ecoportal-api"
8
8
  spec.version = Ecoportal::API::VERSION
9
9
  spec.authors = ["Tapio Saarinen"]
10
- spec.email = ["tapio@ecoportal.co.nz", "rien@ecoportal.co.nz", "oscar@ecoportal.co.nz"]
10
+ spec.email = ["tapio@ecoportal.co.nz", "rien@ecoportal.co.nz", "oscar@ecoportal.co.nz", "bozydar@ecoportal.co.nz"]
11
11
 
12
12
  spec.summary = %q{A collection of helpers for interacting with the ecoPortal MS's various APIs}
13
13
  spec.homepage = "https://www.ecoportal.com"
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
26
  spec.add_development_dependency "yard", "~> 0.9", ">= 0.9.18"
27
27
  spec.add_development_dependency "redcarpet", "~> 3.4", ">= 3.4.0"
28
+ spec.add_development_dependency "pry"
28
29
 
29
30
  spec.add_dependency 'http', '~> 3'
30
31
  spec.add_dependency 'hash-polyfill', '~> 0'
@@ -7,7 +7,8 @@ module Ecoportal
7
7
  :show_shortcuts,
8
8
  :show_coming_soon,
9
9
  :show_recently_visited_forms,
10
- :show_tasks
10
+ :show_tasks,
11
+ :show_task_bubbles
11
12
  ].each do |field|
12
13
  define_method(field) do
13
14
  if doc.key?(field)
@@ -7,7 +7,10 @@ module Ecoportal
7
7
  include Enumerable
8
8
  include Common::DocHelpers
9
9
 
10
- class_resolver :person_class, "Ecoportal::API::V1::Person"
10
+ JOB_TIMEOUT = 60
11
+ DELAY_STATUS_CHECK = 5
12
+
13
+ class_resolver :person_class, "Ecoportal::API::V1::Person"
11
14
 
12
15
  attr_reader :client
13
16
 
@@ -28,7 +31,7 @@ module Ecoportal
28
31
  # @yieldparam person [Person]
29
32
  def each(params: {}, &block)
30
33
  return to_enum(:each) unless block
31
- results_from = nil
34
+ results_from = nil
32
35
  loop do
33
36
  params.update(results_from: results_from) if results_from
34
37
  response = client.get("/people", params: params)
@@ -56,7 +59,7 @@ module Ecoportal
56
59
  # @param doc [String, Hash, Person] data containing an `id` (internal or external) of the target person.
57
60
  # @return [WrappedResponse] an object with the api response.
58
61
  def get(doc)
59
- response = client.get("/people/"+CGI::escape(get_id(doc)))
62
+ response = client.get("/people/"+CGI.escape(get_id(doc)))
60
63
  Common::WrappedResponse.new(response, person_class)
61
64
  end
62
65
 
@@ -66,7 +69,7 @@ module Ecoportal
66
69
  def update(doc)
67
70
  body = get_body(doc)
68
71
  id = get_id(doc)
69
- client.patch("/people/"+CGI::escape(id), data: body)
72
+ client.patch("/people/"+CGI.escape(id), data: body)
70
73
  end
71
74
 
72
75
  # Requests to create a person via api.
@@ -83,7 +86,7 @@ module Ecoportal
83
86
  def upsert(doc)
84
87
  body = get_body(doc)
85
88
  id = get_id(doc)
86
- client.post("/people/"+CGI::escape(id), data: body)
89
+ client.post("/people/"+CGI.escape(id), data: body)
87
90
  end
88
91
 
89
92
  # Requests to completelly remove from an organization an existing person via api.
@@ -91,13 +94,14 @@ module Ecoportal
91
94
  # @return [Response] an object with the api response.
92
95
  def delete(doc)
93
96
  id = get_id(doc)
94
- client.delete("/people/"+CGI::escape(id))
97
+ client.delete("/people/"+CGI.escape(id))
95
98
  end
96
99
 
97
100
  # Creates a `BatchOperation` and yields it to the given bock.
98
101
  # @yield [batch_op] adds multiple api requests for the current batch.
99
102
  # @yieldparam batch_op [BatchOperation]
100
- def batch
103
+ def batch(job_mode: true, &block)
104
+ return job(&block) if job_mode
101
105
  operation = Common::BatchOperation.new("/people", person_class, logger: client.logger)
102
106
  yield operation
103
107
  # The batch operation is responsible for logging the output
@@ -108,12 +112,72 @@ module Ecoportal
108
112
  end
109
113
  end
110
114
 
115
+ def job
116
+ operation = Common::BatchOperation.new("/people", person_class, logger: client.logger)
117
+ yield operation
118
+ # The batch operation is responsible for logging the output
119
+ job_id = create_job(operation)
120
+ status = wait_for_job_completion(job_id)
121
+
122
+ if status&.complete?
123
+ operation.process_response job_result(job_id, operation)
124
+ else
125
+ raise "Job `#{job_id}` not complete. Probably timeout after #{JOB_TIMEOUT} seconds. Current status: #{status}"
126
+ end
127
+ end
128
+
111
129
  # Creates a new `Person` object.
112
130
  # @return [Person] new empty person object of the current version.
113
131
  def new
114
132
  person_class.new
115
133
  end
116
134
 
135
+ private
136
+
137
+ JobStatus = Struct.new(:id, :complete?, :errored?, :progress)
138
+ def job_status(job_id)
139
+ response = client.get("/people/job/#{CGI.escape(job_id)}/status")
140
+
141
+ raise "Status error" unless response.success?
142
+ JobStatus.new(
143
+ response.body["id"],
144
+ response.body["complete"],
145
+ response.body["errored"],
146
+ response.body["progress"]
147
+ )
148
+ end
149
+
150
+ def job_result(job_id, operation)
151
+ # The batch operation is responsible for logging the output
152
+ client.without_response_logging do
153
+ client.get("/people/job/#{CGI.escape(job_id)}").tap do |response|
154
+ operation.process_response(response)
155
+ end
156
+ end
157
+ end
158
+
159
+ def wait_for_job_completion(job_id)
160
+ # timeout library is evil. So we make poor-man timeout.
161
+ # https://jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/
162
+ before = Time.now
163
+ while true
164
+ status = job_status(job_id)
165
+ break status if status.complete?
166
+ break status if Time.now >= before + JOB_TIMEOUT
167
+ sleep(DELAY_STATUS_CHECK)
168
+ status
169
+ end
170
+ end
171
+
172
+ def create_job(operation)
173
+ job_id = nil
174
+ client.without_response_logging do
175
+ client.post("/people/job", data: operation.as_json).tap do |response|
176
+ job_id = response.body["id"]
177
+ end
178
+ end
179
+ job_id
180
+ end
117
181
  end
118
182
  end
119
183
  end
@@ -1,5 +1,5 @@
1
1
  module Ecoportal
2
2
  module API
3
- VERSION = "0.5.1"
3
+ VERSION = "0.5.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecoportal-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tapio Saarinen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-01 00:00:00.000000000 Z
11
+ date: 2019-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,6 +92,20 @@ dependencies:
92
92
  - - "~>"
93
93
  - !ruby/object:Gem::Version
94
94
  version: '3.4'
95
+ - !ruby/object:Gem::Dependency
96
+ name: pry
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
95
109
  - !ruby/object:Gem::Dependency
96
110
  name: http
97
111
  requirement: !ruby/object:Gem::Requirement
@@ -125,12 +139,14 @@ email:
125
139
  - tapio@ecoportal.co.nz
126
140
  - rien@ecoportal.co.nz
127
141
  - oscar@ecoportal.co.nz
142
+ - bozydar@ecoportal.co.nz
128
143
  executables: []
129
144
  extensions: []
130
145
  extra_rdoc_files: []
131
146
  files:
132
147
  - ".gitignore"
133
148
  - ".rspec"
149
+ - ".rubocop.yml"
134
150
  - ".travis.yml"
135
151
  - ".yardopts"
136
152
  - Gemfile
@@ -199,7 +215,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
199
215
  - !ruby/object:Gem::Version
200
216
  version: '0'
201
217
  requirements: []
202
- rubygems_version: 3.0.3
218
+ rubygems_version: 3.0.4
203
219
  signing_key:
204
220
  specification_version: 4
205
221
  summary: A collection of helpers for interacting with the ecoPortal MS's various APIs