usps-imis-api 0.11.42 → 0.12.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: db2bc7a4cecec659c0b9d6c86ec8a5819f066537f35b0db089ca5f7df243c597
4
- data.tar.gz: 7945390fbd0d073799a01e25cbfd9ffcd6a590da818055a39a7c6368a881e787
3
+ metadata.gz: 6d4935929ad9c147f74130bc48c0980ad3ffa801bb7fb19680f07fca5492d58d
4
+ data.tar.gz: dc9dee4a54c2b39fa7b30ec518fb797855f40022d8990b9d6bb348a22bf23006
5
5
  SHA512:
6
- metadata.gz: 20eca697722eb6ef3d502afa3b87f46655feee6739eabec86580a032d28f590f29b619abdca8d25f56edcc77a7584305fe9911c73c596b5b70dd847d00fb1b96
7
- data.tar.gz: 92676045e015b98881b8d40e5d99b9720ddae3618d3686837e6ac9ac0a5414308ba146f4a9339e64038f85c696abbc2d07f148c3a62bed14d93c9f9e7096f0b7
6
+ metadata.gz: d867fd2358a479359eeb68b00e97c4da63e74909c51ba4eb335af3e4353ec3eb297f79a0d538e565adfc8bf080127d261f8a59f430127266ed9efbb0cebf0039
7
+ data.tar.gz: 8fc15085b191a48c63d7de467e6479846cde28158e4dbd02d847082243450135e68120b6e9f88d6c497d4c841a3aedd763c7f9c182023588fe89f4642b2c9c55
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Usps
4
+ module Imis
5
+ module CommandLine
6
+ # Command line interface output formatters
7
+ #
8
+ # @private
9
+ #
10
+ module Formatters
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
+
16
+ private
17
+
18
+ def simplify(data)
19
+ return data.to_a if data.is_a?(Query)
20
+ return data.is_a?(BaseData) ? data.raw : data if force_raw_output?
21
+
22
+ if data.is_a?(PartyData)
23
+ logger.debug 'Returning simplified PartyData#properties'
24
+ data.properties
25
+ elsif data.is_a?(Data)
26
+ logger.debug 'Returning simplified Data#properties'
27
+ data.properties(include_ids: options[:include_ids])
28
+ elsif data.is_a?(Array) && data.all?(Data)
29
+ logger.debug 'Returning simplified Array<Data#properties>'
30
+ data.map { it.properties(include_ids: options[:include_ids]) }
31
+ else
32
+ data
33
+ end
34
+ end
35
+
36
+ def force_raw_output? = options[:raw] || RAW_HASH_RESPONSE_OPTIONS.any? { options[it] }
37
+
38
+ # :nocov:
39
+ def output(&)
40
+ if ENV.fetch('SUPPRESS_OUTPUT', false)
41
+ logger.debug 'Output suppressed'
42
+ return
43
+ end
44
+
45
+ return unless block_given?
46
+
47
+ value = yield
48
+
49
+ if value.is_a?(Array) && @options[:jsonl] then jsonl(value)
50
+ elsif value.is_a?(Array) && @options[:csv] then csv(value)
51
+ else raw(value)
52
+ end
53
+ end
54
+
55
+ def raw(value) = puts(JSON.dump(value))
56
+
57
+ def jsonl(value) = puts(value.map { JSON.dump(it) })
58
+
59
+ def csv(value)
60
+ puts(CSV.generate(headers: value.first.keys, write_headers: true) do |csv|
61
+ value.each { csv << it.values }
62
+ end)
63
+ end
64
+ # :nocov:
65
+ end
66
+ end
67
+ end
68
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'performers'
4
+ require_relative 'formatters'
4
5
 
5
6
  module Usps
6
7
  module Imis
@@ -9,6 +10,7 @@ module Usps
9
10
  #
10
11
  class Interface
11
12
  include Performers
13
+ include Formatters
12
14
 
13
15
  # Prioritized list of config file paths to automatically check if not provided
14
16
  #
@@ -21,11 +23,6 @@ module Usps
21
23
  system: '/usr/local/imis/config.yml'
22
24
  }.freeze
23
25
 
24
- # CLI options that indicate the response is a raw Hash rather than a Data object,
25
- # and should not be simplified
26
- #
27
- RAW_HASH_RESPONSE_OPTIONS = %i[business_objects auth_token].freeze
28
-
29
26
  # Options passed in from the command line
30
27
  #
31
28
  attr_reader :options
@@ -109,45 +106,6 @@ module Usps
109
106
  logger.debug "Record ID: #{api.record_id}" if api.record_id
110
107
  end
111
108
 
112
- def simplify(data)
113
- return data.to_a if data.is_a?(Query)
114
- return data.is_a?(BaseData) ? data.raw : data if force_raw_output?
115
-
116
- if data.is_a?(PartyData)
117
- logger.debug 'Returning simplified PartyData#properties'
118
- data.properties
119
- elsif data.is_a?(Data)
120
- logger.debug 'Returning simplified Data#properties'
121
- data.properties(include_ids: options[:include_ids])
122
- elsif data.is_a?(Array) && data.all?(Data)
123
- logger.debug 'Returning simplified Array<Data#properties>'
124
- data.map { it.properties(include_ids: options[:include_ids]) }
125
- else
126
- data
127
- end
128
- end
129
-
130
- def force_raw_output? = options[:raw] || RAW_HASH_RESPONSE_OPTIONS.any? { options[it] }
131
-
132
- # :nocov:
133
- def output(&)
134
- if ENV.fetch('SUPPRESS_OUTPUT', false)
135
- logger.debug 'Output suppressed'
136
- return
137
- end
138
-
139
- return unless block_given?
140
-
141
- value = yield
142
-
143
- if value.is_a?(Array) && @options[:jsonl]
144
- puts(value.map { JSON.dump(it) })
145
- else
146
- puts JSON.dump(value)
147
- end
148
- end
149
- # :nocov:
150
-
151
109
  # Supports YAML or JSON config data
152
110
  #
153
111
  def configure!
@@ -0,0 +1,134 @@
1
+ # IDs
2
+ certificate:
3
+ - Member certificate number
4
+ - type: string
5
+ short: c
6
+ id:
7
+ - Member iMIS ID
8
+ - type: integer
9
+ short: i
10
+ record_id:
11
+ - Specific Record ID
12
+ - type: integer
13
+ short: I
14
+ uuid:
15
+ - Record UUID
16
+ - type: string
17
+ short: u
18
+
19
+ # Primary interactions
20
+ 'on':
21
+ - Business Object name
22
+ - type: string
23
+ short: O
24
+ panel:
25
+ - Panel name
26
+ - type: string
27
+ short: P
28
+ query:
29
+ - IQA Query or Business Object name to query
30
+ - type: string
31
+ short: Q
32
+ mapper:
33
+ - Interact with mapped fields
34
+ - short: M
35
+ map:
36
+ - "Shorthand for <%= '-Mf'.green %> to access a single mapped field"
37
+ - type: string
38
+ short: m
39
+ business_objects:
40
+ - List available Business Objects
41
+ - short: B
42
+
43
+ # Alternate verbs
44
+ create:
45
+ - "Send a <%= 'POST'.cyan %> request"
46
+ - short: C
47
+ delete:
48
+ - "Send a <%= 'DELETE'.cyan %> request"
49
+ - short: D
50
+
51
+ # Data
52
+ ordinal:
53
+ - Ordinal ID within a Panel
54
+ - type: integer
55
+ short: r
56
+ field:
57
+ - Specific field to return or update
58
+ - type: string
59
+ short: f
60
+ fields:
61
+ - Specific field(s) to return
62
+ - type: strings
63
+ short: F
64
+ data:
65
+ - "JSON string input -- <%= 'STDIN'.red %> takes priority"
66
+ - type: string
67
+ short: d
68
+
69
+ # Query params
70
+ page:
71
+ - Specific page number from a query to return
72
+ - type: integer
73
+ default: 1
74
+ short: p
75
+ page_size:
76
+ - Maximum number of records to include on a single page (max 500)
77
+ - type: integer
78
+ default: 100
79
+ short: -s
80
+ offset:
81
+ - Number of records to skip from the beginning of a query
82
+ - type: integer
83
+ short: -o
84
+
85
+ # Output formats
86
+ raw:
87
+ - Return raw JSON output, rather than simplified data
88
+ - short: R
89
+ jsonl:
90
+ - Format array output as JSONL
91
+ - short: j
92
+ csv:
93
+ - Format output as CSV (if possible)
94
+ - short: S
95
+
96
+ # General config
97
+ config:
98
+ - "Path to the JSON/YAML config file to use, or one of the following preset options: \
99
+ `<%= 'local'.yellow %>`, \
100
+ `<%= 'local_dot_config'.yellow %>`, \
101
+ `<%= 'local_config'.yellow %>`, \
102
+ `<%= 'user'.yellow %>`, \
103
+ `<%= 'system'.yellow %>`. \
104
+ If no option is provided, the first matching preset option will be automatically used."
105
+ - type: string
106
+ short: Y
107
+ show_config:
108
+ - Return the active config file path
109
+ - short: X
110
+ include_ids:
111
+ - "Include any <%= 'iMIS ID'.yellow %> and <%= 'Ordinal'.yellow %> properties in returned data"
112
+ - short: N
113
+
114
+ # Iteractions for supporting other language wrappers
115
+ auth_token:
116
+ - Return an auth token for other language wrappers
117
+ - short: T
118
+ token:
119
+ - Provide an existing auth token
120
+ - type: string
121
+ short: t
122
+
123
+ # Logging
124
+ quiet:
125
+ - "Suppress logging to <%= 'STDERR'.red %>"
126
+ - short: q
127
+ log:
128
+ - "Redirect logging to <%= 'STDOUT'.red %>"
129
+ - short: l
130
+ log_level:
131
+ - Set the logging level
132
+ - type: string
133
+ default: info
134
+ short: L
@@ -7,22 +7,25 @@ module Usps
7
7
  #
8
8
  class OptionsParser
9
9
  SOLO_FLAGS = %i[show_config auth_token business_objects].freeze
10
+ QUERY_FLAGS = %i[query page page_size offset].freeze
10
11
  CONFLICTING_OPTION_GROUPS = [
11
12
  %i[certificate id uuid],
12
13
  %i[record_id uuid],
13
14
  %i[on panel query mapper map] + SOLO_FLAGS,
14
15
  %i[field fields map query],
15
16
  %i[raw include_ids],
17
+ %i[raw jsonl csv],
16
18
  %i[quiet log_level],
17
19
  %i[quiet log],
20
+ %i[page offset],
18
21
 
19
22
  %i[create delete],
20
23
  %i[create ordinal],
21
24
  %i[data fields],
22
25
 
23
- *(SOLO_FLAGS + %i[mapper query map field fields certificate]).map { [:create, it] },
24
- *(SOLO_FLAGS + %i[mapper query map field fields certificate data raw]).map { [:delete, it] },
25
- *(SOLO_FLAGS + %i[mapper query map on]).map { [:ordinal, it] },
26
+ *(SOLO_FLAGS + QUERY_FLAGS + %i[mapper map field fields certificate]).map { [:create, it] },
27
+ *(SOLO_FLAGS + QUERY_FLAGS + %i[mapper map field fields certificate data raw]).map { [:delete, it] },
28
+ *(SOLO_FLAGS + QUERY_FLAGS + %i[mapper map on]).map { [:ordinal, it] },
26
29
  *(SOLO_FLAGS + %i[certificate]).map { [:data, it] }
27
30
  ].freeze
28
31
 
@@ -88,17 +91,7 @@ module Usps
88
91
  #
89
92
  Optimist.educate if ARGV.empty? && defaults?
90
93
 
91
- # :nocov:
92
- @options[:data] = read_stdin if stdin?
93
- # :nocov:
94
-
95
- @options[:data] = JSON.parse(@options[:data]) if @options[:data]
96
-
97
- # Support shorthand for setting `certificate` param on queries
98
- return unless @options[:query] && @options[:certificate]
99
-
100
- @options[:data] ||= {}
101
- @options[:data]['certificate'] = @options.delete(:certificate)
94
+ pre_process_options!
102
95
  end
103
96
 
104
97
  private
@@ -128,6 +121,24 @@ module Usps
128
121
  options_with_defaults.all? { |option, data| data[1].nil? || options[option] == data[1][:default] } &&
129
122
  options.except(*options_with_defaults.keys).values.none?
130
123
  end
124
+
125
+ def pre_process_options!
126
+ # :nocov:
127
+ @options[:data] = read_stdin if stdin?
128
+ # :nocov:
129
+
130
+ @options[:data] = JSON.parse(@options[:data]) if @options[:data]
131
+
132
+ raise Errors::CommandLineError, 'Maximum page size is 500' if @options[:page_size] > 500
133
+
134
+ @options[:offset] = (@options[:page] - 1) * @options[:page_size] if @options[:page]
135
+
136
+ # Support shorthand for setting `certificate` param on queries
137
+ return unless @options[:query] && @options[:certificate]
138
+
139
+ @options[:data] ||= {}
140
+ @options[:data]['certificate'] = @options.delete(:certificate)
141
+ end
131
142
  end
132
143
  end
133
144
  end
@@ -15,11 +15,11 @@ module Usps
15
15
  in mapper:, **extra then perform_mapper(mapper, **extra)
16
16
  in on:, **extra then perform_on(on, **extra)
17
17
  in panel:, **extra then perform_panel(panel, **extra)
18
- in query:, **extra then api.query(query, extra[:data])
18
+ in query:, **extra then perform_query(query, **extra)
19
19
  in business_objects: true then api.business_objects
20
20
  in auth_token: true then api.auth_token
21
- in certificate: then api.imis_id
22
- in show_config: then config_path
21
+ in certificate: _ then api.imis_id
22
+ in show_config: true then config_path
23
23
  in version: true then api.class.version
24
24
  end
25
25
  rescue NoMatchingPatternError => e
@@ -33,8 +33,7 @@ module Usps
33
33
  in mapper: true then converted[:mapper] = api.mapper
34
34
  in on: then converted[:on] = api.on(on)
35
35
  in panel: then converted[:panel] = api.panels.public_send(panel)
36
- else
37
- # Nothing to convert
36
+ else # Nothing to convert
38
37
  end
39
38
 
40
39
  # Remove flags when false
@@ -59,8 +58,7 @@ module Usps
59
58
  in data: then on.put_fields(data)
60
59
  in fields: then on.get_fields(*fields)
61
60
  in field: then on.get_field(field)
62
- else
63
- on.get
61
+ else on.get
64
62
  end
65
63
  end
66
64
 
@@ -75,6 +73,19 @@ module Usps
75
73
  in ordinal: then panel.get(ordinal)
76
74
  end
77
75
  end
76
+
77
+ def perform_query(query, **options)
78
+ case options
79
+ in page: _, page_size:, offset:, data: then api.query(query, page_size:, offset:, **data).page
80
+ in page: _, page_size:, offset: then api.query(query, page_size:, offset:).page
81
+ in page: _, page_size:, data: then api.query(query, page_size:, **data).page
82
+ in page: _, page_size: then api.query(query, page_size:).page
83
+ in page_size:, offset:, data: then api.query(query, page_size:, offset:, **data)
84
+ in page_size:, data: then api.query(query, page_size:, **data)
85
+ in data: then api.query(query, **data)
86
+ else api.query(query)
87
+ end
88
+ end
78
89
  end
79
90
  end
80
91
  end
@@ -3,6 +3,7 @@
3
3
  require 'optimist'
4
4
  require 'colorize'
5
5
  require 'erb'
6
+ require 'csv'
6
7
 
7
8
  module Usps
8
9
  module Imis
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Usps
4
4
  module Imis
5
- VERSION = '0.11.42'
5
+ VERSION = '0.12.1'
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.42
4
+ version: 0.12.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -51,6 +51,20 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: 1.1.0
54
+ - !ruby/object:Gem::Dependency
55
+ name: csv
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: 3.3.5
61
+ type: :runtime
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: 3.3.5
54
68
  - !ruby/object:Gem::Dependency
55
69
  name: optimist
56
70
  requirement: !ruby/object:Gem::Requirement
@@ -80,7 +94,9 @@ files:
80
94
  - lib/usps/imis/blank_object.rb
81
95
  - lib/usps/imis/business_object.rb
82
96
  - lib/usps/imis/command_line.rb
97
+ - lib/usps/imis/command_line/formatters.rb
83
98
  - lib/usps/imis/command_line/interface.rb
99
+ - lib/usps/imis/command_line/options.yml.erb
84
100
  - lib/usps/imis/command_line/options_parser.rb
85
101
  - lib/usps/imis/command_line/performers.rb
86
102
  - lib/usps/imis/config.rb