idp_ruby 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8f634f75943a1968dc490b1a312c66cba30edd8f8853ab8fd5620fbba53f4dea
4
+ data.tar.gz: e5fa3c006d4bd66695dc839b53126e3f877939efd4973786526ffbe308b60608
5
+ SHA512:
6
+ metadata.gz: b8c3b1133d1af8512dd8da7bf0af857e5be2eacf8f7360aad2a7594631bad5288d72a357505239acd20a9762820b8eea6b7463c429ed16cc9cc445ea3e9b0088
7
+ data.tar.gz: f49db71a6c7016d1686527c15ecc3cfc0c59d8b0baab9e4610dd2e2d87de9366c55e3b366a620b141e18f8ae28d7b4dcce8a250b8ada1f931e8265f7dd3ee043
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at anyang@6estates.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in idp_ruby.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 “anyang”
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # 6Estates idp-ruby
2
+
3
+ A Ruby SDK for communicating with the 6Estates Intelligent Document Processing(IDP) Platform.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add idp_sdk_ruby
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install idp_sdk_ruby
14
+
15
+ ## Documentation
16
+ The documentation for the 6Estates IDP API can be found via https://idp-sea.6estates.com/docs
17
+
18
+
19
+ ## Usage
20
+
21
+ ### 1. Initialize the 6Estates IDP Client
22
+ 6E API Access Token(Deprecated)
23
+
24
+ ```ruby
25
+ require 'idp_sdk_ruby'
26
+
27
+ c=IdpSdkRuby::Client.new(region:"your-region", token:'your-token-here')
28
+ ```
29
+
30
+ 6E API Authorization based on oauth 2.0
31
+ ```ruby
32
+ require 'idp_sdk_ruby'
33
+
34
+ oauth=IdpSdkRuby::oauthutil(region:"your-region", authorization: "your-authorization-here")
35
+ c=IdpSdkRuby::Client.new(region:"your-region", token:oauth, isOauth:true)
36
+
37
+ ```
38
+ ### 2. To Extract Fields in Synchronous Way
39
+ If you just need to do one file at a time
40
+
41
+ ```ruby
42
+ require 'idp_sdk_ruby'
43
+ c=IdpSdkRuby::Client.new(region:"test", token:'your-token-here')
44
+ task_result=c.extraction_task.run_simple_task(file:File.new('path-to-the-file',"rb"), file_type:IdpSdkRuby::FileType.new().full_name_of_the_file_type)
45
+ ```
46
+
47
+ ### 3. To Extract Fields in Asynchronous Way
48
+ If you need to do a batch of files
49
+
50
+ ```ruby
51
+ require 'idp_sdk_ruby'
52
+ c=IdpSdkRuby::Client.new(region:"test", token:'your-token-here')
53
+ task=c.extraction_task.create(file:File.new('path-to-the-file',"rb"), file_type:IdpSdkRuby::FileType.new().full_name_of_the_file_type)
54
+ task_result = result(task.task_id)
55
+ while(task_result.status=='Doing' or task_result.status=='Init')
56
+ sleep(3)
57
+ task_result = result(task.task_id)
58
+ end
59
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "idp_ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/idp_ruby.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/idp_ruby/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "idp_ruby"
5
+ spec.version = IdpRuby::VERSION
6
+ spec.authors = ["“anyang”"]
7
+ spec.email = ["anyang@6estates.com"]
8
+
9
+ spec.summary = "idp_sdk_ruby"
10
+ spec.description = "idp_sdk_ruby"
11
+ spec.homepage = "https://rubygems.org/gems/idp_ruby"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ #spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ #spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
+ #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,3 @@
1
+ module IdpRuby
2
+ VERSION = "0.1.0"
3
+ end
data/lib/idp_ruby.rb ADDED
@@ -0,0 +1,258 @@
1
+ #idp-sdk-ruby.rb
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ require 'rest_client'
5
+ require 'json'
6
+ require_relative 'idp_sdk_ruby/version'
7
+
8
+ module IdpSdkRuby
9
+ class FileType
10
+ def bank_statement
11
+ return 'CBKS'
12
+ end
13
+ def invoice
14
+ return 'CINV'
15
+ end
16
+ def cheque
17
+ return 'CHQ'
18
+ end
19
+ def credit_bureau_singapore
20
+ return 'CBS'
21
+ end
22
+ def receipt
23
+ return 'RCPT'
24
+ end
25
+ def payslip
26
+ return 'PS'
27
+ end
28
+ def packing_list
29
+ return 'PL'
30
+ end
31
+ def bill_of_lading
32
+ return 'BL'
33
+ end
34
+ def air_waybill
35
+ return 'AWBL'
36
+ end
37
+ def kartu_tanda_penduduk
38
+ return 'KTP'
39
+ end
40
+ def hong_kong_annual_return
41
+ return 'HKAR'
42
+ end
43
+ def purchase_order
44
+ return 'PO'
45
+ end
46
+ def delivery_order
47
+ return 'DO'
48
+ end
49
+ def FileType
50
+ return ['CBKS','CINV','CHQ','CBS','RCPT','PS','PL','BL','AWBL','KTP','HKAR','PO','DO']
51
+ end
52
+ end
53
+
54
+ class IDPException < StandardError
55
+ end
56
+
57
+ class IDPConfigurationException < StandardError
58
+ end
59
+
60
+ class Task
61
+ def initialize(json=nil)
62
+ @raw=json
63
+ end
64
+
65
+ def task_id
66
+ return @raw['data']
67
+ end
68
+
69
+ end
70
+
71
+ class TaskResultField
72
+ def initialize(json=nil)
73
+ @raw=json
74
+ end
75
+
76
+ def field_code
77
+ return @raw['field_code']
78
+ end
79
+
80
+ def field_name
81
+ return @raw['field_name']
82
+ end
83
+
84
+ def value
85
+ return @raw['value']
86
+ end
87
+
88
+ def type
89
+ return @raw['type']
90
+ end
91
+ end
92
+
93
+ class TaskResult
94
+ def initialize(json=nil)
95
+ # puts json
96
+ @raw=json
97
+ end
98
+ def raw
99
+ # puts raw
100
+ return @raw
101
+ end
102
+ def status
103
+ # puts @raw['data']['taskStatus']
104
+ return @raw['data']['taskStatus']
105
+ end
106
+ def fields
107
+ fields=Array.new(0)
108
+ for x in @raw['data']['fields']
109
+ fields[fields.length]=TaskResultField.new(x)
110
+ end
111
+ return fields
112
+ end
113
+ end
114
+
115
+ class ExtractionTaskClient
116
+ def initialize(token:nil,region:nil, isOauth:nil)
117
+ @token=token
118
+ @region=region
119
+ @isOauth=isOauth
120
+
121
+ if region === 'test'
122
+ region = ''
123
+ else
124
+ region = '-'+region
125
+ end
126
+ @url_post = "https://idp"+region + \
127
+ ".6estates.com/customer/extraction/fields/async"
128
+ @url_get = "https://idp"+region + \
129
+ ".6estates.com/customer/extraction/field/async/result/"
130
+ end
131
+
132
+ def create(file:nil, file_type:nil, lang:nil,
133
+ customer:nil, customer_param:nil, callback:nil,
134
+ auto_callback:nil, callback_mode:nil, hitl:nil)
135
+ if FileType.new().FileType.count {|x|x==file_type} == 0
136
+ raise IDPException.new("Invalid file type")
137
+ end
138
+ if file.nil?
139
+ raise IDPException.new("File is required")
140
+ end
141
+ if @isOauth
142
+ headers = {"Authorization"=> @token}
143
+ else
144
+ headers = {"X-ACCESS-TOKEN"=> @token}
145
+ end
146
+ # files = {"file": file}
147
+ data = {'fileType'=> file_type, 'lang'=> lang, 'customer'=> customer,
148
+ 'customerParam'=> customer_param, 'callback'=> callback,
149
+ 'autoCallback'=>auto_callback, 'callbackMode'=> callback_mode,
150
+ 'hitl'=> hitl, "file"=>file}
151
+ data=data.delete_if{|key,value|value.nil?}
152
+
153
+ r = JSON.parse(RestClient.post(@url_post, data, headers))
154
+ if r['status']==200
155
+ return Task.new(r)
156
+ end
157
+ raise IDPException.new(r['message'])
158
+ end
159
+
160
+ def result(task_id=nil)
161
+ if task_id.nil?
162
+ raise IDPConfigurationException.new('Task ID is required')
163
+ end
164
+ if @isOauth
165
+ headers = {"Authorization"=> @token}
166
+ else
167
+ headers = {"X-ACCESS-TOKEN"=> @token}
168
+ end
169
+ r = JSON.parse(RestClient.get(@url_get+task_id.to_s, headers))
170
+
171
+ if r['status']==200
172
+ return TaskResult.new(r)
173
+ end
174
+ raise IDPException.new(r['message'])
175
+ end
176
+
177
+ def run_simple_task(file:nil, file_type:nil, poll_interval:3, timeout:600)
178
+ """
179
+ Run simple extraction task
180
+ :param file: Pdf/image file. Only one file is allowed to be uploaded each time
181
+ :type file: file
182
+ :param file_type: The code of the file type (e.g., CBKS). Please see details of File Type Code.
183
+ :type file_type: FileType
184
+ :param poll_interval: Interval to poll the result from api, in seconds
185
+ :type poll_interval: float
186
+ :param timeout: Timeout in seconds
187
+ :type timeout: float
188
+ """
189
+ ct=timeout/poll_interval
190
+ task = create(file:file, file_type:file_type)
191
+ task_result = result(task.task_id)
192
+ while(task_result.status=='Doing' or task_result.status=='Init')
193
+ if (ct-=1) == 0
194
+ raise IDPException.new('Task timeout exceeded: {timeout}')
195
+ end
196
+ puts "Task is doing. Please wait."
197
+ sleep(poll_interval)
198
+ task_result = result(task.task_id)
199
+
200
+ if task_result.status == 'Done'
201
+ return task_result
202
+ end
203
+ end
204
+ return task_result
205
+ end
206
+ end
207
+
208
+ class Client
209
+ def initialize(token:nil,region:nil, isOauth:false)
210
+ if token.nil?
211
+ raise IDPConfigurationException.new('Token is required')
212
+ end
213
+ if ["test", "sea"].count {|x|x==region} == 0
214
+ raise IDPConfigurationException.new(
215
+ "Region is required and limited in ['test','sea']")
216
+ end
217
+
218
+ @token=token
219
+ @region=region
220
+ @isOauth=isOauth
221
+ @extraction_task=ExtractionTaskClient.new(token:token,region:region, isOauth:isOauth)
222
+
223
+ end
224
+ def extraction_task
225
+ return @extraction_task
226
+ end
227
+ end
228
+
229
+ def IdpSdkRuby.oauthutil(region:nil, authorization:nil)
230
+ if authorization.nil?
231
+ raise IDPConfigurationException.new('Authorization is required')
232
+ end
233
+ if ["test", "sea"].count {|x|x==region} == 0
234
+ raise IDPConfigurationException.new(
235
+ "Region is required and limited in ['test','sea']")
236
+ end
237
+ if region === 'test'
238
+ region = '-onp'
239
+ else
240
+ region = '-'+region
241
+ end
242
+ url_post = "https://oauth"+region + \
243
+ ".6estates.com/oauth/token?grant_type=client_bind"
244
+
245
+ headers = {"Authorization" => authorization}
246
+
247
+ r = JSON.parse(RestClient.post(url_post, nil, headers))
248
+ if r['status'] != 200
249
+ raise IDPException.new(r['message'])
250
+ return
251
+ end
252
+
253
+ if r['data']['expired']
254
+ raise IDPException.new("This IDP Authorization is expired, please re-send the request to get new IDP Authorization.")
255
+ end
256
+ return r['data']['value']
257
+ end
258
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idp_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "“anyang”"
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-10-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: idp_sdk_ruby
14
+ email:
15
+ - anyang@6estates.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - CODE_OF_CONDUCT.md
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - idp_ruby.gemspec
29
+ - lib/idp_ruby.rb
30
+ - lib/idp_ruby/version.rb
31
+ homepage: https://rubygems.org/gems/idp_ruby
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://rubygems.org/gems/idp_ruby
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.1.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: idp_sdk_ruby
55
+ test_files: []