usps-imis-api 0.11.19 → 0.11.21

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: 0aea0bbaf9d56d6b87c79ea640e60c796bed4349b9aff10523882fa69b5f8fc5
4
- data.tar.gz: a30ff5cd43dc7b0d60b79e7184f9707124e37f315eb8f64588801cf09664f655
3
+ metadata.gz: 35b16ade1f6397b66b62731eb83a514ef1fa4a1e4ef656f639a153ecb1bbc8a5
4
+ data.tar.gz: b0b191774bd37dcea2342ff8fa5349a06bfb62de69cb1835316bc522367bbda9
5
5
  SHA512:
6
- metadata.gz: 93736a5bd1693f879294910ef3908e03fbb002dc271152a636aa76743b9f390ff31f9d8fba054ed4244a27212640dba4e373252aefe80618d41fb897ef456ced
7
- data.tar.gz: 8041ac9e86d6e62da036110ca27ecb3f57709039d9770d2317d710817b40f2a5ff80568bc0611846c675b292e423827031cd4f4c14b9cb00a5b7553bced490d0
6
+ metadata.gz: 7604ac9a7672a37fa47cd6d5585d54e54f2a5bcf8853fb4200ceb54c20db0c24e16b5036f94ced091fa9523cf2157934bb840c6f171ec4b567113f50f1423ea3
7
+ data.tar.gz: cc021585df733a3dc596cbd54486655b297290665128b1f4fa362976e9274d2b4b572ae88417f3fd17f9cd33a3352e62a64726e9c9357e1801629be93c150b94
data/Readme.md CHANGED
@@ -82,5 +82,7 @@ file to [rubygems.org](https://rubygems.org).
82
82
 
83
83
  ## PHP
84
84
 
85
- This same API is
86
- [available for PHP](https://github.com/unitedstatespowersquadrons/imis-api).
85
+ A wrapper for the CLI from this API is
86
+ [available for PHP](https://github.com/unitedstatespowersquadrons/imis-api-php).
87
+
88
+ You can also view the [original PHP API](https://github.com/unitedstatespowersquadrons/imis-api).
data/lib/usps/imis/api.rb CHANGED
@@ -38,6 +38,17 @@ module Usps
38
38
  #
39
39
  attr_reader :logger
40
40
 
41
+ # Create a new instance of +Api+ and provide an existing auth token
42
+ #
43
+ # @param token [String] Auth token
44
+ #
45
+ def self.with_token(token)
46
+ new.tap do |api|
47
+ api.instance_variable_set(:@token, token)
48
+ api.instance_variable_set(:@token_expiration, Time.now + 3600) # Greater than the actual lifetime of the token
49
+ end
50
+ end
51
+
41
52
  # A new instance of +Api+
42
53
  #
43
54
  # @param imis_id [Integer, String] iMIS ID to select immediately on initialization
@@ -191,6 +202,14 @@ module Usps
191
202
  @panels ||= Panels.all(self)
192
203
  end
193
204
 
205
+ # Used by the PHP Wrapper to reduce authentication requests
206
+ #
207
+ def auth_token
208
+ authenticate
209
+
210
+ { token: @token, token_expiration: @token_expiration }
211
+ end
212
+
194
213
  # Ruby 3.5 instance variable filter
195
214
  #
196
215
  def instance_variables_to_inspect = %i[@token_expiration @imis_id]
@@ -8,6 +8,11 @@ module Usps
8
8
  class Interface
9
9
  NAME = 'USPS iMIS API - Ruby'
10
10
 
11
+ # CLI options that indicate the response is a raw Hash rather than a Data object,
12
+ # and should not be simplified
13
+ #
14
+ RAW_HASH_RESPONSE_OPTIONS = %i[business_objects auth_token].freeze
15
+
11
16
  # Options passed in from the command line
12
17
  #
13
18
  attr_reader :options
@@ -29,7 +34,7 @@ module Usps
29
34
  def run
30
35
  logger.info 'Running'
31
36
  logger.debug 'CLI Options:'
32
- logger.json options
37
+ logger.json(options.dup.tap { it[:token] = '[FILTERED]' if it[:token] })
33
38
 
34
39
  set_member
35
40
 
@@ -61,7 +66,12 @@ module Usps
61
66
  # :nocov:
62
67
 
63
68
  def api
64
- @api ||= Usps::Imis::Api.new
69
+ @api ||=
70
+ if options[:token]
71
+ Usps::Imis::Api.with_token(options[:token])
72
+ else
73
+ Usps::Imis::Api.new
74
+ end
65
75
  end
66
76
 
67
77
  def set_member
@@ -72,7 +82,7 @@ module Usps
72
82
  # Query
73
83
  end
74
84
 
75
- logger.debug "iMIS ID: #{api.imis_id}"
85
+ logger.debug "iMIS ID: #{api.imis_id}" if api.imis_id
76
86
  end
77
87
 
78
88
  # rubocop:disable Metrics
@@ -103,6 +113,10 @@ module Usps
103
113
  in query: then api.query(query)
104
114
 
105
115
  in business_objects: true then api.business_objects
116
+
117
+ in auth_token: true then api.auth_token
118
+
119
+ in certificate: then api.imis_id
106
120
  end
107
121
  rescue NoMatchingPatternError => e
108
122
  raise Errors::CommandLineError, "Unable to match pattern from options: #{e.message}"
@@ -127,10 +141,10 @@ module Usps
127
141
 
128
142
  def simplify(data)
129
143
  return data.to_a if data.is_a?(Query)
130
- return data if options[:raw]
144
+ return data if options[:raw] || RAW_HASH_RESPONSE_OPTIONS.any? { options[it] }
131
145
 
132
146
  # Hash includes Usps::Imis::Data
133
- if data.is_a?(Hash) && !options[:business_objects]
147
+ if data.is_a?(Hash)
134
148
  logger.debug 'Returning simplified Data#properties'
135
149
  data.properties(include_ids: options[:include_ids])
136
150
  elsif data.is_a?(Array) && data.all? { it.is_a?(Hash) }
@@ -29,6 +29,10 @@ module Usps
29
29
  fields: ['Specific field(s) to return', { type: :strings, short: :F }],
30
30
  data: ['JSON string input', { type: :string }],
31
31
 
32
+ # Iteractions for supporting other language wrappers
33
+ auth_token: ['Return an auth token for other language wrappers', { short: :T }],
34
+ token: ['Provide an existing auth token', { type: :string }],
35
+
32
36
  # General config
33
37
  config: ['Path to the JSON/YAML config file to use', { type: :string, short: :C }],
34
38
  raw: ['Return raw JSON output, rather than simplified data', { short: :R }],
@@ -40,7 +44,7 @@ module Usps
40
44
 
41
45
  CONFLICTING_OPTION_GROUPS = [
42
46
  %i[certificate id],
43
- %i[on panel query mapper map business_objects],
47
+ %i[on panel query mapper map business_objects auth_token],
44
48
  %i[field fields map query],
45
49
  %i[raw include_ids],
46
50
  %i[quiet log_level],
@@ -63,11 +67,6 @@ module Usps
63
67
  %i[delete raw]
64
68
  ].freeze
65
69
 
66
- DEPENDENT_OPTION_PAIRS = [
67
- %i[ordinal panel],
68
- %i[create data]
69
- ].freeze
70
-
71
70
  attr_reader :arguments, :options
72
71
 
73
72
  def self.banner_header(version)
@@ -118,7 +117,6 @@ module Usps
118
117
 
119
118
  OPTIONS.each { |option, data| opt(option, *data) }
120
119
  CONFLICTING_OPTION_GROUPS.each { |group| conflicts(*group) }
121
- DEPENDENT_OPTION_PAIRS.each { |pair| depends(*pair) }
122
120
 
123
121
  educate_on_error
124
122
  end
@@ -5,6 +5,8 @@ module Usps
5
5
  # Formats log statements
6
6
  #
7
7
  class LoggerFormatter < ::Logger::Formatter
8
+ include ::ActiveSupport::TaggedLogging::Formatter
9
+
8
10
  def call(severity, time, _progname, message)
9
11
  log_chunks = [
10
12
  format('%-5s', severity.to_s),
@@ -14,7 +16,7 @@ module Usps
14
16
  '|',
15
17
  formatted_tags,
16
18
  '|',
17
- message.sub(/^#{Regexp.escape(tags_text)}/, '')
19
+ message.to_s.sub(/^#{Regexp.escape(tags_text)}/, '')
18
20
  ]
19
21
  "#{log_chunks.join(' ')}\n"
20
22
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.11.19'
5
+ VERSION = '0.11.21'
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.19
4
+ version: 0.11.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander