Skip to content

Commit 821a889

Browse files
✨ add support for US Mail V3 (#135)
1 parent 5667c8c commit 821a889

File tree

12 files changed

+455
-14
lines changed

12 files changed

+455
-14
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'mindee'
2+
3+
# Init a new client
4+
mindee_client = Mindee::Client.new(api_key: 'my-api-key')
5+
6+
# Load a file from disk
7+
input_source = mindee_client.source_from_path('/path/to/the/file.ext')
8+
9+
# Parse the file
10+
result = mindee_client.enqueue_and_parse(
11+
input_source,
12+
Mindee::Product::US::UsMail::UsMailV3
13+
)
14+
15+
# Print a full summary of the parsed data in RST format
16+
puts result.document
17+
18+
# Print the document-level parsed data
19+
# puts result.document.inference.prediction

docs/us_mail_v2.md renamed to docs/us_mail_v3.md

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ input_source = mindee_client.source_from_path('/path/to/the/file.ext')
2222
# Parse the file
2323
result = mindee_client.enqueue_and_parse(
2424
input_source,
25-
Mindee::Product::US::UsMail::UsMailV2
25+
Mindee::Product::US::UsMail::UsMailV3
2626
)
2727

2828
# Print a full summary of the parsed data in RST format
@@ -35,7 +35,20 @@ puts result.document
3535

3636
**Output (RST):**
3737
```rst
38-
:Sender Name: zed
38+
########
39+
Document
40+
########
41+
:Mindee ID: f9c36f59-977d-4ddc-9f2d-31c294c456ac
42+
:Filename: default_sample.jpg
43+
44+
Inference
45+
#########
46+
:Product: mindee/us_mail v3.0
47+
:Rotation applied: Yes
48+
49+
Prediction
50+
==========
51+
:Sender Name: company zed
3952
:Sender Address:
4053
:City: Dallas
4154
:Complete Address: 54321 Elm Street, Dallas, Texas 54321
@@ -44,11 +57,12 @@ puts result.document
4457
:Street: 54321 Elm Street
4558
:Recipient Names: Jane Doe
4659
:Recipient Addresses:
47-
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+
48-
| City | Complete Address | Is Address Change | Postal Code | Private Mailbox Number | State | Street |
49-
+=================+=====================================+===================+=============+========================+=======+===========================+
50-
| Detroit | 1234 Market Street PMB 4321, Det... | | 12345 | 4321 | MI | 1234 Market Street |
51-
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+
60+
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+-----------------+
61+
| City | Complete Address | Is Address Change | Postal Code | Private Mailbox Number | State | Street | Unit |
62+
+=================+=====================================+===================+=============+========================+=======+===========================+=================+
63+
| Detroit | 1234 Market Street PMB 4321, Det... | False | 12345 | 4321 | MI | 1234 Market Street | |
64+
+-----------------+-------------------------------------+-------------------+-------------+------------------------+-------+---------------------------+-----------------+
65+
:Return to Sender: False
5266
```
5367

5468
# Field Types
@@ -78,7 +92,7 @@ Fields which are specific to this product; they are not used in any other produc
7892
### Recipient Addresses Field
7993
The addresses of the recipients.
8094

81-
A `UsMailV2RecipientAddress` implements the following attributes:
95+
A `UsMailV3RecipientAddress` implements the following attributes:
8296

8397
* `city` (String): The city of the recipient's address.
8498
* `complete` (String): The complete address of the recipient.
@@ -87,12 +101,13 @@ A `UsMailV2RecipientAddress` implements the following attributes:
87101
* `private_mailbox_number` (String): The private mailbox number of the recipient's address.
88102
* `state` (String): Second part of the ISO 3166-2 code, consisting of two letters indicating the US State.
89103
* `street` (String): The street of the recipient's address.
104+
* `unit` (String): The unit number of the recipient's address.
90105
Fields which are specific to this product; they are not used in any other product.
91106

92107
### Sender Address Field
93108
The address of the sender.
94109

95-
A `UsMailV2SenderAddress` implements the following attributes:
110+
A `UsMailV3SenderAddress` implements the following attributes:
96111

97112
* `city` (String): The city of the sender's address.
98113
* `complete` (String): The complete address of the sender.
@@ -101,10 +116,17 @@ A `UsMailV2SenderAddress` implements the following attributes:
101116
* `street` (String): The street of the sender's address.
102117

103118
# Attributes
104-
The following fields are extracted for US Mail V2:
119+
The following fields are extracted for US Mail V3:
120+
121+
## Return to Sender
122+
**is_return_to_sender** ([BooleanField](#boolean-field)): Whether the mailing is marked as return to sender.
123+
124+
```rb
125+
puts result.document.inference.prediction.is_return_to_sender.value
126+
```
105127

106128
## Recipient Addresses
107-
**recipient_addresses** (Array<[UsMailV2RecipientAddress](#recipient-addresses-field)>): The addresses of the recipients.
129+
**recipient_addresses** (Array<[UsMailV3RecipientAddress](#recipient-addresses-field)>): The addresses of the recipients.
108130

109131
```rb
110132
for recipient_addresses_elem in result.document.inference.prediction.recipient_addresses do
@@ -122,7 +144,7 @@ end
122144
```
123145

124146
## Sender Address
125-
**sender_address** ([UsMailV2SenderAddress](#sender-address-field)): The address of the sender.
147+
**sender_address** ([UsMailV3SenderAddress](#sender-address-field)): The address of the sender.
126148

127149
```rb
128150
puts result.document.inference.prediction.sender_address.value

lib/mindee/parsing/standard/boolean_field.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ class BooleanField < Field
1414
def initialize(prediction, page_id = nil, reconstructed: false)
1515
super
1616
end
17+
18+
def to_s
19+
return '' if value.nil?
20+
21+
value ? 'True' : 'False'
22+
end
1723
end
1824
end
1925
end

lib/mindee/product.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@
3636
require_relative 'product/us/driver_license/driver_license_v1'
3737
require_relative 'product/us/healthcare_card/healthcare_card_v1'
3838
require_relative 'product/us/us_mail/us_mail_v2'
39+
require_relative 'product/us/us_mail/us_mail_v3'
3940
require_relative 'product/us/w9/w9_v1'

lib/mindee/product/ind/indian_passport/indian_passport_v1_document.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Mindee
66
module Product
77
module IND
88
module IndianPassport
9-
# Passport - India API version 1.0 document data.
9+
# Passport - India API version 1.2 document data.
1010
class IndianPassportV1Document < Mindee::Parsing::Common::Prediction
1111
include Mindee::Parsing::Standard
1212
# The first line of the address of the passport holder.

lib/mindee/product/ind/indian_passport/indian_passport_v1_page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Mindee
77
module Product
88
module IND
99
module IndianPassport
10-
# Passport - India API version 1.0 page data.
10+
# Passport - India API version 1.2 page data.
1111
class IndianPassportV1Page < Mindee::Parsing::Common::Page
1212
# @param prediction [Hash]
1313
def initialize(prediction)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../../parsing'
4+
require_relative 'us_mail_v3_document'
5+
require_relative 'us_mail_v3_page'
6+
7+
module Mindee
8+
module Product
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+
17+
# @param prediction [Hash]
18+
def initialize(prediction)
19+
super
20+
@prediction = UsMailV3Document.new(prediction['prediction'], nil)
21+
@pages = []
22+
prediction['pages'].each do |page|
23+
if page.key?('prediction') && !page['prediction'].nil? && !page['prediction'].empty?
24+
@pages.push(UsMailV3Page.new(page))
25+
end
26+
end
27+
end
28+
29+
class << self
30+
# Name of the endpoint for this product.
31+
# @return [String]
32+
attr_reader :endpoint_name
33+
# Version for this product.
34+
# @return [String]
35+
attr_reader :endpoint_version
36+
end
37+
end
38+
end
39+
end
40+
end
41+
end
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../../../parsing'
4+
require_relative 'us_mail_v3_document'
5+
6+
module Mindee
7+
module Product
8+
module US
9+
module UsMail
10+
# US Mail API version 3.0 page data.
11+
class UsMailV3Page < Mindee::Parsing::Common::Page
12+
# @param prediction [Hash]
13+
def initialize(prediction)
14+
super(prediction)
15+
@prediction = UsMailV3PagePrediction.new(
16+
prediction['prediction'],
17+
prediction['id']
18+
)
19+
end
20+
end
21+
22+
# US Mail V3 page prediction.
23+
class UsMailV3PagePrediction < UsMailV3Document
24+
# @return [String]
25+
def to_s
26+
out_str = String.new
27+
out_str << "\n#{super}"
28+
out_str
29+
end
30+
end
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)