@@ -28,41 +28,39 @@ def initialize(arguments)
2828 @search_parser = init_search_parser
2929 end
3030
31- # Summarize the result of the command.
31+ # Summarize and print the result of the command.
3232 # @param command [String]
33- # @return [String]
34- def summarize_result ( command )
33+ def print_result ( command )
3534 if command == 'search-models'
3635 @search_parser . parse! ( @arguments )
37- @ result = search ( @options )
38- summarized_result = @options [ :print_full ] ? @ result. to_s : @ result. models . to_s
36+ result = search ( @options )
37+ summarized_result = output_format == :full ? result . to_s : result . models . to_s
3938 else
4039 @product_parser [ command ] . parse! ( @arguments )
4140 @options [ :file_path ] = @arguments . shift
4241 if @options [ :file_path ] . nil?
4342 warn 'file missing'
4443 abort ( @product_parser [ command ] . help )
4544 end
46- @result = send ( command , @options )
47- summarized_result = @options [ :print_full ] ? @result . inference . to_s : @result . inference . result . to_s
45+ result = send ( command , @options )
46+ summarized_result = output_format == :full ? result . inference . to_s : result . inference . result . to_s
47+ end
48+
49+ if output_format == :raw
50+ puts JSON . pretty_generate ( raw_payload ( result . raw_http ) )
51+ else
52+ puts summarized_result
4853 end
49- summarized_result
5054 end
5155
5256 # Executes the command.
5357 # @return [void]
5458 def execute
55- @options = { }
59+ @options = { output_format : :summary }
5660 command = @arguments . shift
5761
5862 validate_command! ( command )
59- summarized_result = summarize_result ( command )
60-
61- if @options [ :raw_json ]
62- puts JSON . pretty_generate ( @result . raw_http )
63- else
64- puts summarized_result
65- end
63+ print_result ( command )
6664 rescue OptionParser ::InvalidOption , OptionParser ::MissingArgument => e
6765 if command == 'search-models'
6866 abort ( "#{ e . message } \n \n #{ @search_parser . help } " )
@@ -101,24 +99,24 @@ def init_search_parser
10199 end
102100
103101 def setup_specific_options ( options_parser , doc_value )
104- options_parser . on ( '-r' , '--rag' , 'Enable RAG' ) { @options [ :rag ] = true } if doc_value [ :rag ]
105- if doc_value [ :raw_text ]
102+ options_parser . on ( '-r' , '--rag' , 'Enable RAG' ) { @options [ :rag ] = true } if doc_value . key? ( :rag )
103+ if doc_value . key? ( :raw_text )
106104 options_parser . on ( '-R' , '--raw-text' , 'Enable Raw Text retrieval' ) do
107105 @options [ :raw_text ] = true
108106 end
109107 end
110- if doc_value [ :confidence ]
108+ if doc_value . key? ( :confidence )
111109 options_parser . on ( '-c' , '--confidence' , 'Enable confidence scores' ) do
112110 @options [ :confidence ] = true
113111 end
114112 end
115- options_parser . on ( '-p' , '--polygon' , 'Enable polygons' ) { @options [ :polygon ] = true } if doc_value [ :polygon ]
116- if doc_value [ :text_context ]
113+ options_parser . on ( '-p' , '--polygon' , 'Enable polygons' ) { @options [ :polygon ] = true } if doc_value . key? ( :polygon )
114+ if doc_value . key? ( :text_context )
117115 options_parser . on ( '-t [TEXT CONTEXT]' , '--text-context [TEXT CONTEXT]' , 'Add Text Context' ) do |v |
118116 @options [ :text_context ] = v
119117 end
120118 end
121- return unless doc_value [ :data_schema ]
119+ return unless doc_value . key? ( :data_schema )
122120
123121 options_parser . on ( '-d [DATA SCHEMA]' , '--data-schema [DATA SCHEMA]' , 'Add Data Schema' ) do |v |
124122 @options [ :data_schema ] = v
@@ -129,8 +127,31 @@ def setup_specific_options(options_parser, doc_value)
129127 # @param options_parser [OptionParser]
130128 def init_common_options ( options_parser )
131129 options_parser . on ( '-k [KEY]' , '--key [KEY]' , 'API key for the endpoint' ) { |v | @options [ :api_key ] = v }
132- options_parser . on ( '-f' , '--full' , 'Print the full data' ) { @options [ :print_full ] = true }
133- options_parser . on ( '-j' , '--raw-json' , 'Print the full raw jason data' ) { @options [ :raw_json ] = true }
130+ options_parser . on ( '-o FORMAT' , '--output-format FORMAT' , [ 'raw' , 'full' , 'summary' ] ,
131+ 'Format of the output (raw, full, summary). Default: summary' ) do |format |
132+ @options [ :output_format ] = format
133+ end
134+ end
135+
136+ # @return [Symbol]
137+ def output_format
138+ @options [ :output_format ] &.to_sym || :summary
139+ end
140+
141+ # Handles JSON payloads represented either as a string or an already-parsed hash.
142+ # Also tolerates one extra JSON encoding layer.
143+ # @param payload [String, Hash]
144+ # @return [Hash, Array, String]
145+ def raw_payload ( payload )
146+ parsed_payload = payload
147+ 2 . times do
148+ break unless parsed_payload . is_a? ( String )
149+
150+ parsed_payload = JSON . parse ( parsed_payload )
151+ rescue JSON ::ParserError
152+ break
153+ end
154+ parsed_payload
134155 end
135156
136157 # @return [Hash]
@@ -144,19 +165,21 @@ def init_product_parser
144165 @options [ :alias ] = v
145166 end
146167 init_common_options ( options_parser )
147- options_parser . on ( '-C PAGES' , '--cut-pages PAGES' , 'Cut document pages' ) { |v | @options [ :cut_pages ] = v }
148- options_parser . on ( '-F' , '--fix-pdf' , 'Repair PDF' ) { @options [ :repair_pdf ] = true }
168+ options_parser . on ( '-F' , '--fix-pdf' , 'Attempt to repair PDF before enqueueing' ) do
169+ @options [ :repair_pdf ] = true
170+ end
149171 setup_specific_options ( options_parser , product_values )
150172 end
151173 end
152174 v2_product_parser
153175 end
154176
155- # @param options [Hash] General options.
156- # @return page_options [Hash] Page options.
157- def setup_product_params ( options , page_options )
158- params = { model_id : options [ :model_id ] }
159- params [ :options ] = Mindee ::ParseOptions . new ( params : page_options ) unless page_options . nil?
177+ # @return [Hash]
178+ def setup_product_params
179+ params = { model_id : @options [ :model_id ] }
180+ @options . each_pair do |key , value |
181+ params [ key ] = value if V2_PRODUCTS [ 'extraction' ] . include? ( key )
182+ end
160183 params
161184 end
162185
@@ -167,8 +190,7 @@ def send(product_command, options)
167190 mindee_client = Mindee ::ClientV2 . new ( api_key : options [ :api_key ] )
168191 response_class = V2_PRODUCTS [ product_command ] [ :response_class ]
169192 input_source = setup_input_source ( options )
170- page_options = setup_page_options ( options )
171- params = setup_product_params ( options , page_options )
193+ params = setup_product_params
172194
173195 mindee_client . enqueue_and_get_result (
174196 response_class ,
0 commit comments