usps-imis-api 0.10.4 → 0.11.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 +4 -4
- data/lib/usps/imis/command_line/interface.rb +149 -0
- data/lib/usps/imis/command_line/options_parser.rb +93 -0
- data/lib/usps/imis/command_line.rb +15 -0
- data/lib/usps/imis/config.rb +9 -1
- data/lib/usps/imis/data.rb +1 -1
- data/lib/usps/imis/error.rb +1 -0
- data/lib/usps/imis/errors/command_line_error.rb +11 -0
- data/lib/usps/imis/version.rb +1 -1
- data/lib/usps/imis.rb +8 -0
- metadata +33 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6a938a3621c6e575ac4d205495bbdccf39b4579a986a4431730d366bee510824
|
|
4
|
+
data.tar.gz: 7cfbacbf135e1f9899510fc54ff05aa079bf70f24b1c93b81de0bcd78fc26218
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 679178e131a5fa45915738be179b82173fa8383c0109ba6fe5e2d73b9f85c1ecdb6bc160e94ed385866371ed10e465de17a05409b55de64e015f8d44d4b12c0a
|
|
7
|
+
data.tar.gz: 66466bcfca8c602e00a04a3f52599cf96f0bae5d64065542e615bbb8acb9cab27fd5b44552e9367ecdb8ef3004ac9a40e3417a5ec8bc6a46b8ffb04cff7aed96
|
|
@@ -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,93 @@
|
|
|
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] = JSON.parse(read_stdin) if stdin?
|
|
67
|
+
# :nocov:
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
private
|
|
71
|
+
|
|
72
|
+
def parse_options
|
|
73
|
+
Optimist.options do
|
|
74
|
+
version "#{Interface::NAME} (v#{Usps::Imis::VERSION})"
|
|
75
|
+
|
|
76
|
+
banner "#{version.bold.blue}\n \n"
|
|
77
|
+
banner OptionsParser.banner_contents
|
|
78
|
+
|
|
79
|
+
OPTIONS.each { |option, data| opt(option, *data) }
|
|
80
|
+
CONFLICTING_OPTION_GROUPS.each { |group| conflicts(*group) }
|
|
81
|
+
|
|
82
|
+
educate_on_error
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# :nocov:
|
|
87
|
+
def stdin? = $stdin.wait_readable(0)
|
|
88
|
+
def read_stdin = $stdin.read.chomp
|
|
89
|
+
# :nocov:
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
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'
|
data/lib/usps/imis/config.rb
CHANGED
|
@@ -12,7 +12,7 @@ module Usps
|
|
|
12
12
|
attr_reader :environment, :logger, :logger_level
|
|
13
13
|
|
|
14
14
|
def initialize
|
|
15
|
-
@environment =
|
|
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
|
data/lib/usps/imis/data.rb
CHANGED
data/lib/usps/imis/error.rb
CHANGED
data/lib/usps/imis/version.rb
CHANGED
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.
|
|
4
|
+
version: 0.11.0
|
|
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
|