usps-imis-api 0.11.16 → 0.11.18

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: b6437192db35a0e8a3e80d44d338366c9460060dc7b13d8f53a17c0d0c7ed8b8
4
- data.tar.gz: 4c4ad6121f1f282e3788e311e78be6d753b56c04f8112a3ac6a23c808e198ca2
3
+ metadata.gz: 76cd84893c77f63cfa3e49c3442a646898ffa3f900a4013c017598099fd9aec5
4
+ data.tar.gz: 86d849a56392e78764036c132b10ebf68515b6647fbf6a9c4cdc394e01cfbe5d
5
5
  SHA512:
6
- metadata.gz: 5aa9c0cac48c98eb3826c107e9db3a471689c6e30cc3d7fbce138db24f0c01128440b4e1fcc8b46bd3e96bd002ce0f4ce2136e9d678b60023af5b66330e55ceb
7
- data.tar.gz: 8fa27434be1d69626b27653189636c4daeb1739407752d24a977314c70ac09029ab624f618c007a78669f8f8d8875b8a63646cb22e894ad87cd62a5c3fbeae20
6
+ metadata.gz: 005bccd46409ddfa00899ab7709c1a60ce3576ea856f601f588dcfb9baa0163ca256ae193c51a103cff15db696a2c98b9b048359708ce5e168f98aeb8d6f5acc
7
+ data.tar.gz: 758d22069c2d8a72cb540ed3aea7db0be5bb6d472f9100db26d19116f65ddd7bb1c60ba927cfc78f72469af65f68e2f7a2e4fd053f1d381517493b59e9feffcb
data/lib/usps/imis/api.rb CHANGED
@@ -34,12 +34,17 @@ module Usps
34
34
  #
35
35
  attr_reader :lock_imis_id
36
36
 
37
+ # Tagged logger
38
+ #
39
+ attr_reader :logger
40
+
37
41
  # A new instance of +Api+
38
42
  #
39
43
  # @param imis_id [Integer, String] iMIS ID to select immediately on initialization
40
44
  #
41
45
  def initialize(imis_id: nil)
42
46
  self.imis_id = imis_id if imis_id
47
+ @logger ||= Imis.logger('Api')
43
48
  end
44
49
 
45
50
  # Manually set the current ID, if you already have it for a given member
@@ -66,8 +71,8 @@ module Usps
66
71
  logger.debug "Fetching iMIS ID for #{certificate}"
67
72
 
68
73
  begin
69
- result = query(Imis.configuration.imis_id_query_name, { certificate: }).tap { logger.debug it }
70
- page = result.page.tap { logger.debug it }
74
+ result = query(Imis.configuration.imis_id_query_name, { certificate: })
75
+ page = result.page.tap { logger.tagged('Response').debug it }
71
76
  self.imis_id = page.first['ID'].to_i
72
77
  rescue StandardError
73
78
  raise Errors::NotFoundError, 'Member not found'
@@ -191,21 +196,23 @@ module Usps
191
196
 
192
197
  private
193
198
 
194
- def logger = Imis.logger('Api')
195
-
196
199
  # Authenticate to the iMIS API, and store the access token and expiration time
197
200
  #
198
201
  def authenticate
199
- logger.debug 'Authenticating with iMIS'
200
-
201
- request = http_post
202
- request.body = URI.encode_www_form(
203
- grant_type: 'password',
204
- username: Imis.configuration.username,
205
- password: Imis.configuration.password
206
- )
207
- result = submit(uri, request)
208
- json = JSON.parse(result.body)
202
+ json = nil # Scope
203
+
204
+ logger.tagged('Auth') do
205
+ logger.info 'Authenticating with iMIS'
206
+
207
+ request = http_post
208
+ request.body = URI.encode_www_form(
209
+ grant_type: 'password',
210
+ username: Imis.configuration.username,
211
+ password: Imis.configuration.password
212
+ )
213
+ result = submit(uri, request)
214
+ json = JSON.parse(result.body)
215
+ end
209
216
 
210
217
  @token = json['access_token']
211
218
  @token_expiration = Time.now + json['expires_in'] - 60
@@ -25,12 +25,17 @@ module Usps
25
25
  #
26
26
  attr_reader :ordinal
27
27
 
28
+ # Tagged logger
29
+ #
30
+ attr_reader :logger
31
+
28
32
  # A new instance of +BusinessObject+
29
33
  #
30
34
  def initialize(api, business_object_name, ordinal: nil)
31
35
  @api = api
32
36
  @business_object_name = business_object_name
33
37
  @ordinal = ordinal
38
+ @logger ||= Imis.logger('BusinessObject')
34
39
  end
35
40
 
36
41
  # Run a query on the entire business object
@@ -132,12 +137,10 @@ module Usps
132
137
 
133
138
  # Ruby 3.5 instance variable filter
134
139
  #
135
- def instance_variables_to_inspect = instance_variables - %i[@api]
140
+ def instance_variables_to_inspect = instance_variables - %i[@api @logger]
136
141
 
137
142
  private
138
143
 
139
- def logger = Imis.logger('BusinessObject')
140
-
141
144
  # Construct a business object API endpoint address
142
145
  #
143
146
  def uri(id: nil)
@@ -186,7 +189,7 @@ module Usps
186
189
 
187
190
  response = submit(uri, authorize(http_get))
188
191
  result = Data.from_json(response.body)
189
- JSON.pretty_generate(result).split("\n").each { logger.debug " -> #{it}" }
192
+ logger.json result
190
193
  result
191
194
  end
192
195
 
@@ -198,7 +201,7 @@ module Usps
198
201
  request.body = JSON.dump(body)
199
202
  response = submit(uri, authorize(request))
200
203
  result = Data.from_json(response.body)
201
- JSON.pretty_generate(result).split("\n").each { logger.debug " -> #{it}" }
204
+ logger.json result
202
205
  result
203
206
  end
204
207
  end
@@ -8,8 +8,14 @@ module Usps
8
8
  class Interface
9
9
  NAME = 'USPS iMIS API - Ruby'
10
10
 
11
+ # Options passed in from the command line
12
+ #
11
13
  attr_reader :options
12
14
 
15
+ # Tagged logger
16
+ #
17
+ attr_reader :logger
18
+
13
19
  def self.run(...) = new(...).run
14
20
 
15
21
  def initialize(**)
@@ -17,9 +23,14 @@ module Usps
17
23
  validate_options!
18
24
  configure! if options[:config]
19
25
  logging!
26
+ @logger ||= Imis.logger('CommandLine')
20
27
  end
21
28
 
22
29
  def run
30
+ logger.info 'Running'
31
+ logger.debug 'CLI Options:'
32
+ logger.json options
33
+
23
34
  set_member
24
35
 
25
36
  result = simplify(perform!)
@@ -60,6 +71,8 @@ module Usps
60
71
  else
61
72
  # Query
62
73
  end
74
+
75
+ logger.debug "iMIS ID: #{api.imis_id}"
63
76
  end
64
77
 
65
78
  # rubocop:disable Metrics
@@ -118,8 +131,10 @@ module Usps
118
131
 
119
132
  # Hash includes Usps::Imis::Data
120
133
  if data.is_a?(Hash) && !options[:business_objects]
134
+ logger.debug 'Returning simplified Data#properties'
121
135
  data.properties(include_ids: options[:include_ids])
122
136
  elsif data.is_a?(Array) && data.all? { it.is_a?(Hash) }
137
+ logger.debug 'Returning simplified Array<Data#properties>'
123
138
  data.map { it.properties(include_ids: options[:include_ids]) }
124
139
  else
125
140
  data
@@ -128,7 +143,10 @@ module Usps
128
143
 
129
144
  # :nocov:
130
145
  def output(&block)
131
- return if ENV.fetch('SUPPRESS_OUTPUT', false)
146
+ if ENV.fetch('SUPPRESS_OUTPUT', false)
147
+ logger.debug 'Output suppressed'
148
+ return
149
+ end
132
150
 
133
151
  puts JSON.dump(block.call) if block_given?
134
152
  end
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'logger'
4
+ require_relative 'logger_formatter'
5
+ require_relative 'logger_helpers'
6
+
3
7
  module Usps
4
8
  module Imis
5
9
  # API Configuration
@@ -16,7 +20,8 @@ module Usps
16
20
  @imis_id_query_name = ENV.fetch('IMIS_ID_QUERY_NAME', nil)
17
21
  @username = ENV.fetch('IMIS_USERNAME', nil)
18
22
  @password = ENV.fetch('IMIS_PASSWORD', nil)
19
- @logger = ActiveSupport::TaggedLogging.new(Logger.new($stdout, level: :info))
23
+ @base_logger = Logger.new($stdout, level: :info)
24
+ @logger = ActiveSupport::TaggedLogging.new(@base_logger)
20
25
 
21
26
  yield self if block_given?
22
27
 
@@ -28,7 +33,9 @@ module Usps
28
33
  end
29
34
 
30
35
  def logger=(logger)
31
- @logger = ActiveSupport::TaggedLogging.new(logger)
36
+ @base_logger = logger.tap { it.formatter = LoggerFormatter.new }
37
+ @base_logger.singleton_class.include(LoggerHelpers)
38
+ @logger = ActiveSupport::TaggedLogging.new(@base_logger)
32
39
  end
33
40
 
34
41
  def silence!
@@ -48,7 +55,7 @@ module Usps
48
55
 
49
56
  # Ruby 3.5 instance variable filter
50
57
  #
51
- def instance_variables_to_inspect = instance_variables - %i[@password @logger]
58
+ def instance_variables_to_inspect = instance_variables - %i[@password @base_logger @logger]
52
59
 
53
60
  # Parameters to filter out of logging
54
61
  #
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'logger_formatter'
4
+ require_relative 'logger_helpers'
5
+
6
+ module Usps
7
+ module Imis
8
+ # Formatted logger with additional helpers
9
+ #
10
+ class Logger < ::Logger
11
+ include LoggerHelpers
12
+
13
+ def initialize(...)
14
+ super
15
+ self.formatter = LoggerFormatter.new
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ # Formats log statements
6
+ #
7
+ class LoggerFormatter < ::Logger::Formatter
8
+ def call(severity, time, _progname, message)
9
+ log_chunks = [
10
+ format('%-5s', severity.to_s),
11
+ "[#{$PROCESS_ID}]",
12
+ "[#{time.strftime('%Y-%m-%d %H:%M:%S %Z')}]",
13
+ 'iMIS Ruby API',
14
+ '|',
15
+ formatted_tags,
16
+ '|',
17
+ message.sub(/^#{Regexp.escape(tags_text)}/, '')
18
+ ]
19
+ "#{log_chunks.join(' ')}\n"
20
+ end
21
+
22
+ private
23
+
24
+ def formatted_tags
25
+ tags = current_tags || []
26
+ tags&.any? ? tags.join(' | ') : ''
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ # Formatted logger helpers
6
+ #
7
+ module LoggerHelpers
8
+ def multiline(string)
9
+ string.split("\n").each { |line| debug(line) }
10
+ end
11
+
12
+ def json(data)
13
+ tagged('JSON') { multiline(JSON.pretty_generate(data)) }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -21,11 +21,16 @@ module Usps
21
21
  #
22
22
  attr_reader :api
23
23
 
24
+ # Tagged logger
25
+ #
26
+ attr_reader :logger
27
+
24
28
  # A new instance of +Mapper+
25
29
  #
26
30
  def initialize(api = nil, imis_id: nil)
27
31
  @api = api || Api.new
28
32
  @api.imis_id = imis_id if imis_id
33
+ @logger ||= Imis.logger('Mapper')
29
34
  end
30
35
 
31
36
  # Get a member's data for a specific field by arbitrary field name
@@ -107,12 +112,10 @@ module Usps
107
112
 
108
113
  # Ruby 3.5 instance variable filter
109
114
  #
110
- def instance_variables_to_inspect = instance_variables - %i[@api]
115
+ def instance_variables_to_inspect = instance_variables - %i[@api @logger]
111
116
 
112
117
  private
113
118
 
114
- def logger = Imis.logger('Mapper')
115
-
116
119
  def map_update(field_key)
117
120
  missing_mapping!(field_key) unless FIELD_MAPPING.key?(field_key.to_sym)
118
121
 
@@ -10,9 +10,14 @@ module Usps
10
10
  #
11
11
  attr_reader :api
12
12
 
13
+ # Tagged logger
14
+ #
15
+ attr_reader :logger
16
+
13
17
  def initialize(api = nil, imis_id: nil)
14
18
  @api = api || Api.new
15
19
  @api.imis_id = imis_id if imis_id
20
+ @logger ||= Imis.logger('Panel')
16
21
  end
17
22
 
18
23
  # Get a specific object from the Panel
@@ -100,12 +105,10 @@ module Usps
100
105
 
101
106
  # Ruby 3.5 instance variable filter
102
107
  #
103
- def instance_variables_to_inspect = instance_variables - %i[@api]
108
+ def instance_variables_to_inspect = instance_variables - %i[@api @logger]
104
109
 
105
110
  private
106
111
 
107
- def logger = Imis.logger('Panel')
108
-
109
112
  def business_object
110
113
  raise Errors::PanelUnimplementedError.from(self.class.name, 'business_object')
111
114
  end
@@ -40,6 +40,10 @@ module Usps
40
40
  #
41
41
  attr_reader :next_page
42
42
 
43
+ # Tagged logger
44
+ #
45
+ attr_reader :logger
46
+
43
47
  # A new instance of +Query+
44
48
  #
45
49
  # @param api [Api] Parent to use for making requests
@@ -55,8 +59,13 @@ module Usps
55
59
  @page_size = page_size
56
60
  @offset = offset
57
61
  @count = 0
62
+ @logger ||= Imis.logger('Query', query_type)
58
63
 
59
- logger.debug "URI: #{uri}"
64
+ logger.tagged('Name').debug query_name
65
+ logger.tagged('Params').json query_params
66
+ logger.tagged('URI').debug uri
67
+ logger.tagged('Page Size').debug page_size
68
+ logger.tagged('Offset').debug offset.to_i
60
69
  end
61
70
 
62
71
  # Iterate through all results from the query
@@ -92,9 +101,9 @@ module Usps
92
101
 
93
102
  @count += result['Count'] || 0
94
103
  total = result['TotalCount']
95
- logger.info " -> #{@count} / #{total} #{'item'.pluralize(total)}"
96
- logger.debug ' -> Query page data:'
97
- JSON.pretty_generate(result).split("\n").each { logger.debug " -> #{it}" }
104
+ logger.info "#{@count} / #{total} #{'item'.pluralize(total)}"
105
+ logger.debug 'Query page data:'
106
+ logger.json result
98
107
 
99
108
  @offset = result['NextOffset']
100
109
  @next_page = result['HasNext']
@@ -120,7 +129,7 @@ module Usps
120
129
 
121
130
  # Ruby 3.5 instance variable filter
122
131
  #
123
- def instance_variables_to_inspect = instance_variables - %i[@api]
132
+ def instance_variables_to_inspect = instance_variables - %i[@api @logger]
124
133
 
125
134
  private
126
135
 
@@ -139,8 +148,6 @@ module Usps
139
148
  "#{Imis::BusinessObject::API_PATH}/#{query_name}?#{path_params.to_query}"
140
149
  end
141
150
  end
142
-
143
- def logger = Imis.logger("#{query_type} Query")
144
151
  end
145
152
  end
146
153
  end
@@ -7,7 +7,7 @@ module Usps
7
7
  module Requests
8
8
  private
9
9
 
10
- def logger = Imis.logger
10
+ def logger = raise("#{self.class.name} must implement #logger")
11
11
 
12
12
  def token = api.token
13
13
  def token_expiration = api.token_expiration
@@ -39,12 +39,15 @@ module Usps
39
39
 
40
40
  def submit(uri, request)
41
41
  logger.info 'Submitting request to iMIS'
42
- logger.debug " -> #{request.class.name.demodulize.upcase} #{uri}"
43
- sanitized_request_body(request).split("\n").each { logger.debug " -> #{it}" }
44
- client(uri).request(request).tap do |result|
45
- raise Errors::ResponseError.from(result) unless result.is_a?(Net::HTTPSuccess)
42
+ logger.tagged('Request') do
43
+ logger.debug "#{request.class.name.demodulize.upcase} #{uri}"
44
+ logger.multiline sanitized_request_body(request)
46
45
 
47
- logger.info 'Request succeeded'
46
+ client(uri).request(request).tap do |result|
47
+ raise Errors::ResponseError.from(result) unless result.is_a?(Net::HTTPSuccess)
48
+
49
+ logger.info 'Request succeeded'
50
+ end
48
51
  end
49
52
  end
50
53
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.11.16'
5
+ VERSION = '0.11.18'
6
6
  end
7
7
  end
data/lib/usps/imis.rb CHANGED
@@ -51,7 +51,7 @@ module Usps
51
51
 
52
52
  # Logger (with optional nested tags) to write to
53
53
  #
54
- def logger(*tags) = configuration.logger.tagged('iMIS', *tags)
54
+ def logger(*) = configuration.logger.tagged(*)
55
55
 
56
56
  # Gem version
57
57
  #
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.11.16
4
+ version: 0.11.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -93,6 +93,9 @@ files:
93
93
  - lib/usps/imis/errors/panel_unimplemented_error.rb
94
94
  - lib/usps/imis/errors/response_error.rb
95
95
  - lib/usps/imis/errors/unexpected_property_type_error.rb
96
+ - lib/usps/imis/logger.rb
97
+ - lib/usps/imis/logger_formatter.rb
98
+ - lib/usps/imis/logger_helpers.rb
96
99
  - lib/usps/imis/mapper.rb
97
100
  - lib/usps/imis/mocks.rb
98
101
  - lib/usps/imis/mocks/business_object.rb