gini-api 0.9.10 → 0.9.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -3
- data/README.md +15 -2
- data/lib/gini-api/client.rb +22 -8
- data/lib/gini-api/document.rb +10 -3
- data/lib/gini-api/version.rb +1 -1
- data/spec/gini-api/document_spec.rb +47 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4119be502a05efa12b957433999a9e4c1f916a99
|
4
|
+
data.tar.gz: 3d5b8e9e41d3560aa9a458b648c649c8ee20a17f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff9a6822d02c6840fce1cb9536fd6fb669c721e1de26ec66a4fa15a5d84aae885f81559538fe63e7f725806dee731aee75e40e0840b480e390794762117a92ee
|
7
|
+
data.tar.gz: 364629b18b3adebafe20294ca3d99346dede83d92e2aa5542fe868978525af8935c93688eeca8752b12ab88fb4dd6c8c000b3ab9187969c6da34d745472beac9
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
gini-api (0.9.
|
4
|
+
gini-api (0.9.11)
|
5
5
|
logger
|
6
6
|
oauth2
|
7
7
|
|
@@ -47,7 +47,7 @@ GEM
|
|
47
47
|
guard-compat (~> 1.1)
|
48
48
|
rspec (>= 2.99.0, < 4.0)
|
49
49
|
hitimes (1.2.2)
|
50
|
-
jwt (1.
|
50
|
+
jwt (1.5.0)
|
51
51
|
listen (2.8.5)
|
52
52
|
celluloid (>= 0.15.2)
|
53
53
|
rb-fsevent (>= 0.9.3)
|
@@ -73,7 +73,7 @@ GEM
|
|
73
73
|
coderay (~> 1.1.0)
|
74
74
|
method_source (~> 0.8.1)
|
75
75
|
slop (~> 3.4)
|
76
|
-
rack (1.6.
|
76
|
+
rack (1.6.1)
|
77
77
|
rake (10.4.2)
|
78
78
|
rb-fsevent (0.9.4)
|
79
79
|
rb-inotify (0.9.5)
|
data/README.md
CHANGED
@@ -58,6 +58,10 @@ doc = api.upload(fh)
|
|
58
58
|
# => Gini::Api::Document
|
59
59
|
doc = api.upload('tmp/my_receipt.pdf', doctype_hint='Receipt')
|
60
60
|
# => Gini::Api::Document
|
61
|
+
doc = api.upload('This is a utf-8 text message i would love to get extractions from', text: true)
|
62
|
+
# => Gini::Api::Document
|
63
|
+
doc = api.upload('/tmp/my_doc.txt')
|
64
|
+
# => Gini::Api::Document
|
61
65
|
doc.id
|
62
66
|
# => "123456789-abcd-ef12-000000000000"
|
63
67
|
doc.progress
|
@@ -153,14 +157,23 @@ doc.extractions.undefinedLabel
|
|
153
157
|
# => nil
|
154
158
|
```
|
155
159
|
|
160
|
+
By default extractions are only fetched once and later requests return the cached values. Setting refresh=true will invalidate the cache and query the API directly.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
d.extractions(refresh: true)[:amountToPay]
|
164
|
+
# => "10.00:EUR"
|
165
|
+
doc.extractions(refresh: true).raw
|
166
|
+
# => {:extractions=>{...
|
167
|
+
```
|
168
|
+
|
156
169
|
#### Incubator
|
157
170
|
|
158
171
|
The incubator Gini API is unstable and subject of change. It allows early access to immature features which are still in research or under development. Please refer to the official API documentation for further details.
|
159
172
|
|
160
173
|
```ruby
|
161
|
-
doc.extractions(incubator
|
174
|
+
doc.extractions(incubator: true).amountLiters
|
162
175
|
# => {:amountLiters=>{:entity=>"volume", :value=>"25.34:l", :box=>{:top=>1774.0, :left=>674.0, :width=>376.0, :height=>48.0, :page=>1}}
|
163
|
-
doc.extractions(incubator
|
176
|
+
doc.extractions(incubator: true)[:amountLiters]
|
164
177
|
# => "25.34:l"
|
165
178
|
```
|
166
179
|
|
data/lib/gini-api/client.rb
CHANGED
@@ -149,28 +149,38 @@ module Gini
|
|
149
149
|
# Upload a document
|
150
150
|
#
|
151
151
|
# @param [String] file path or open filehandle of the document to upload
|
152
|
-
# @param [
|
153
|
-
# @
|
152
|
+
# @param [Hash] options Hash of available upload settings
|
153
|
+
# @option options [String] :doctype_hint Document type hint to optimize results or get incubator results
|
154
|
+
# @option options [String] :text Use given file-string as text upload
|
155
|
+
# @option options [Float] :interval Interval to poll progress
|
154
156
|
#
|
155
157
|
# @return [Gini::Api::Document] Return Gini::Api::Document object for uploaded document
|
156
158
|
#
|
157
159
|
# @example Upload and wait for completion
|
158
160
|
# doc = api.upload('/tmp/myfile.pdf')
|
159
161
|
# @example Upload with doctype hint
|
160
|
-
# doc = api.upload('/tmp/myfile.pdf', doctype_hint
|
162
|
+
# doc = api.upload('/tmp/myfile.pdf', doctype_hint: 'Receipt')
|
161
163
|
# @example Upload and monitor progress
|
162
164
|
# doc = api.upload('/tmp/myfile.pdf') { |d| puts "Progress: #{d.progress}" }
|
165
|
+
# @example Upload and monitor progress
|
166
|
+
# doc = api.upload('This is a text message i would love to get extractions from', text: true)
|
163
167
|
#
|
164
|
-
def upload(file,
|
168
|
+
def upload(file, options = {}, &block)
|
169
|
+
opts = {
|
170
|
+
doctype_hint: nil,
|
171
|
+
text: false,
|
172
|
+
interval: 0.5
|
173
|
+
}.merge(options)
|
174
|
+
|
165
175
|
duration = Hash.new(0)
|
166
176
|
|
167
177
|
# Document upload
|
168
|
-
duration[:upload], response = upload_document(file,
|
178
|
+
duration[:upload], response = upload_document(file, opts)
|
169
179
|
|
170
180
|
# Start polling (0.5s) when document has been uploaded successfully
|
171
181
|
if response.status == 201
|
172
182
|
doc = Gini::Api::Document.new(self, response.headers['location'])
|
173
|
-
duration[:processing] = poll_document(doc, interval, &block)
|
183
|
+
duration[:processing] = poll_document(doc, opts[:interval], &block)
|
174
184
|
|
175
185
|
duration[:total] = duration.values.inject(:+)
|
176
186
|
doc.duration = duration
|
@@ -319,13 +329,17 @@ module Gini
|
|
319
329
|
#
|
320
330
|
# @return [Faraday::Response] Response object from upload
|
321
331
|
#
|
322
|
-
def upload_document(file,
|
332
|
+
def upload_document(file, opts)
|
323
333
|
response = nil
|
334
|
+
|
335
|
+
# Use StringIO on file string and force utf-8
|
336
|
+
file = StringIO.new(file.force_encoding('UTF-8')) if opts[:text]
|
337
|
+
|
324
338
|
duration = Benchmark.realtime do
|
325
339
|
response = upload_connection.post do |req|
|
326
340
|
req.options[:timeout] = @upload_timeout
|
327
341
|
req.url 'documents'
|
328
|
-
req.params[:doctype] = doctype_hint if doctype_hint
|
342
|
+
req.params[:doctype] = opts[:doctype_hint] if opts[:doctype_hint]
|
329
343
|
req.headers['Content-Type'] = 'multipart/form-data'
|
330
344
|
req.headers['Authorization'] = "Bearer #{@token.token}"
|
331
345
|
req.headers.merge!(version_header)
|
data/lib/gini-api/document.rb
CHANGED
@@ -101,12 +101,19 @@ module Gini
|
|
101
101
|
|
102
102
|
# Initialize extractions from @_links and return Gini::Api::Extractions object
|
103
103
|
#
|
104
|
-
# @param [
|
104
|
+
# @param [Hash] options Options
|
105
|
+
# @option options [Boolean] :refresh Invalidate extractions cache
|
106
|
+
# @option options [Boolean] :incubator Return experimental extractions
|
105
107
|
#
|
106
108
|
# @return [Gini::Api::Document::Extractions] Return Gini::Api::Document::Extractions object for uploaded document
|
107
109
|
#
|
108
|
-
def extractions(
|
109
|
-
|
110
|
+
def extractions(options = {})
|
111
|
+
opts = { refresh: false, incubator: false }.merge(options)
|
112
|
+
if opts[:refresh] or @extractions.nil?
|
113
|
+
@extractions = Gini::Api::Document::Extractions.new(@api, @_links[:extractions], opts[:incubator])
|
114
|
+
else
|
115
|
+
@extractions
|
116
|
+
end
|
110
117
|
end
|
111
118
|
|
112
119
|
# Initialize layout from @_links[:layout] and return Gini::Api::Layout object
|
data/lib/gini-api/version.rb
CHANGED
@@ -237,12 +237,53 @@ describe Gini::Api::Document do
|
|
237
237
|
})
|
238
238
|
end
|
239
239
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
240
|
+
context 'with default options' do
|
241
|
+
|
242
|
+
it do
|
243
|
+
expect(api.token).to receive(:get).with(
|
244
|
+
"#{location}/extractions",
|
245
|
+
{ headers: { accept: header } }
|
246
|
+
).and_return(OAuth2::Response.new(ex_response))
|
247
|
+
expect(document.extractions).to be_a(Gini::Api::Document::Extractions)
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'returns the same data every time' do
|
251
|
+
expect(api.token).to receive(:get).once.with(
|
252
|
+
"#{location}/extractions",
|
253
|
+
{ headers: { accept: header } }
|
254
|
+
).and_return(OAuth2::Response.new(ex_response))
|
255
|
+
expect(document.extractions).to be_a(Gini::Api::Document::Extractions)
|
256
|
+
expect(document.extractions).to be_a(Gini::Api::Document::Extractions)
|
257
|
+
expect(document.extractions).to be_a(Gini::Api::Document::Extractions)
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
261
|
+
|
262
|
+
context 'with refresh == true' do
|
263
|
+
|
264
|
+
it do
|
265
|
+
expect(api.token).to receive(:get).twice.with(
|
266
|
+
"#{location}/extractions",
|
267
|
+
{ headers: { accept: header } }
|
268
|
+
).and_return(OAuth2::Response.new(ex_response))
|
269
|
+
expect(document.extractions).to be_a(Gini::Api::Document::Extractions)
|
270
|
+
expect(document.extractions(refresh: true)).to be_a(Gini::Api::Document::Extractions)
|
271
|
+
end
|
272
|
+
|
273
|
+
end
|
274
|
+
|
275
|
+
context 'with incubator == true' do
|
276
|
+
|
277
|
+
let(:incubator_header) { 'application/vnd.gini.incubator+json' }
|
278
|
+
|
279
|
+
it do
|
280
|
+
expect(api.token).to receive(:get).with(
|
281
|
+
"#{location}/extractions",
|
282
|
+
{ headers: { accept: incubator_header } }
|
283
|
+
).and_return(OAuth2::Response.new(ex_response))
|
284
|
+
expect(document.extractions(incubator: true)).to be_a(Gini::Api::Document::Extractions)
|
285
|
+
end
|
286
|
+
|
246
287
|
end
|
247
288
|
|
248
289
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gini-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Kerwin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2
|