usps-imis-api 0.9.15 → 0.10.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ccbec46e992d8642344cfeb67755b31e98c7e915c8066dc2207b3c31023d7bd1
4
- data.tar.gz: 7c1ecddcfe0b322f00ed419e95ab8cd63728191476e650de3364c46b7e4d41fe
3
+ metadata.gz: 46a4e7cdf12bf5d20a7fca22f0efbdaa2b8d6d9b4f4419b8b787828de969f0b5
4
+ data.tar.gz: a3bd80bfb5bb48f495c60e07b7c93a91d834e40fc50ce9157c65bccbdd4be234
5
5
  SHA512:
6
- metadata.gz: 821d850f11301fdb49062366ccba161aa0abe4758e176f0328b2356d53806e1f13d3caa34d4cf7e7a95085f810f6c5ceb716ca25995566458405e5eeb269f3ed
7
- data.tar.gz: 02b02bed4a879ec5bc946d4063b25589f2130dac4b6ea30a01d29358f27af9a06722a77dfb9aa7948ff96cd1cde183ded233d1fd9567ab4eab736e36fc4cae26
6
+ metadata.gz: 13a72cdd52375dd37a1856e3add682d4162e0a8cd6ae3fb72b78bb5bc9d834deff583662cff3a1d1fe73f7c69a664a53f1f6e8bf88b8eb10bc8fcaa77fe163d2
7
+ data.tar.gz: 6289ddfecf558b29c44cede8fdb44cd8fe694554351057c753b232951c5fcd1c945419afc20894b88d15426ba55881e7538ffeb11a8f7629e25dbd48cd219968
data/lib/usps/imis/api.rb CHANGED
@@ -61,8 +61,12 @@ module Usps
61
61
  def imis_id_for(certificate)
62
62
  raise Errors::LockedIdError if lock_imis_id
63
63
 
64
+ logger.debug "Fetching iMIS ID for #{certificate}"
65
+
64
66
  begin
65
- self.imis_id = query(Imis.configuration.imis_id_query_name, { certificate: }).first['ID'].to_i
67
+ result = query(Imis.configuration.imis_id_query_name, { certificate: }).tap { logger.debug it }
68
+ page = result.page.tap { logger.debug it }
69
+ self.imis_id = page.first['ID'].to_i
66
70
  rescue StandardError
67
71
  raise Errors::NotFoundError, 'Member not found'
68
72
  end
@@ -95,9 +99,11 @@ module Usps
95
99
  end
96
100
  end
97
101
 
98
- # Build an IQA Query interface
102
+ # Build a Query interface
103
+ #
104
+ # Works with IQA queries and Business Objects
99
105
  #
100
- # @param query_name [String] Full path of the query in IQA, e.g. +$/_ABC/Fiander/iMIS_ID+
106
+ # @param query_name [String] Full path of the query, e.g. +$/_ABC/Fiander/iMIS_ID+
101
107
  # @query_params [Hash] Conforms to pattern +{ param_name => param_value }+
102
108
  #
103
109
  # @return [Hash] Response data from the API
@@ -33,6 +33,10 @@ module Usps
33
33
  @ordinal = ordinal
34
34
  end
35
35
 
36
+ # Run a query on the entire business object
37
+ #
38
+ def query = api.query(business_object_name)
39
+
36
40
  # Get a business object for the current member
37
41
  #
38
42
  # If +fields+ is provided, will return only those field values
@@ -170,6 +174,8 @@ module Usps
170
174
  # Useful for stubbing data in tests
171
175
  #
172
176
  def raw_object
177
+ raise Errors::MissingIdError if api.imis_id.nil?
178
+
173
179
  response = submit(uri, authorize(http_get))
174
180
  result = Data.from_json(response.body)
175
181
  JSON.pretty_generate(result).split("\n").each { logger.debug " -> #{it}" }
@@ -179,6 +185,8 @@ module Usps
179
185
  # Upload an object to the API
180
186
  #
181
187
  def put_object(request, body)
188
+ raise Errors::MissingIdError if api.imis_id.nil?
189
+
182
190
  request.body = JSON.dump(body)
183
191
  response = submit(uri, authorize(request))
184
192
  result = Data.from_json(response.body)
@@ -12,7 +12,7 @@ module Usps
12
12
  attr_reader :environment, :logger, :logger_level
13
13
 
14
14
  def initialize
15
- @environment = defined?(Rails) ? Rails.env : ActiveSupport::StringInquirer.new('development')
15
+ @environment = defined?(::Rails) ? ::Rails.env : ActiveSupport::StringInquirer.new('development')
16
16
  @imis_id_query_name = ENV.fetch('IMIS_ID_QUERY_NAME', nil)
17
17
  @username = ENV.fetch('IMIS_USERNAME', nil)
18
18
  @password = ENV.fetch('IMIS_PASSWORD', nil)
@@ -31,6 +31,10 @@ module Usps
31
31
  @logger = ActiveSupport::TaggedLogging.new(logger)
32
32
  end
33
33
 
34
+ def silence!
35
+ self.logger = Logger.new(nil)
36
+ end
37
+
34
38
  # Environment-specific API endpoint hostname
35
39
  #
36
40
  # @return The API hostname for the current environment
@@ -47,6 +47,7 @@ require_relative 'errors/api_error'
47
47
  require_relative 'errors/config_error'
48
48
  require_relative 'errors/locked_id_error'
49
49
  require_relative 'errors/mapper_error'
50
+ require_relative 'errors/missing_id_error'
50
51
  require_relative 'errors/not_found_error'
51
52
  require_relative 'errors/response_error'
52
53
  require_relative 'errors/panel_unimplemented_error'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ module Errors
6
+ # Exception raised when attempting to access a +BusinessObject+ without an iMIS ID
7
+ #
8
+ class MissingIdError < Error
9
+ def initialize = super(message)
10
+
11
+ def message = 'Cannot access an individual Business Object without an iMIS ID'
12
+ end
13
+ end
14
+ end
15
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- # API wrapper for IQA Queries
5
+ # API wrapper for IQA and Business Object Queries
6
6
  #
7
7
  class Query
8
8
  include Enumerable
@@ -10,7 +10,7 @@ module Usps
10
10
 
11
11
  # Endpoint for IQA query requests
12
12
  #
13
- QUERY_PATH = 'api/Query'
13
+ IQA_PATH = 'api/Query'
14
14
 
15
15
  # The parent +Api+ object
16
16
  #
@@ -36,6 +36,10 @@ module Usps
36
36
  #
37
37
  attr_reader :count
38
38
 
39
+ # Whether the current query has a next page
40
+ #
41
+ attr_reader :next_page
42
+
39
43
  # A new instance of +Query+
40
44
  #
41
45
  # @param api [Api] Parent to use for making requests
@@ -48,14 +52,18 @@ module Usps
48
52
  @api = api
49
53
  @query_name = query_name
50
54
  @query_params = query_params
55
+ @query_params.merge!(QueryName: query_name) if iqa?
51
56
  @page_size = page_size
52
57
  @offset = offset
58
+ @count = 0
59
+
60
+ logger.debug "URI: #{uri}"
53
61
  end
54
62
 
55
63
  # Iterate through all results from the query
56
64
  #
57
65
  def each(&)
58
- logger.info 'Running IQA Query on iMIS'
66
+ logger.info 'Running'
59
67
 
60
68
  items = []
61
69
  find_each { items << it }
@@ -65,10 +73,10 @@ module Usps
65
73
  # Iterate through all results from the query, fetching one page at a time
66
74
  #
67
75
  def find_each(&)
68
- result = reset!
76
+ reset!
69
77
 
70
- while result['HasNext']
71
- result = fetch_next.tap do |result_page|
78
+ while page?
79
+ fetch_next.tap do |result_page|
72
80
  result_page['Items']['$values'].map { it.except('$type') }.each(&)
73
81
  end
74
82
  end
@@ -76,14 +84,16 @@ module Usps
76
84
  nil
77
85
  end
78
86
 
79
- # Fetch a raw query page
87
+ # Fetch a filtered query page, and update the current offset
80
88
  #
81
- def fetch = JSON.parse(submit(uri, authorize(http_get)).body)
89
+ def page = fetch_next['Items']['$values'].map { iqa? ? it.except('$type') : Imis::Data[it] }
82
90
 
83
91
  # Fetch the next raw query page, and update the current offset
84
92
  #
85
93
  def fetch_next
86
- logger.info 'Fetching IQA Query page'
94
+ return unless page?
95
+
96
+ logger.info "Fetching #{query_type} Query page"
87
97
 
88
98
  result = fetch
89
99
 
@@ -94,30 +104,44 @@ module Usps
94
104
  JSON.pretty_generate(result).split("\n").each { logger.debug " -> #{it}" }
95
105
 
96
106
  @offset = result['NextOffset']
107
+ @next_page = result['HasNext']
97
108
 
98
109
  result
99
110
  end
100
111
 
112
+ # Fetch a raw query page
113
+ #
114
+ def fetch = JSON.parse(submit(uri, authorize(http_get)).body)
115
+
116
+ # Reset query paging progress
117
+ #
118
+ def reset!
119
+ return if next_page.nil?
120
+
121
+ logger.debug 'Resetting progress'
122
+
123
+ @count = 0
124
+ @offset = 0
125
+ @next_page = nil
126
+ end
127
+
101
128
  # Ruby 3.5 instance variable filter
102
129
  #
103
130
  def instance_variables_to_inspect = instance_variables - %i[@api]
104
131
 
105
132
  private
106
133
 
107
- def path_params = query_params.merge(QueryName: query_name).merge({ Offset: offset, Limit: page_size }.compact)
108
- def path = "#{QUERY_PATH}?#{path_params.to_query}"
109
- def uri = URI(File.join(Imis.configuration.hostname, path))
110
-
111
- def logger = Imis.logger('Query')
134
+ # Only skip if explicitly set
135
+ def page? = next_page.nil? || next_page
112
136
 
113
- def reset!
114
- logger.debug 'Resetting Query progress'
115
-
116
- @count = 0
117
- @offset = 0
137
+ def iqa? = query_name.match?(/^\$/)
138
+ def query_type = iqa? ? :IQA : :'Business Object'
139
+ def path_params = query_params.merge({ Offset: offset, Limit: page_size }.compact)
140
+ def endpoint = iqa? ? IQA_PATH : "#{Imis::BusinessObject::API_PATH}/#{query_name}"
141
+ def path = "#{endpoint}?#{path_params.to_query}"
142
+ def uri = URI(File.join(Imis.configuration.hostname, path))
118
143
 
119
- { 'HasNext' => true }
120
- end
144
+ def logger = Imis.logger("#{query_type} Query")
121
145
  end
122
146
  end
123
147
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.9.15'
5
+ VERSION = '0.10.0'
6
6
  end
7
7
  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.9.15
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -40,6 +40,7 @@ files:
40
40
  - lib/usps/imis/errors/config_error.rb
41
41
  - lib/usps/imis/errors/locked_id_error.rb
42
42
  - lib/usps/imis/errors/mapper_error.rb
43
+ - lib/usps/imis/errors/missing_id_error.rb
43
44
  - lib/usps/imis/errors/not_found_error.rb
44
45
  - lib/usps/imis/errors/panel_unimplemented_error.rb
45
46
  - lib/usps/imis/errors/response_error.rb