usps-imis-api 0.3.3.pre.1 → 0.4.0.pre.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc22f317de0a82cfb40a5083b7db1d9fb86a5d942bb83547dc289f85dafb850a
4
- data.tar.gz: ead09b5d23d8bc4caace906d06ed728b8d6eaca16a09b911ba1d7d327847e2c4
3
+ metadata.gz: 3459419a9ef8f312f60eba3a4fc03c2742fba42e5c9cbda666f91dd74c4caa5e
4
+ data.tar.gz: 0fd66d1145913d41f07c3d4ca6750b4ec7462f0bfdfbf9987385a06a2ab1c199
5
5
  SHA512:
6
- metadata.gz: 12edb38028ba33c80cec5445e4516814d9abbbb535a0073dfc565edfb80554175b42e1047c91a31da97a188af9882ed46e77839a8bdaf032043eb993bddb352d
7
- data.tar.gz: f4b506cf80210450c215ff7fede0f1b83469b6595e4f21c23cb5eb7ceea186c71371d6404cf64fb285ca88b1d81423d3c5e50b65cb5c6c088a293efd34031d23
6
+ metadata.gz: 7bbe93c98e47e1169cbae8b5979d365745fbfe9adcfc6a3f0842b9277321add4c0f4f55bd2315ee89378c9ec86771b1135d775bb3383eb5bb3350a11b8c2a294
7
+ data.tar.gz: 5317c2e4c886e596f2f408e752925c32911319ef74b4cb1fc695c3bdc8284a980e62ff4a4a24f9c946d26795c532f48a777c965d5f81f01c22f8b59f03b66057
data/.rubocop.yml CHANGED
@@ -45,9 +45,9 @@ Metrics/MethodLength:
45
45
  Max: 15
46
46
  Metrics/ClassLength:
47
47
  Enabled: true
48
- Max: 125
48
+ Max: 150
49
49
  Metrics/ModuleLength:
50
- Max: 125
50
+ Max: 150
51
51
  Metrics/ParameterLists:
52
52
  Enabled: true
53
53
  Metrics/CyclomaticComplexity:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- usps-imis-api (0.3.3.pre.1)
4
+ usps-imis-api (0.4.0.pre.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/Readme.md CHANGED
@@ -41,6 +41,12 @@ Instantiate the API object:
41
41
  api = Usps::Imis::Api.new
42
42
  ```
43
43
 
44
+ If you already have an iMIS ID to work with, you can pass that in immediately:
45
+
46
+ ```ruby
47
+ api = Usps::Imis::Api.new(imis_id: imis_id)
48
+ ```
49
+
44
50
  ### Authentication
45
51
 
46
52
  If a token is not available, this will automatically fetch one when needed. As long as that token
@@ -176,6 +182,12 @@ vsc.update(certificate: 'E136924', year: 2024, count: 43, ordinal: ordinal)
176
182
  vsc.destroy(ordinal)
177
183
  ```
178
184
 
185
+ If you already have an iMIS ID to work with, you can pass that in immediately:
186
+
187
+ ```ruby
188
+ vsc = Usps::Imis::Panel::Vsc.new(imis_id: imis_id)
189
+ ```
190
+
179
191
  Panels are also accessible directly from the API object:
180
192
 
181
193
  ```ruby
@@ -210,6 +222,9 @@ end
210
222
 
211
223
  Exception and error response handling will be added later.
212
224
 
225
+ To print exception information to STDERR when raising, set the environment
226
+ variable `IMIS_ERROR_LOG_TO_STDERR=true`.
227
+
213
228
  ## Automated Testing and Linting
214
229
 
215
230
  Testing is available by running:
data/lib/usps/imis/api.rb CHANGED
@@ -149,7 +149,7 @@ module Usps
149
149
 
150
150
  def submit(uri, request)
151
151
  client(uri).request(request).tap do |result|
152
- raise Error::Api.from(result) unless result.is_a?(Net::HTTPSuccess)
152
+ raise Error::Response.new(result) unless result.is_a?(Net::HTTPSuccess)
153
153
  end
154
154
  end
155
155
 
@@ -19,7 +19,7 @@ module Usps
19
19
  when :development
20
20
  IMIS_ROOT_URL_DEV
21
21
  else
22
- raise "Unexpected API environment: #{environment}"
22
+ raise Error:Api, "Unexpected API environment: #{environment}"
23
23
  end
24
24
  end
25
25
  end
@@ -4,57 +4,23 @@ module Usps
4
4
  module Imis
5
5
  module Error
6
6
  class Api < StandardError
7
- attr_reader :response
8
7
  attr_accessor :metadata
9
8
 
10
- def self.from(response)
11
- new('The iMIS API returned an error.', response)
12
- end
13
-
14
- def initialize(message, response, metadata = {})
9
+ def initialize(message, metadata = {})
15
10
  super(message)
16
11
  @metadata = metadata
17
- @response = response
12
+
13
+ warn inspect if ENV.fetch('IMIS_ERROR_LOG_TO_STDERR', 'false') == 'true'
18
14
  end
19
15
 
20
16
  def bugsnag_meta_data
21
- { api: { status: status, body: body }.merge!(metadata) }
17
+ metadata != {} ? { api: metadata } : {}
22
18
  end
23
19
 
24
20
  private
25
21
 
26
- def status
27
- @status ||=
28
- case response.code
29
- when '400'
30
- :bad_request
31
- when '401'
32
- :unauthorized # RequestVerificationToken invalid
33
- when '404'
34
- :not_found
35
- when '422'
36
- :unprocessable_entity # validation error
37
- when /^50\d$/
38
- :internal_server_error # error within iMIS
39
- end
40
- end
41
-
42
- def response_body
43
- @response_body ||= JSON.parse(response.body)
44
- rescue StandardError
45
- @response_body ||= response.body
46
- end
47
-
48
- def body
49
- return response_body unless response_body.is_a?(Hash)
50
-
51
- case response_body['error']
52
- when 'invalid_grant'
53
- response_body['error_description']
54
- else
55
- # Unknown error type: just use the raw response
56
- response.body
57
- end
22
+ def base_metadata
23
+ { api: {} }
58
24
  end
59
25
  end
60
26
  end
@@ -3,7 +3,7 @@
3
3
  module Usps
4
4
  module Imis
5
5
  module Error
6
- class Mapper < StandardError; end
6
+ class Mapper < Api; end
7
7
  end
8
8
  end
9
9
  end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ module Error
6
+ class Response < Api
7
+ attr_reader :response
8
+ attr_accessor :metadata
9
+
10
+ def initialize(response, metadata = {})
11
+ @response = response
12
+ super(message, metadata)
13
+ end
14
+
15
+ def bugsnag_meta_data
16
+ base_metadata.tap { |m| m[:api].merge!(metadata) }
17
+ end
18
+
19
+ def message
20
+ [
21
+ "#{self.class.name}: [#{status.to_s.upcase}] The iMIS API returned an error.",
22
+ (metadata.inspect if metadata != {}),
23
+ body
24
+ ].compact.join("\n")
25
+ end
26
+
27
+ private
28
+
29
+ def base_metadata
30
+ { api: { status: status, body: body } }
31
+ end
32
+
33
+ def status
34
+ @status ||=
35
+ case response.code
36
+ when '400'
37
+ :bad_request
38
+ when '401'
39
+ :unauthorized # RequestVerificationToken invalid
40
+ when '404'
41
+ :not_found
42
+ when '422'
43
+ :unprocessable_entity # validation error
44
+ when /^50\d$/
45
+ :internal_server_error # error within iMIS
46
+ end
47
+ end
48
+
49
+ def response_body
50
+ @response_body ||= JSON.parse(response.body)
51
+ rescue StandardError
52
+ @response_body ||= response.body
53
+ end
54
+
55
+ def body
56
+ return response_body unless response_body.is_a?(Hash)
57
+
58
+ case response_body['error']
59
+ when 'invalid_grant'
60
+ response_body['error_description']
61
+ else
62
+ # Unknown error type: just use the raw response
63
+ response.body
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -14,8 +14,9 @@ module Usps
14
14
 
15
15
  attr_reader :api
16
16
 
17
- def initialize(api = nil)
17
+ def initialize(api = nil, imis_id: nil)
18
18
  @api = api || Api.new
19
+ api.imis_id = imis_id if imis_id
19
20
  end
20
21
 
21
22
  # Update a member's data on multiple affected business objects by arbitrary field names
@@ -59,7 +60,7 @@ module Usps
59
60
  end
60
61
 
61
62
  raise(
62
- Imis::Error::Mapper,
63
+ Error::Mapper,
63
64
  "Unrecognized field: \"#{field_name}\". " \
64
65
  'Please report what data you are attempting to work with to ITCom leadership.'
65
66
  )
@@ -30,11 +30,11 @@ module Usps
30
30
  private
31
31
 
32
32
  def business_object
33
- raise "#{self.class.name} must implement #business_object"
33
+ raise Error::Api, "#{self.class.name} must implement #business_object"
34
34
  end
35
35
 
36
36
  def payload(_data)
37
- raise "#{self.class.name} must implement #payload(data)"
37
+ raise Error::Api, "#{self.class.name} must implement #payload(data)"
38
38
  end
39
39
  end
40
40
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.3.3-1'
5
+ VERSION = '0.4.0-1'
6
6
  end
7
7
  end
data/lib/usps/imis.rb CHANGED
@@ -13,6 +13,7 @@ require 'ext/hash' unless defined?(Rails)
13
13
  require_relative 'imis/config'
14
14
  require_relative 'imis/error/api'
15
15
  require_relative 'imis/error/mapper'
16
+ require_relative 'imis/error/response'
16
17
  require_relative 'imis/api'
17
18
  require_relative 'imis/mapper'
18
19
  require_relative 'imis/panel/base_panel'
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ ApiResponseStub = Struct.new(:code, :body)
6
+
7
+ describe Usps::Imis::Error::Response do
8
+ let(:warning_text) do
9
+ <<~WARNING.chomp
10
+ Usps::Imis::Error::Response: [INTERNAL_SERVER_ERROR] The iMIS API returned an error.
11
+ Body of the API response error
12
+ WARNING
13
+ end
14
+
15
+ it 'handles an API response correctly' do
16
+ response = ApiResponseStub.new('500', 'Body of the API response error')
17
+
18
+ expect(described_class.new(response).message).to eq(warning_text)
19
+ end
20
+ end
@@ -30,7 +30,7 @@ describe Usps::Imis::Panel::Education do
30
30
  new_record = education.create(details)
31
31
  expect(new_record).to be_a(Hash)
32
32
 
33
- ordinal = new_record['Properties']['$values'][1]['Value']['$value']
33
+ ordinal = new_record['Identity']['IdentityElements']['$values'][1]
34
34
 
35
35
  update_result =
36
36
  education.update(details.merge(source: 'Online Exams System - Modified', ordinal: ordinal))
@@ -26,7 +26,7 @@ describe Usps::Imis::Panel::Vsc do
26
26
  new_record = vsc.create(details)
27
27
  expect(new_record).to be_a(Hash)
28
28
 
29
- ordinal = new_record['Properties']['$values'][1]['Value']['$value']
29
+ ordinal = new_record['Identity']['IdentityElements']['$values'][1]
30
30
 
31
31
  update_result = vsc.update(details.merge(count: 43, ordinal: ordinal))
32
32
  updated = update_result['Properties']['$values'].find { |v| v['Name'] == 'Quantity' }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps-imis-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3.pre.1
4
+ version: 0.4.0.pre.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -32,12 +32,14 @@ files:
32
32
  - lib/usps/imis/config.rb
33
33
  - lib/usps/imis/error/api.rb
34
34
  - lib/usps/imis/error/mapper.rb
35
+ - lib/usps/imis/error/response.rb
35
36
  - lib/usps/imis/mapper.rb
36
37
  - lib/usps/imis/panel/base_panel.rb
37
38
  - lib/usps/imis/panel/education.rb
38
39
  - lib/usps/imis/panel/vsc.rb
39
40
  - lib/usps/imis/version.rb
40
41
  - spec/lib/usps/imis/api_spec.rb
42
+ - spec/lib/usps/imis/error/response_spec.rb
41
43
  - spec/lib/usps/imis/mapper_spec.rb
42
44
  - spec/lib/usps/imis/panel/education_spec.rb
43
45
  - spec/lib/usps/imis/panel/vsc_spec.rb