mindee 3.17.0 → 3.19.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/CHANGELOG.md +14 -0
- data/README.md +4 -4
- data/bin/mindee.rb +0 -6
- data/docs/code_samples/{carte_vitale_v1.txt → us_mail_v3_async.txt} +2 -2
- data/docs/custom_v1.md +1 -1
- data/docs/getting_started.md +5 -5
- data/docs/{us_mail_v2.md → us_mail_v3.md} +34 -12
- data/lib/mindee/client.rb +2 -2
- data/lib/mindee/extraction/tax_extractor/tax_extractor.rb +34 -19
- data/lib/mindee/input/sources/base64_input_source.rb +31 -0
- data/lib/mindee/input/sources/bytes_input_source.rb +21 -0
- data/lib/mindee/input/sources/file_input_source.rb +20 -0
- data/lib/mindee/input/sources/local_input_source.rb +183 -0
- data/lib/mindee/input/sources/path_input_source.rb +20 -0
- data/lib/mindee/input/sources/url_input_source.rb +127 -0
- data/lib/mindee/input/sources.rb +6 -248
- data/lib/mindee/parsing/standard/boolean_field.rb +6 -0
- data/lib/mindee/product/ind/indian_passport/indian_passport_v1_document.rb +1 -1
- data/lib/mindee/product/ind/indian_passport/indian_passport_v1_page.rb +1 -1
- data/lib/mindee/product/{fr/carte_vitale/carte_vitale_v1.rb → us/us_mail/us_mail_v3.rb} +11 -11
- data/lib/mindee/product/us/us_mail/us_mail_v3_document.rb +107 -0
- data/lib/mindee/product/{fr/carte_vitale/carte_vitale_v1_page.rb → us/us_mail/us_mail_v3_page.rb} +8 -8
- data/lib/mindee/product/us/us_mail/us_mail_v3_recipient_address.rb +113 -0
- data/lib/mindee/product/us/us_mail/us_mail_v3_sender_address.rb +66 -0
- data/lib/mindee/product.rb +1 -1
- data/lib/mindee/version.rb +1 -1
- metadata +18 -10
- data/lib/mindee/product/fr/carte_vitale/carte_vitale_v1_document.rb +0 -52
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
require 'uri'
|
5
|
+
require 'fileutils'
|
6
|
+
|
7
|
+
module Mindee
|
8
|
+
module Input
|
9
|
+
module Source
|
10
|
+
# Load a remote document from a file url.
|
11
|
+
class UrlInputSource
|
12
|
+
# @return [String]
|
13
|
+
attr_reader :url
|
14
|
+
|
15
|
+
def initialize(url)
|
16
|
+
raise 'URL must be HTTPS' unless url.start_with? 'https://'
|
17
|
+
|
18
|
+
@url = url
|
19
|
+
end
|
20
|
+
|
21
|
+
# Downloads the file from the URL and saves it to the specified path.
|
22
|
+
#
|
23
|
+
# @param path [String] Path to save the file to.
|
24
|
+
# @param filename [String, nil] Optional name to give to the file.
|
25
|
+
# @param username [String, nil] Optional username for authentication.
|
26
|
+
# @param password [String, nil] Optional password for authentication.
|
27
|
+
# @param token [String, nil] Optional token for JWT-based authentication.
|
28
|
+
# @param max_redirects [Integer] Maximum amount of redirects to follow.
|
29
|
+
# @return [String] The full path of the saved file.
|
30
|
+
def save_to_file(path, filename: nil, username: nil, password: nil, token: nil, max_redirects: 3)
|
31
|
+
response_body = fetch_file_content(username: username, password: password, token: token,
|
32
|
+
max_redirects: max_redirects)
|
33
|
+
|
34
|
+
filename = fill_filename(filename)
|
35
|
+
|
36
|
+
full_path = File.join(path.chomp('/'), filename)
|
37
|
+
File.write(full_path, response_body)
|
38
|
+
|
39
|
+
full_path
|
40
|
+
end
|
41
|
+
|
42
|
+
# Downloads the file from the url, and returns a BytesInputSource wrapper object for it.
|
43
|
+
#
|
44
|
+
# @param filename [String, nil] Optional name to give to the file.
|
45
|
+
# @param username [String, nil] Optional username for authentication.
|
46
|
+
# @param password [String, nil] Optional password for authentication.
|
47
|
+
# @param token [String, nil] Optional token for JWT-based authentication.
|
48
|
+
# @param max_redirects [Integer] Maximum amount of redirects to follow.
|
49
|
+
# @return [BytesInputSource] The full path of the saved file.
|
50
|
+
def as_local_input_source(filename: nil, username: nil, password: nil, token: nil, max_redirects: 3)
|
51
|
+
filename = fill_filename(filename)
|
52
|
+
response_body = fetch_file_content(username: username, password: password, token: token,
|
53
|
+
max_redirects: max_redirects)
|
54
|
+
bytes = StringIO.new(response_body)
|
55
|
+
|
56
|
+
BytesInputSource.new(bytes.read, filename)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Fetches the file content from the URL.
|
60
|
+
#
|
61
|
+
# @param username [String, nil] Optional username for authentication.
|
62
|
+
# @param password [String, nil] Optional password for authentication.
|
63
|
+
# @param token [String, nil] Optional token for JWT-based authentication.
|
64
|
+
# @param max_redirects [Integer] Maximum amount of redirects to follow.
|
65
|
+
# @return [String] The downloaded file content.
|
66
|
+
def fetch_file_content(username: nil, password: nil, token: nil, max_redirects: 3)
|
67
|
+
uri = URI.parse(@url)
|
68
|
+
request = Net::HTTP::Get.new(uri)
|
69
|
+
|
70
|
+
request['Authorization'] = "Bearer #{token}" if token
|
71
|
+
request.basic_auth(username, password) if username && password
|
72
|
+
|
73
|
+
response = make_request(uri, request, max_redirects)
|
74
|
+
if response.code.to_i > 299
|
75
|
+
raise "Failed to download file: HTTP status code #{response.code}"
|
76
|
+
elsif response.code.to_i < 200
|
77
|
+
raise "Failed to download file: Invalid response code #{response.code}."
|
78
|
+
end
|
79
|
+
|
80
|
+
response.body
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def extract_filename_from_url(uri)
|
86
|
+
filename = File.basename(uri.path)
|
87
|
+
filename.empty? ? '' : filename
|
88
|
+
end
|
89
|
+
|
90
|
+
def fill_filename(filename)
|
91
|
+
filename ||= extract_filename_from_url(URI.parse(@url))
|
92
|
+
if filename.empty? || File.extname(filename).empty?
|
93
|
+
filename = generate_file_name(extension: get_file_extension(filename))
|
94
|
+
end
|
95
|
+
filename
|
96
|
+
end
|
97
|
+
|
98
|
+
def make_request(uri, request, max_redirects)
|
99
|
+
Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
|
100
|
+
response = http.request(request)
|
101
|
+
if response.is_a?(Net::HTTPRedirection) && max_redirects.positive?
|
102
|
+
location = response['location']
|
103
|
+
raise 'No location in redirection header.' if location.nil?
|
104
|
+
|
105
|
+
new_uri = URI.parse(location)
|
106
|
+
request = Net::HTTP::Get.new(new_uri)
|
107
|
+
make_request(new_uri, request, max_redirects - 1)
|
108
|
+
else
|
109
|
+
response
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def get_file_extension(filename)
|
115
|
+
ext = File.extname(filename)
|
116
|
+
ext.empty? ? nil : ext.downcase
|
117
|
+
end
|
118
|
+
|
119
|
+
def generate_file_name(extension: nil)
|
120
|
+
extension ||= '.tmp'
|
121
|
+
random_string = Array.new(8) { rand(36).to_s(36) }.join
|
122
|
+
"mindee_temp_#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}_#{random_string}#{extension}"
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/mindee/input/sources.rb
CHANGED
@@ -1,250 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require_relative '
|
7
|
-
require_relative '
|
8
|
-
|
9
|
-
module Mindee
|
10
|
-
module Input
|
11
|
-
# Document source handling.
|
12
|
-
module Source
|
13
|
-
# Mime types accepted by the server.
|
14
|
-
ALLOWED_MIME_TYPES = [
|
15
|
-
'application/pdf',
|
16
|
-
'image/heic',
|
17
|
-
'image/png',
|
18
|
-
'image/jpeg',
|
19
|
-
'image/tiff',
|
20
|
-
'image/webp',
|
21
|
-
].freeze
|
22
|
-
|
23
|
-
# Standard error for invalid mime types
|
24
|
-
class MimeTypeError < StandardError
|
25
|
-
end
|
26
|
-
|
27
|
-
# Error sent if the file's mimetype isn't allowed
|
28
|
-
class InvalidMimeTypeError < MimeTypeError
|
29
|
-
# @return [String]
|
30
|
-
attr_reader :invalid_mimetype
|
31
|
-
|
32
|
-
# @param mime_type [String]
|
33
|
-
def initialize(mime_type)
|
34
|
-
@invalid_mimetype = mime_type
|
35
|
-
super("'#{@invalid_mimetype}' mime type not allowed, must be one of #{ALLOWED_MIME_TYPES.join(', ')}")
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# Error sent if a pdf file couldn't be fixed
|
40
|
-
class UnfixablePDFError < MimeTypeError
|
41
|
-
def initialize
|
42
|
-
super("Corrupted PDF couldn't be repaired.")
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Base class for loading documents.
|
47
|
-
class LocalInputSource
|
48
|
-
# @return [String]
|
49
|
-
attr_reader :filename
|
50
|
-
# @return [String]
|
51
|
-
attr_reader :file_mimetype
|
52
|
-
# @return [StringIO]
|
53
|
-
attr_reader :io_stream
|
54
|
-
|
55
|
-
# @param io_stream [StringIO]
|
56
|
-
# @param filename [String]
|
57
|
-
# @param fix_pdf [Boolean]
|
58
|
-
def initialize(io_stream, filename, fix_pdf: false)
|
59
|
-
@io_stream = io_stream
|
60
|
-
@filename = filename
|
61
|
-
@file_mimetype = if fix_pdf
|
62
|
-
Marcel::MimeType.for @io_stream
|
63
|
-
else
|
64
|
-
Marcel::MimeType.for @io_stream, name: @filename
|
65
|
-
end
|
66
|
-
return if ALLOWED_MIME_TYPES.include? @file_mimetype
|
67
|
-
|
68
|
-
if filename.end_with?('.pdf') && fix_pdf
|
69
|
-
rescue_broken_pdf(@io_stream)
|
70
|
-
@file_mimetype = Marcel::MimeType.for @io_stream
|
71
|
-
|
72
|
-
return if ALLOWED_MIME_TYPES.include? @file_mimetype
|
73
|
-
end
|
74
|
-
|
75
|
-
raise InvalidMimeTypeError, @file_mimetype.to_s
|
76
|
-
end
|
77
|
-
|
78
|
-
# Attempts to fix pdf files if mimetype is rejected.
|
79
|
-
# "Broken PDFs" are often a result of third-party injecting invalid headers.
|
80
|
-
# This attempts to remove them and send the file
|
81
|
-
# @param stream [StringIO]
|
82
|
-
def rescue_broken_pdf(stream)
|
83
|
-
stream.gets('%PDF-')
|
84
|
-
raise UnfixablePDFError if stream.eof? || stream.pos > 500
|
85
|
-
|
86
|
-
stream.pos = stream.pos - 5
|
87
|
-
data = stream.read
|
88
|
-
@io_stream.close
|
89
|
-
|
90
|
-
@io_stream = StringIO.new
|
91
|
-
@io_stream << data
|
92
|
-
end
|
93
|
-
|
94
|
-
# Shorthand for pdf mimetype validation.
|
95
|
-
def pdf?
|
96
|
-
@file_mimetype.to_s == 'application/pdf'
|
97
|
-
end
|
98
|
-
|
99
|
-
# Parses a PDF file according to provided options.
|
100
|
-
# @param options [Hash, nil] Page cutting/merge options:
|
101
|
-
#
|
102
|
-
# * `:page_indexes` Zero-based list of page indexes.
|
103
|
-
# * `:operation` Operation to apply on the document, given the `page_indexes specified:
|
104
|
-
# * `:KEEP_ONLY` - keep only the specified pages, and remove all others.
|
105
|
-
# * `:REMOVE` - remove the specified pages, and keep all others.
|
106
|
-
# * `:on_min_pages` Apply the operation only if document has at least this many pages.
|
107
|
-
def process_pdf(options)
|
108
|
-
@io_stream.seek(0)
|
109
|
-
@io_stream = PdfProcessor.parse(@io_stream, options)
|
110
|
-
end
|
111
|
-
|
112
|
-
# Reads a document.
|
113
|
-
# @param close [Boolean]
|
114
|
-
# @return [Array<String, [String, aBinaryString ], [Hash, nil] >]
|
115
|
-
def read_document(close: true)
|
116
|
-
@io_stream.seek(0)
|
117
|
-
# Avoids needlessly re-packing some files
|
118
|
-
data = @io_stream.read
|
119
|
-
@io_stream.close if close
|
120
|
-
['document', data, { filename: Mindee::Input::Source.convert_to_unicode_escape(@filename) }]
|
121
|
-
end
|
122
|
-
|
123
|
-
def count_pdf_pages
|
124
|
-
return 1 unless pdf?
|
125
|
-
|
126
|
-
@io_stream.seek(0)
|
127
|
-
pdf_processor = Mindee::PDF::PdfProcessor.open_pdf(@io_stream)
|
128
|
-
pdf_processor.pages.size
|
129
|
-
end
|
130
|
-
|
131
|
-
# Compresses the file, according to the provided info.
|
132
|
-
# @param [Integer] quality Quality of the output file.
|
133
|
-
# @param [Integer, nil] max_width Maximum width (Ignored for PDFs).
|
134
|
-
# @param [Integer, nil] max_height Maximum height (Ignored for PDFs).
|
135
|
-
# @param [Boolean] force_source_text Whether to force the operation on PDFs with source text.
|
136
|
-
# This will attempt to re-render PDF text over the rasterized original. If disabled, ignored the operation.
|
137
|
-
# WARNING: this operation is strongly discouraged.
|
138
|
-
# @param [Boolean] disable_source_text If the PDF has source text, whether to re-apply it to the original or
|
139
|
-
# not. Needs force_source_text to work.
|
140
|
-
def compress!(quality: 85, max_width: nil, max_height: nil, force_source_text: false, disable_source_text: true)
|
141
|
-
buffer = if pdf?
|
142
|
-
Mindee::PDF::PDFCompressor.compress_pdf(
|
143
|
-
@io_stream,
|
144
|
-
quality: quality,
|
145
|
-
force_source_text_compression: force_source_text,
|
146
|
-
disable_source_text: disable_source_text
|
147
|
-
)
|
148
|
-
else
|
149
|
-
Mindee::Image::ImageCompressor.compress_image(
|
150
|
-
@io_stream,
|
151
|
-
quality: quality,
|
152
|
-
max_width: max_width,
|
153
|
-
max_height: max_height
|
154
|
-
)
|
155
|
-
end
|
156
|
-
@io_stream = buffer
|
157
|
-
@io_stream.rewind
|
158
|
-
end
|
159
|
-
|
160
|
-
# Checks whether the file has source text if it is a pdf. False otherwise
|
161
|
-
# @return [Boolean] True if the file is a PDF and has source text.
|
162
|
-
def source_text?
|
163
|
-
Mindee::PDF::PDFTools.source_text?(@io_stream)
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
# Load a document from a path.
|
168
|
-
class PathInputSource < LocalInputSource
|
169
|
-
# @param filepath [String]
|
170
|
-
# @param fix_pdf [Boolean]
|
171
|
-
def initialize(filepath, fix_pdf: false)
|
172
|
-
io_stream = File.open(filepath, 'rb')
|
173
|
-
super(io_stream, File.basename(filepath), fix_pdf: fix_pdf)
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
# Load a document from a base64 string.
|
178
|
-
class Base64InputSource < LocalInputSource
|
179
|
-
# @param base64_string [String]
|
180
|
-
# @param filename [String]
|
181
|
-
# @param fix_pdf [Boolean]
|
182
|
-
def initialize(base64_string, filename, fix_pdf: false)
|
183
|
-
io_stream = StringIO.new(base64_string.unpack1('m*'))
|
184
|
-
io_stream.set_encoding Encoding::BINARY
|
185
|
-
super(io_stream, filename, fix_pdf: fix_pdf)
|
186
|
-
end
|
187
|
-
|
188
|
-
# Overload of the same function to prevent a base64 from being re-encoded.
|
189
|
-
# @param close [Boolean]
|
190
|
-
# @return [Array<String, [String, aBinaryString ], [Hash, nil] >]
|
191
|
-
def read_document(close: true)
|
192
|
-
@io_stream.seek(0)
|
193
|
-
data = @io_stream.read
|
194
|
-
@io_stream.close if close
|
195
|
-
['document', [data].pack('m'), { filename: Source.convert_to_unicode_escape(@filename) }]
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
# Load a document from raw bytes.
|
200
|
-
class BytesInputSource < LocalInputSource
|
201
|
-
# @param raw_bytes [String]
|
202
|
-
# @param filename [String]
|
203
|
-
# @param fix_pdf [Boolean]
|
204
|
-
def initialize(raw_bytes, filename, fix_pdf: false)
|
205
|
-
io_stream = StringIO.new(raw_bytes)
|
206
|
-
io_stream.set_encoding Encoding::BINARY
|
207
|
-
super(io_stream, filename, fix_pdf: fix_pdf)
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
# Load a document from a file handle.
|
212
|
-
class FileInputSource < LocalInputSource
|
213
|
-
# @param input_file [File]
|
214
|
-
# @param filename [String]
|
215
|
-
# @param fix_pdf [Boolean]
|
216
|
-
def initialize(input_file, filename, fix_pdf: false)
|
217
|
-
io_stream = input_file
|
218
|
-
super(io_stream, filename, fix_pdf: fix_pdf)
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
# Load a remote document from a file url.
|
223
|
-
class UrlInputSource
|
224
|
-
# @return [String]
|
225
|
-
attr_reader :url
|
226
|
-
|
227
|
-
def initialize(url)
|
228
|
-
raise 'URL must be HTTPS' unless url.start_with? 'https://'
|
229
|
-
|
230
|
-
@url = url
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
|
-
# Replaces non-ASCII characters by their unicode escape sequence.
|
235
|
-
# Keeps other characters as is.
|
236
|
-
# @return A clean String.
|
237
|
-
def self.convert_to_unicode_escape(string)
|
238
|
-
unicode_escape_string = ''.dup
|
239
|
-
string.each_char do |char|
|
240
|
-
unicode_escape_string << if char.bytesize > 1
|
241
|
-
"\\u#{char.unpack1('U').to_s(16).rjust(4, '0')}"
|
242
|
-
else
|
243
|
-
char
|
244
|
-
end
|
245
|
-
end
|
246
|
-
unicode_escape_string
|
247
|
-
end
|
248
|
-
end
|
249
|
-
end
|
250
|
-
end
|
3
|
+
require_relative 'sources/local_input_source'
|
4
|
+
require_relative 'sources/bytes_input_source'
|
5
|
+
require_relative 'sources/base64_input_source'
|
6
|
+
require_relative 'sources/file_input_source'
|
7
|
+
require_relative 'sources/path_input_source'
|
8
|
+
require_relative 'sources/url_input_source'
|
@@ -6,7 +6,7 @@ module Mindee
|
|
6
6
|
module Product
|
7
7
|
module IND
|
8
8
|
module IndianPassport
|
9
|
-
# Passport - India API version 1.
|
9
|
+
# Passport - India API version 1.2 document data.
|
10
10
|
class IndianPassportV1Document < Mindee::Parsing::Common::Prediction
|
11
11
|
include Mindee::Parsing::Standard
|
12
12
|
# The first line of the address of the passport holder.
|
@@ -7,7 +7,7 @@ module Mindee
|
|
7
7
|
module Product
|
8
8
|
module IND
|
9
9
|
module IndianPassport
|
10
|
-
# Passport - India API version 1.
|
10
|
+
# Passport - India API version 1.2 page data.
|
11
11
|
class IndianPassportV1Page < Mindee::Parsing::Common::Page
|
12
12
|
# @param prediction [Hash]
|
13
13
|
def initialize(prediction)
|
@@ -1,27 +1,27 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative '../../../parsing'
|
4
|
-
require_relative '
|
5
|
-
require_relative '
|
4
|
+
require_relative 'us_mail_v3_document'
|
5
|
+
require_relative 'us_mail_v3_page'
|
6
6
|
|
7
7
|
module Mindee
|
8
8
|
module Product
|
9
|
-
module
|
10
|
-
#
|
11
|
-
module
|
12
|
-
#
|
13
|
-
class
|
14
|
-
@endpoint_name = '
|
15
|
-
@endpoint_version = '
|
9
|
+
module US
|
10
|
+
# US Mail module.
|
11
|
+
module UsMail
|
12
|
+
# US Mail API version 3 inference prediction.
|
13
|
+
class UsMailV3 < Mindee::Parsing::Common::Inference
|
14
|
+
@endpoint_name = 'us_mail'
|
15
|
+
@endpoint_version = '3'
|
16
16
|
|
17
17
|
# @param prediction [Hash]
|
18
18
|
def initialize(prediction)
|
19
19
|
super
|
20
|
-
@prediction =
|
20
|
+
@prediction = UsMailV3Document.new(prediction['prediction'], nil)
|
21
21
|
@pages = []
|
22
22
|
prediction['pages'].each do |page|
|
23
23
|
if page.key?('prediction') && !page['prediction'].nil? && !page['prediction'].empty?
|
24
|
-
@pages.push(
|
24
|
+
@pages.push(UsMailV3Page.new(page))
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative '../../../parsing'
|
4
|
+
require_relative 'us_mail_v3_sender_address'
|
5
|
+
require_relative 'us_mail_v3_recipient_address'
|
6
|
+
|
7
|
+
module Mindee
|
8
|
+
module Product
|
9
|
+
module US
|
10
|
+
module UsMail
|
11
|
+
# US Mail API version 3.0 document data.
|
12
|
+
class UsMailV3Document < Mindee::Parsing::Common::Prediction
|
13
|
+
include Mindee::Parsing::Standard
|
14
|
+
# Whether the mailing is marked as return to sender.
|
15
|
+
# @return [Mindee::Parsing::Standard::BooleanField]
|
16
|
+
attr_reader :is_return_to_sender
|
17
|
+
# The addresses of the recipients.
|
18
|
+
# @return [Array<Mindee::Product::US::UsMail::UsMailV3RecipientAddress>]
|
19
|
+
attr_reader :recipient_addresses
|
20
|
+
# The names of the recipients.
|
21
|
+
# @return [Array<Mindee::Parsing::Standard::StringField>]
|
22
|
+
attr_reader :recipient_names
|
23
|
+
# The address of the sender.
|
24
|
+
# @return [Mindee::Product::US::UsMail::UsMailV3SenderAddress]
|
25
|
+
attr_reader :sender_address
|
26
|
+
# The name of the sender.
|
27
|
+
# @return [Mindee::Parsing::Standard::StringField]
|
28
|
+
attr_reader :sender_name
|
29
|
+
|
30
|
+
# @param prediction [Hash]
|
31
|
+
# @param page_id [Integer, nil]
|
32
|
+
def initialize(prediction, page_id)
|
33
|
+
super()
|
34
|
+
@is_return_to_sender = BooleanField.new(prediction['is_return_to_sender'], page_id)
|
35
|
+
@recipient_addresses = []
|
36
|
+
prediction['recipient_addresses'].each do |item|
|
37
|
+
@recipient_addresses.push(UsMailV3RecipientAddress.new(item, page_id))
|
38
|
+
end
|
39
|
+
@recipient_names = []
|
40
|
+
prediction['recipient_names'].each do |item|
|
41
|
+
@recipient_names.push(StringField.new(item, page_id))
|
42
|
+
end
|
43
|
+
@sender_address = UsMailV3SenderAddress.new(prediction['sender_address'], page_id)
|
44
|
+
@sender_name = StringField.new(prediction['sender_name'], page_id)
|
45
|
+
end
|
46
|
+
|
47
|
+
# @return [String]
|
48
|
+
def to_s
|
49
|
+
sender_address = @sender_address.to_s
|
50
|
+
recipient_names = @recipient_names.join("\n #{' ' * 17}")
|
51
|
+
recipient_addresses = recipient_addresses_to_s
|
52
|
+
out_str = String.new
|
53
|
+
out_str << "\n:Sender Name: #{@sender_name}".rstrip
|
54
|
+
out_str << "\n:Sender Address:"
|
55
|
+
out_str << sender_address
|
56
|
+
out_str << "\n:Recipient Names: #{recipient_names}".rstrip
|
57
|
+
out_str << "\n:Recipient Addresses:"
|
58
|
+
out_str << recipient_addresses
|
59
|
+
out_str << "\n:Return to Sender: #{@is_return_to_sender}".rstrip
|
60
|
+
out_str[1..].to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
# @param char [String]
|
66
|
+
# @return [String]
|
67
|
+
def recipient_addresses_separator(char)
|
68
|
+
out_str = String.new
|
69
|
+
out_str << ' '
|
70
|
+
out_str << "+#{char * 17}"
|
71
|
+
out_str << "+#{char * 37}"
|
72
|
+
out_str << "+#{char * 19}"
|
73
|
+
out_str << "+#{char * 13}"
|
74
|
+
out_str << "+#{char * 24}"
|
75
|
+
out_str << "+#{char * 7}"
|
76
|
+
out_str << "+#{char * 27}"
|
77
|
+
out_str << "+#{char * 17}"
|
78
|
+
out_str << '+'
|
79
|
+
out_str
|
80
|
+
end
|
81
|
+
|
82
|
+
# @return [String]
|
83
|
+
def recipient_addresses_to_s
|
84
|
+
return '' if @recipient_addresses.empty?
|
85
|
+
|
86
|
+
line_items = @recipient_addresses.map(&:to_table_line).join("\n#{recipient_addresses_separator('-')}\n ")
|
87
|
+
out_str = String.new
|
88
|
+
out_str << "\n#{recipient_addresses_separator('-')}"
|
89
|
+
out_str << "\n |"
|
90
|
+
out_str << ' City |'
|
91
|
+
out_str << ' Complete Address |'
|
92
|
+
out_str << ' Is Address Change |'
|
93
|
+
out_str << ' Postal Code |'
|
94
|
+
out_str << ' Private Mailbox Number |'
|
95
|
+
out_str << ' State |'
|
96
|
+
out_str << ' Street |'
|
97
|
+
out_str << ' Unit |'
|
98
|
+
out_str << "\n#{recipient_addresses_separator('=')}"
|
99
|
+
out_str << "\n #{line_items}"
|
100
|
+
out_str << "\n#{recipient_addresses_separator('-')}"
|
101
|
+
out_str
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/lib/mindee/product/{fr/carte_vitale/carte_vitale_v1_page.rb → us/us_mail/us_mail_v3_page.rb}
RENAMED
@@ -1,26 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative '../../../parsing'
|
4
|
-
require_relative '
|
4
|
+
require_relative 'us_mail_v3_document'
|
5
5
|
|
6
6
|
module Mindee
|
7
7
|
module Product
|
8
|
-
module
|
9
|
-
module
|
10
|
-
#
|
11
|
-
class
|
8
|
+
module US
|
9
|
+
module UsMail
|
10
|
+
# US Mail API version 3.0 page data.
|
11
|
+
class UsMailV3Page < Mindee::Parsing::Common::Page
|
12
12
|
# @param prediction [Hash]
|
13
13
|
def initialize(prediction)
|
14
14
|
super(prediction)
|
15
|
-
@prediction =
|
15
|
+
@prediction = UsMailV3PagePrediction.new(
|
16
16
|
prediction['prediction'],
|
17
17
|
prediction['id']
|
18
18
|
)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
#
|
23
|
-
class
|
22
|
+
# US Mail V3 page prediction.
|
23
|
+
class UsMailV3PagePrediction < UsMailV3Document
|
24
24
|
# @return [String]
|
25
25
|
def to_s
|
26
26
|
out_str = String.new
|