usps-imis-api 0.3.3 → 0.4.0.pre.2

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: 829f39c81242bde99b954d06710ba008290415eb765340be960bad36bd471e0c
4
- data.tar.gz: f15146f633cd77744521b14bcd6d196cf05518f6455528a2b2362a4726debfd8
3
+ metadata.gz: f4e11dc87c8bb7fd70aaa15146afaffa667de0da75cf54117672f42e46a8f4bd
4
+ data.tar.gz: e9e8abd630971b1151849d17d45196b1ec1f509dcd1abf213840d83e24a1eb07
5
5
  SHA512:
6
- metadata.gz: 87c7576a9be599cabc7968a5df6484d421b80e933c5dfb3719974584f2ad563b12e2b3294deaa73bd716e5b8ca270e99a95072673440a4b139b6bd358446d826
7
- data.tar.gz: b844cc08b4c36ec3702a0e97ce4dd6ec000c2926b1440113624f0071e67689b6ef70e44833051a29091e77ea6cdefedce026f6cf4138f4bc76043b2f00559e72
6
+ metadata.gz: 0ceaf850a029c05cf002189e286a2d83d83c52a34fe638511dfcc8c18f1e73a620243eccd48ec299459e43813402f379c2d005d0abb01cb4edbc1d55f79652de
7
+ data.tar.gz: 568a3c2134674c2be717c5653d16b90015c18d699eea58ba2ac6d9c832f8fbe5a4bdd46f3d25ed3dd95fa94413849f369718a98fbe54811cb9bb8fe831ea8109
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- usps-imis-api (0.3.3)
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,67 +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
18
12
 
19
13
  warn inspect if ENV.fetch('IMIS_ERROR_LOG_TO_STDERR', 'false') == 'true'
20
14
  end
21
15
 
22
16
  def bugsnag_meta_data
23
- { api: { status: status, body: body }.merge!(metadata) }
24
- end
25
-
26
- def inspect
27
- [
28
- "#{self.class.name}: [#{status.to_s.upcase}] #{message}",
29
- (metadata.inspect if metadata != {}),
30
- body
31
- ].compact.join("\n")
17
+ metadata != {} ? { api: metadata } : {}
32
18
  end
33
19
 
34
20
  private
35
21
 
36
- def status
37
- @status ||=
38
- case response.code
39
- when '400'
40
- :bad_request
41
- when '401'
42
- :unauthorized # RequestVerificationToken invalid
43
- when '404'
44
- :not_found
45
- when '422'
46
- :unprocessable_entity # validation error
47
- when /^50\d$/
48
- :internal_server_error # error within iMIS
49
- end
50
- end
51
-
52
- def response_body
53
- @response_body ||= JSON.parse(response.body)
54
- rescue StandardError
55
- @response_body ||= response.body
56
- end
57
-
58
- def body
59
- return response_body unless response_body.is_a?(Hash)
60
-
61
- case response_body['error']
62
- when 'invalid_grant'
63
- response_body['error_description']
64
- else
65
- # Unknown error type: just use the raw response
66
- response.body
67
- end
22
+ def base_metadata
23
+ { api: {} }
68
24
  end
69
25
  end
70
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
@@ -60,7 +60,7 @@ module Usps
60
60
  end
61
61
 
62
62
  raise(
63
- Imis::Error::Mapper,
63
+ Error::Mapper,
64
64
  "Unrecognized field: \"#{field_name}\". " \
65
65
  'Please report what data you are attempting to work with to ITCom leadership.'
66
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'
5
+ VERSION = '0.4.0-2'
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'
@@ -4,10 +4,10 @@ require 'spec_helper'
4
4
 
5
5
  ApiResponseStub = Struct.new(:code, :body)
6
6
 
7
- describe Usps::Imis::Error::Api do
7
+ describe Usps::Imis::Error::Response do
8
8
  let(:warning_text) do
9
9
  <<~WARNING.chomp
10
- Usps::Imis::Error::Api: [INTERNAL_SERVER_ERROR] The iMIS API returned an error.
10
+ Usps::Imis::Error::Response: [INTERNAL_SERVER_ERROR] The iMIS API returned an error.
11
11
  Body of the API response error
12
12
  WARNING
13
13
  end
@@ -15,6 +15,6 @@ describe Usps::Imis::Error::Api do
15
15
  it 'handles an API response correctly' do
16
16
  response = ApiResponseStub.new('500', 'Body of the API response error')
17
17
 
18
- expect(described_class.from(response).inspect).to eq(warning_text)
18
+ expect(described_class.new(response).message).to eq(warning_text)
19
19
  end
20
20
  end
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
4
+ version: 0.4.0.pre.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -32,13 +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
41
- - spec/lib/usps/imis/error/api_spec.rb
42
+ - spec/lib/usps/imis/error/response_spec.rb
42
43
  - spec/lib/usps/imis/mapper_spec.rb
43
44
  - spec/lib/usps/imis/panel/education_spec.rb
44
45
  - spec/lib/usps/imis/panel/vsc_spec.rb