usps-imis-api 0.11.34 → 0.11.36

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: 898b26512f82544ff037e289bfe2466ac375bfa481a0c7ebf77d5d6c2d9ecf96
4
- data.tar.gz: 1a01816c119999a2ca2e8b01ae75c83df028a9d2cc1542b174b23a2e49f2877e
3
+ metadata.gz: 27d14e895f15b3dc94ab5d32f07f52855253b412d0f09e62925ae2833664269e
4
+ data.tar.gz: 3c2f8d03ae31e1b4372f5ae16047ad09c7401fee5831e62c3f72e2e5517761cd
5
5
  SHA512:
6
- metadata.gz: 89ff926322610bc6eefaf4aa605d7ecc498d1240f577a39e8a3267ab923d744f6364c73da84afeb65eb8672114d315a043c7a5d3d492b10f788c5734f1be13b4
7
- data.tar.gz: 5858b73de1805cc9ee93527964b9220c2bd3c7b91075913103adff9a36994e018f8ac2c40404e80d8afeda314c3e7d4e6ab6e5ea4ba82c12022d67347c3a6bd3
6
+ metadata.gz: 0f67ef6dd5d2dc56a8fcb982498c7766eb6b6a7ea26e340f7fe358cb88d694e3d1a4c392da59660fd6ddb134bc09c1e9b3394609af7dcf8ba388d203fa33f51a
7
+ data.tar.gz: a6bf0462662992192b01cac14124b640dd3ccf7ad56637719cf2b71558cdf3198a89df6110b37e16fac56b4aecfd6d0ca4659fef0f598a28774e11bbaf2bd191
data/Readme.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  Run this command:
8
8
 
9
- ```ruby
9
+ ```sh
10
10
  gem install usps-imis-api
11
11
  ```
12
12
 
@@ -36,6 +36,7 @@ Usps::Imis.configure do |config|
36
36
  # These options will use these defaults
37
37
  config.logger = ActiveSupport::TaggedLogging.new(Logger.new($stdout))
38
38
  config.logger.level = :info
39
+ config.global_log_path = '/var/log/imis/imis.log'
39
40
  end
40
41
  ```
41
42
 
data/lib/usps/imis/api.rb CHANGED
@@ -12,6 +12,8 @@ module Usps
12
12
  class Api
13
13
  include Requests
14
14
 
15
+ NAME = 'USPS iMIS API - Ruby'
16
+
15
17
  # Endpoint for (re-)authentication requests
16
18
  #
17
19
  AUTHENTICATION_PATH = 'Token'
@@ -244,6 +246,10 @@ module Usps
244
246
  { token: @token, token_expiration: @token_expiration }
245
247
  end
246
248
 
249
+ # Name and version of the API
250
+ #
251
+ def version = "#{NAME} (v#{Imis::VERSION})"
252
+
247
253
  # Ruby 3.5 instance variable filter
248
254
  #
249
255
  def instance_variables_to_inspect = %i[@token_expiration @imis_id]
@@ -10,7 +10,16 @@ module Usps
10
10
  class Interface
11
11
  include Performers
12
12
 
13
- NAME = 'USPS iMIS API - Ruby'
13
+ # Prioritized list of config file paths to automatically check if not provided
14
+ #
15
+ CONFIG_PATHS = {
16
+ local_dot: File.join(Dir.pwd, '.imis.yml'),
17
+ local: File.join(Dir.pwd, 'imis.yml'),
18
+ local_dot_config: File.join(Dir.pwd, '.config', 'imis.yml'),
19
+ local_config: File.join(Dir.pwd, 'config', 'imis.yml'),
20
+ user: "#{Dir.home}/.config/imis.yml",
21
+ system: '/usr/local/imis/config.yml'
22
+ }.freeze
14
23
 
15
24
  # CLI options that indicate the response is a raw Hash rather than a Data object,
16
25
  # and should not be simplified
@@ -39,7 +48,7 @@ module Usps
39
48
  @options = input_options.merge(**)
40
49
  options[:version] = true if default_options?
41
50
 
42
- configure! if options[:config]
51
+ configure!
43
52
  logging!
44
53
  @logger ||= Imis.logger('CommandLine')
45
54
  end
@@ -111,7 +120,7 @@ module Usps
111
120
  # Hash includes Usps::Imis::Data
112
121
  logger.debug 'Returning simplified Data#properties'
113
122
  data.properties(include_ids: options[:include_ids])
114
- elsif data.is_a?(Array) && data.all? { it.is_a?(Hash) }
123
+ elsif data.is_a?(Array) && data.all?(Hash)
115
124
  logger.debug 'Returning simplified Array<Data#properties>'
116
125
  data.map { it.properties(include_ids: options[:include_ids]) }
117
126
  else
@@ -141,15 +150,26 @@ module Usps
141
150
  # Supports YAML or JSON config data
142
151
  #
143
152
  def configure!
144
- config_data = YAML.safe_load_file(options[:config])
153
+ return if config_path.nil?
154
+
155
+ config_data = YAML.safe_load_file(config_path)
145
156
 
146
157
  Usps::Imis.configure do |config|
147
- config_data.each do |key, value|
158
+ global_log!(config)
159
+
160
+ config_data&.each do |key, value|
148
161
  config.public_send("#{key}=", value)
149
162
  end
150
163
  end
151
164
  end
152
165
 
166
+ def config_path
167
+ return @config_path if @config_path
168
+ return @config_path = CONFIG_PATHS.values.find { File.exist?(it) } if options[:config].nil?
169
+
170
+ @config_path = CONFIG_PATHS[options[:config].to_sym] || options[:config]
171
+ end
172
+
153
173
  def logging!
154
174
  Usps::Imis.configure do |config|
155
175
  # :nocov:
@@ -166,6 +186,21 @@ module Usps
166
186
  end
167
187
  end
168
188
 
189
+ def global_log!(config)
190
+ # :nocov:
191
+ return if ENV['TESTING']
192
+
193
+ time = DateTime.now
194
+ log_path = [
195
+ time.year,
196
+ time.month,
197
+ time.day,
198
+ time.strftime('%Y-%m-%dT%H-%M-%S%z')
199
+ ].join('/')
200
+ config.global_log_path = "/var/log/imis/cli/#{log_path}.log"
201
+ # :nocov:
202
+ end
203
+
169
204
  def default_options? = options[:log_level] == 'info' && options.except(:log_level).values.none?
170
205
  end
171
206
  end
@@ -29,14 +29,24 @@ module Usps
29
29
  ordinal: ['Ordinal ID within a Panel', { type: :integer }],
30
30
  field: ['Specific field to return or update', { type: :string }],
31
31
  fields: ['Specific field(s) to return', { type: :strings, short: :F }],
32
- data: ['JSON string input', { type: :string }],
32
+ data: ["JSON string input -- #{'STDIN'.red} takes priority", { type: :string }],
33
33
 
34
34
  # Iteractions for supporting other language wrappers
35
35
  auth_token: ['Return an auth token for other language wrappers', { short: :T }],
36
36
  token: ['Provide an existing auth token', { type: :string }],
37
37
 
38
38
  # General config
39
- config: ['Path to the JSON/YAML config file to use', { type: :string, short: :C }],
39
+ config: [
40
+ 'Path to the JSON/YAML config file to use, or one of the following preset options: ' \
41
+ "`#{'local'.yellow}`, " \
42
+ "`#{'local_dot_config'.yellow}`, " \
43
+ "`#{'local_config'.yellow}`, " \
44
+ "`#{'user'.yellow}`, " \
45
+ "`#{'system'.yellow}`. " \
46
+ 'If no option is provided, the first matching preset option will be automatically used.',
47
+ { type: :string, short: :C }
48
+ ],
49
+ show_config: ['Return the active config file path', { short: :X }],
40
50
  raw: ['Return raw JSON output, rather than simplified data', { short: :R }],
41
51
  include_ids: ["Include any #{'iMIS ID'.yellow} and #{'Ordinal'.yellow} properties in returned data"],
42
52
  jsonl: ['Format array output as JSONL', { short: :j }],
@@ -48,7 +58,7 @@ module Usps
48
58
  CONFLICTING_OPTION_GROUPS = [
49
59
  %i[certificate id uuid],
50
60
  %i[record_id uuid],
51
- %i[on panel query mapper map business_objects auth_token],
61
+ %i[on panel query mapper map business_objects auth_token show_config],
52
62
  %i[field fields map query],
53
63
  %i[raw include_ids],
54
64
  %i[quiet log_level],
@@ -19,7 +19,8 @@ module Usps
19
19
  in business_objects: true then api.business_objects
20
20
  in auth_token: true then api.auth_token
21
21
  in certificate: then api.imis_id
22
- in version: true then "#{Interface::NAME} (v#{Imis::VERSION})"
22
+ in show_config: then config_path
23
+ in version: true then api.version
23
24
  end
24
25
  rescue NoMatchingPatternError => e
25
26
  raise Errors::CommandLineError, "Unable to match pattern from options: #{e.message}"
@@ -36,8 +37,8 @@ module Usps
36
37
  # Nothing to convert
37
38
  end
38
39
 
39
- # Remove mapper flag when false
40
- converted.delete(:mapper) unless converted[:mapper]
40
+ # Remove flags when false
41
+ %i[mapper show_config].each { converted.delete(it) unless converted[it] }
41
42
  end
42
43
  end
43
44
 
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'fileutils'
4
+
3
5
  require_relative 'logger'
4
6
  require_relative 'logger_formatter'
5
7
  require_relative 'logger_helpers'
@@ -45,6 +47,23 @@ module Usps
45
47
  @logger = ActiveSupport::TaggedLogging.new(@base_logger)
46
48
  end
47
49
 
50
+ def global_logger
51
+ return @global_logger if @global_logger
52
+
53
+ global_log_path
54
+ @global_logger
55
+ end
56
+
57
+ def global_log_path
58
+ @global_log_path || self.global_log_path = '/var/log/imis/imis.log'
59
+ end
60
+
61
+ def global_log_path=(path)
62
+ @global_log_path = path
63
+ @global_logger = build_global_logger
64
+ global_log_path
65
+ end
66
+
48
67
  def silence!
49
68
  self.logger = Logger.new(nil)
50
69
  end
@@ -62,7 +81,7 @@ module Usps
62
81
 
63
82
  # Ruby 3.5 instance variable filter
64
83
  #
65
- def instance_variables_to_inspect = instance_variables - %i[@password @base_logger @logger]
84
+ def instance_variables_to_inspect = instance_variables - %i[@password @base_logger @logger @global_logger]
66
85
 
67
86
  # Parameters to filter out of logging
68
87
  #
@@ -82,6 +101,23 @@ module Usps
82
101
 
83
102
  ActiveSupport::StringInquirer.new(ENV.fetch('IMIS_ENVIRONMENT', 'development'))
84
103
  end
104
+
105
+ def build_global_logger
106
+ writable =
107
+ begin
108
+ FileUtils.mkdir_p(File.dirname(global_log_path))
109
+ File.writable?(global_log_path) || File.writable?(File.dirname(global_log_path))
110
+ rescue StandardError
111
+ false
112
+ end
113
+ return ActiveSupport::TaggedLogging.new(::Logger.new(nil)) unless writable
114
+
115
+ ActiveSupport::TaggedLogging.new(
116
+ ::Logger
117
+ .new(File.open(global_log_path, 'a'), level: :debug)
118
+ .tap { it.formatter = LoggerFormatter.new }
119
+ )
120
+ end
85
121
  end
86
122
  end
87
123
  end
@@ -14,6 +14,15 @@ module Usps
14
14
  super
15
15
  self.formatter = LoggerFormatter.new
16
16
  end
17
+
18
+ # Also send logger methods to the global logger
19
+ #
20
+ %w[debug info warn error fatal unknown].each do |method|
21
+ define_method(method) do |message|
22
+ super(message)
23
+ Usps::Imis.config.global_logger.tagged(formatter.current_tags).public_send(method, message)
24
+ end
25
+ end
17
26
  end
18
27
  end
19
28
  end
@@ -81,7 +81,7 @@ module Usps
81
81
 
82
82
  if %w[Addresses AlternateIds CommunicationPreferences Emails Salutations].include?(key)
83
83
  hash[key] = value.map { format_extracted(it.to_h) }
84
- elsif value.all? { it.is_a?(String) }
84
+ elsif value.all?(String)
85
85
  # Do nothing
86
86
  else
87
87
  hash[key] = format_extracted(value.to_h)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.11.34'
5
+ VERSION = '0.11.36'
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.11.34
4
+ version: 0.11.36
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
133
  - !ruby/object:Gem::Version
134
134
  version: '0'
135
135
  requirements: []
136
- rubygems_version: 4.0.3
136
+ rubygems_version: 4.0.9
137
137
  specification_version: 4
138
138
  summary: iMIS API Wrapper
139
139
  test_files: []