usps-imis-api 0.10.4 → 0.11.1

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: b058c2109e980f6706a58b10847ec6d68c943b44c6de7d5c6dbf44d00b5095e9
4
- data.tar.gz: 4b49c8c39e215e09add71b36d68e86a3a3b6b6f10f999010c9d858787feaa1cc
3
+ metadata.gz: 79ac4896cbc9c4c191719e15d3addb758aa35f16cee662571eff3c751258925e
4
+ data.tar.gz: '09ad846e09959909b4e3c719b823c5ced5fdcd0f9682b2c8e235a29087b3218c'
5
5
  SHA512:
6
- metadata.gz: e646b741b55e2f25395004da9ca74c3bed8a44d7a645e8a0434ec74d38315f0d9e91bbfd17c53111069627ef0ff946eb5f0075df35d375e69cc9ebdc3edfcc7e
7
- data.tar.gz: 42650930b35cdef5a05bc361796d2665ff16ca50a005a268551220d2ba6dd2983d451dfa3dd3ebb00886229224078a6ba44eeabf27aad03e5a32d52e4e8ea417
6
+ metadata.gz: be267ec4b89aafe323d457ddf7dcc05c565b1613765b318cbda0c653fc900e49916ae55b8b3a89c9f5ede33faa21b51cbb66c800d7b68017b06ed261c2661b5d
7
+ data.tar.gz: 20068ce7e8870804cdf02724958c763f5c6843d53d52f73aca9a0856f56771cfc448c271579dde9cc09ff8b2b97149da473f119c262a0e26e4bd057019a743c3
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ module CommandLine
6
+ # Command line interface for the Api
7
+ #
8
+ class Interface
9
+ NAME = 'USPS iMIS API - Ruby'
10
+
11
+ attr_reader :options
12
+
13
+ def self.run(...) = new(...).run
14
+
15
+ def initialize(**)
16
+ @options = input_options.merge(**)
17
+ validate_options!
18
+ configure! if options[:config]
19
+ logging!
20
+ end
21
+
22
+ def run
23
+ set_member
24
+
25
+ result = simplify(perform!)
26
+
27
+ output { result }
28
+
29
+ result
30
+ end
31
+
32
+ private
33
+
34
+ # :nocov:
35
+ def input_options
36
+ if ENV.fetch('CI', false)
37
+ defaults = {
38
+ raw: false,
39
+ quiet: false,
40
+ log: false,
41
+ log_level: 'info',
42
+ version: false,
43
+ help: false
44
+ }
45
+ return defaults
46
+ end
47
+
48
+ OptionsParser.new.options
49
+ end
50
+ # :nocov:
51
+
52
+ def api
53
+ @api ||= Usps::Imis::Api.new
54
+ end
55
+
56
+ def set_member
57
+ case options
58
+ in certificate: then api.imis_id_for(certificate)
59
+ in id: then api.imis_id = id
60
+ else
61
+ # Query
62
+ end
63
+ end
64
+
65
+ # rubocop:disable Metrics
66
+ def perform!
67
+ case options
68
+ in map:, data: then api.mapper[map.to_sym] = data
69
+ in map: then api.mapper[map.to_sym]
70
+
71
+ in on:, delete: true then api.on(on).delete
72
+ in on:, data:, post: true then api.on(on).post(data)
73
+ in on:, data:, field: then api.on(on).put_field(field, data)
74
+ in on:, data: then api.on(on).put_fields(data)
75
+ in on:, fields: then api.on(on).get_fields(*fields)
76
+ in on:, field: then api.on(on).get_field(field)
77
+ in on: then api.on(on).get
78
+
79
+ in panel:, ordinal:, delete: true then api.panels.public_send(panel).delete(ordinal)
80
+ in panel:, data:, post: true then api.panels.public_send(panel).post(data)
81
+ in panel:, ordinal:, data:, field: then api.panels.public_send(panel).put_field(ordinal, field, data)
82
+ in panel:, ordinal:, data: then api.panels.public_send(panel).put_fields(ordinal, data)
83
+ in panel:, ordinal:, fields: then api.panels.public_send(panel).get_fields(ordinal, *fields)
84
+ in panel:, ordinal:, field: then api.panels.public_send(panel).get_field(ordinal, field)
85
+ in panel:, ordinal: then api.panels.public_send(panel).get(ordinal)
86
+
87
+ in query:, data: then api.query(query, data)
88
+ in query: then api.query(query)
89
+ end
90
+ rescue NoMatchingPatternError => e
91
+ raise Errors::CommandLineError, "Unable to match pattern from options: #{e.message}"
92
+ end
93
+ # rubocop:enable Metrics
94
+
95
+ def simplify(data)
96
+ return data.to_a if data.is_a?(Query)
97
+ return data if options[:raw]
98
+ return data unless data.is_a?(Hash) # Includes Usps::Imis::Data
99
+
100
+ data.properties(include_ids: options[:include_ids])
101
+ end
102
+
103
+ # :nocov:
104
+ def output(&block)
105
+ return if ENV.fetch('SUPPRESS_OUTPUT', false)
106
+
107
+ puts JSON.dump(block.call) if block_given?
108
+ end
109
+ # :nocov:
110
+
111
+ def configure!
112
+ json_config = JSON.parse(File.read(options[:config]))
113
+
114
+ Usps::Imis.configure do |config|
115
+ json_config.each do |key, value|
116
+ config.public_send("#{key}=", value)
117
+ end
118
+ end
119
+ end
120
+
121
+ def logging!
122
+ Usps::Imis.configure do |config|
123
+ # :nocov:
124
+ if options[:quiet] || ENV.fetch('SUPPRESS_OUTPUT', false)
125
+ config.logger = ActiveSupport::TaggedLogging.new(Logger.new(nil))
126
+ elsif options[:log]
127
+ # Use default
128
+ else
129
+ config.logger = ActiveSupport::TaggedLogging.new(Logger.new($stderr))
130
+ end
131
+ # :nocov:
132
+
133
+ config.logger.level = options[:log_level]
134
+ end
135
+ end
136
+
137
+ def validate_options!
138
+ if options[:log_level] == 'info' && options.except(:log_level, :quiet).values.none?
139
+ raise Errors::CommandLineError, 'No options provided'
140
+ end
141
+
142
+ return if options[:query].nil? || options[:query].start_with?('$/')
143
+
144
+ raise Errors::CommandLineError, 'Invalid IQA Query name'
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ module CommandLine
6
+ # Command line options parser
7
+ #
8
+ class OptionsParser
9
+ OPTIONS = {
10
+ certificate: ['Member certificate number', { type: :string }],
11
+ id: ['Member iMIS ID', { type: :integer }],
12
+ on: ['Business Object name', { type: :string }],
13
+ panel: ['Panel name', { type: :string }],
14
+ ordinal: ['Ordinal ID within a Panel', { type: :integer }],
15
+ query: ['IQA Query name', { type: :string, short: :Q }],
16
+ post: ["Send a #{'POST'.cyan} request", { short: :P }],
17
+ delete: ["Send a #{'DELETE'.cyan} request", { short: :D }],
18
+ field: ['Specific field to return or update', { type: :string }],
19
+ fields: ['Specific field(s) to return', { type: :strings, short: :F }],
20
+ map: ['Mapped field name to return or update', { type: :string }],
21
+ data: ['JSON string input', { type: :string }],
22
+ config: ['Path to the JSON config file to use', { type: :string, short: :C }],
23
+ raw: ['Return raw JSON output, rather than simplified data', { short: :R }],
24
+ include_ids: ['Include ID properties in returned data'],
25
+ quiet: ['Suppress logging to STDERR'],
26
+ log: ['Redirect logging to STDOUT'],
27
+ log_level: ['Set the logging level', { type: :string, default: 'info', short: :L }]
28
+ }.freeze
29
+
30
+ CONFLICTING_OPTION_GROUPS = [
31
+ %i[certificate id],
32
+ %i[on panel query map],
33
+ %i[field fields map query]
34
+ ].freeze
35
+
36
+ attr_reader :arguments, :options
37
+
38
+ def self.banner_contents
39
+ <<~BANNER
40
+ Usage:
41
+ imis.rb #{'[options]'.gray}
42
+
43
+ The default HTTP verb is #{'GET'.cyan}.
44
+ If #{'--data'.green}/#{'-d'.green} is provided, the default HTTP verb is #{'PUT'.cyan}.
45
+
46
+ #{'--data'.green}/#{'-d'.green} is used to provide field(s) data for #{'PUT'.cyan}
47
+ requests and mapper updates, object data for #{'POST'.cyan},
48
+ and to provide any query params.
49
+
50
+ Configuration can be specified with #{'--config'.green}/#{'-C'.green}, or by setting
51
+ the following environment variables:
52
+ #{'IMIS_ENVIRONMENT'.yellow}
53
+ #{'IMIS_USERNAME'.yellow}
54
+ #{'IMIS_PASSWORD'.yellow}
55
+ #{'IMIS_ID_QUERY_NAME'.yellow}
56
+
57
+ Options:
58
+ BANNER
59
+ end
60
+
61
+ def initialize
62
+ @options = parse_options.compact
63
+ @arguments = ARGV # Not currently used
64
+
65
+ # :nocov:
66
+ @options[:data] = read_stdin if stdin?
67
+ # :nocov:
68
+
69
+ @options[:data] = JSON.parse(@options[:data])
70
+ end
71
+
72
+ private
73
+
74
+ def parse_options
75
+ Optimist.options do
76
+ version "#{Interface::NAME} (v#{Usps::Imis::VERSION})"
77
+
78
+ banner "#{version.bold.blue}\n \n"
79
+ banner OptionsParser.banner_contents
80
+
81
+ OPTIONS.each { |option, data| opt(option, *data) }
82
+ CONFLICTING_OPTION_GROUPS.each { |group| conflicts(*group) }
83
+
84
+ educate_on_error
85
+ end
86
+ end
87
+
88
+ # :nocov:
89
+ def stdin? = $stdin.wait_readable(0)
90
+ def read_stdin = $stdin.read.chomp
91
+ # :nocov:
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optimist'
4
+ require 'colorize'
5
+
6
+ module Usps
7
+ module Imis
8
+ # Wrapper for the command line interface
9
+ #
10
+ module CommandLine; end
11
+ end
12
+ end
13
+
14
+ require_relative 'command_line/options_parser'
15
+ require_relative 'command_line/interface'
@@ -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 = default_environment
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)
@@ -53,6 +53,14 @@ module Usps
53
53
  # Parameters to filter out of logging
54
54
  #
55
55
  def filtered_parameters = %i[password]
56
+
57
+ private
58
+
59
+ def default_environment
60
+ return ::Rails.env if defined?(::Rails)
61
+
62
+ ActiveSupport::StringInquirer.new(ENV.fetch('IMIS_ENVIRONMENT', 'development'))
63
+ end
56
64
  end
57
65
  end
58
66
  end
@@ -36,7 +36,7 @@ module Usps
36
36
  return if property.nil?
37
37
 
38
38
  value = property['Value']
39
- value.is_a?(String) ? value : value['$value']
39
+ value.nil? || value.is_a?(String) ? value : value['$value']
40
40
  end
41
41
 
42
42
  # Hash of all property names to values
@@ -52,3 +52,4 @@ require_relative 'errors/not_found_error'
52
52
  require_relative 'errors/response_error'
53
53
  require_relative 'errors/panel_unimplemented_error'
54
54
  require_relative 'errors/unexpected_property_type_error'
55
+ require_relative 'errors/command_line_error'
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ module Errors
6
+ # Exception raised by the command line interface
7
+ #
8
+ class CommandLineError < Error; end
9
+ end
10
+ end
11
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.10.4'
5
+ VERSION = '0.11.1'
6
6
  end
7
7
  end
data/lib/usps/imis.rb CHANGED
@@ -1,3 +1,4 @@
1
+ #!/usr/bin/env ruby
1
2
  # frozen_string_literal: true
2
3
 
3
4
  # Core dependencies
@@ -20,6 +21,7 @@ require_relative 'imis/error'
20
21
  require_relative 'imis/api'
21
22
  require_relative 'imis/properties'
22
23
  require_relative 'imis/panels'
24
+ require_relative 'imis/command_line'
23
25
  require_relative 'imis/mocks'
24
26
  require_relative 'imis/version'
25
27
 
@@ -56,3 +58,9 @@ module Usps
56
58
  end
57
59
  end
58
60
  end
61
+
62
+ # Invoke CLI, only if invoked from the command line
63
+ #
64
+ # :nocov:
65
+ Usps::Imis::CommandLine::Interface.run if __FILE__ == $PROGRAM_NAME
66
+ # :nocov:
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.10.4
4
+ version: 0.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -23,6 +23,34 @@ dependencies:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
25
  version: '8.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: colorize
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.1.0
40
+ - !ruby/object:Gem::Dependency
41
+ name: optimist
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 3.2.1
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.1
26
54
  description: A wrapper for the iMIS API.
27
55
  email: jsfiander@gmail.com
28
56
  executables: []
@@ -33,10 +61,14 @@ files:
33
61
  - lib/usps/imis.rb
34
62
  - lib/usps/imis/api.rb
35
63
  - lib/usps/imis/business_object.rb
64
+ - lib/usps/imis/command_line.rb
65
+ - lib/usps/imis/command_line/interface.rb
66
+ - lib/usps/imis/command_line/options_parser.rb
36
67
  - lib/usps/imis/config.rb
37
68
  - lib/usps/imis/data.rb
38
69
  - lib/usps/imis/error.rb
39
70
  - lib/usps/imis/errors/api_error.rb
71
+ - lib/usps/imis/errors/command_line_error.rb
40
72
  - lib/usps/imis/errors/config_error.rb
41
73
  - lib/usps/imis/errors/locked_id_error.rb
42
74
  - lib/usps/imis/errors/mapper_error.rb