Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
gini-api (0.9.8)
gini-api (0.9.9)
logger
oauth2

Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,18 @@ doc.extractions.candidates[:dates]
# => Array of all found candidates
doc.extractions.raw
# => {:extractions=>{...
doc.extractions.undefinedLabel
# => nil
```

### Submitting feedback

```ruby
doc.submit_feedback(:bic, 'XXXXXXXX')
# => nil
doc.submit_feedback(:unknownlabel, 'XXXXXXX')
doc.extractions.bic = 'XXXXXXXX'
# => 'XXXXXXXX'
doc.extractions.bic = { value: 'XXXXXXXX', :box=>{:top=>2176.0, :left=>2000.0, :width=>173.0, :height=>50.0, :page=>1 }
# => { value: 'XXXXXXXX', box: { top: 2176.0, left: 2000.0, width: 173.0, height: 50.0, page: 1 }
doc.extractions.unknownLabel = 'XXXXXXXX'
# => raises Gini::Api::DocumentError
```

Expand Down
1 change: 1 addition & 0 deletions lib/gini-api/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def pages

# Submit feedback on extraction label
#
# @deprecated Use 'doc.extractions.LABEL = VALUE' instead. Will be removed in next version
# @param [String] label Extraction label to submit feedback on
# @param [String] value The new value for the given label
#
Expand Down
59 changes: 56 additions & 3 deletions lib/gini-api/document/extractions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ def update

response.parsed[:extractions].each do |k,v|
instance_variable_set("@#{k}", v)
self.class.send(:attr_reader, k)
end

instance_variable_set("@candidates", response.parsed[:candidates])
self.class.send(:attr_reader, :candidates)
end

# Get filed value for given extraction key
Expand All @@ -49,11 +47,66 @@ def update
#
def [](item)
unless instance_variable_get("@#{item}")
raise Gini::Api::DocumentError.new("Invalid extraction key #{item}: Not found")
raise Gini::Api::DocumentError.new("Invalid extraction key '#{item}': Not found")
end

# method_missing requires some additional checks
label = instance_variable_get("@#{item}")

unless label.is_a? Hash and label.has_key? :value
raise Gini::Api::DocumentError.new("Extraction key '#{item}' has no :value defined")
end

instance_variable_get("@#{item}")[:value]
end

# Submit feedback on extraction label
#
# @param [String] label Extraction label to submit feedback on
# @param [Hash] feedback Hash containing at least key :value (:box is optional)
#
def submit_feedback(label, feedback)
response = @api.request(
:put,
"#{@location}/#{label}",
headers: { 'content-type' => @api.version_header[:accept] },
body: feedback.to_json
)
rescue Gini::Api::RequestError => e
if e.api_status == 422
raise Gini::Api::DocumentError.new(
"Failed to submit feedback for label '#{label}' (code=#{e.api_status}, msg=#{e.api_response.body})",
response
)
end
raise
end

# Create setter and getter dynamically with method_missing
#
# @param [Symbol] m method name
# @param [Array] args method arguments
# @param [Block] block Block passed to the missing method
# @return [Hash, Nil] Return extraction hash or nil
#
def method_missing(m, *args, &block)
m_name = m.to_s
label = m_name.split('=')[0]

if m_name.end_with? '='
# setter method. Set instance variable and submit feedback
if args[0].is_a? Hash
feedback = args[0]
else
feedback = { value: args[0] }
end
instance_variable_set("@#{label}", feedback)
submit_feedback(label, feedback)
else
# getter. return instance variable or nil
instance_variable_get("@#{label}")
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/gini-api/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Gini
module Api
# Package version
VERSION = "0.9.8"
VERSION = "0.9.9"
end
end
106 changes: 103 additions & 3 deletions spec/gini-api/document/extraction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
entity: 'date',
value: '2012-06-20',
candidates: 'dates'
}
},
invalid: {
this_is: 'wrong'
},
},
candidates: {
dates: [
Expand Down Expand Up @@ -99,11 +102,20 @@

describe '#[]' do

context 'with invalid key' do
context 'with missing key' do

it 'raises exception' do
expect { extractions[:unknown] }.to \
raise_error(Gini::Api::DocumentError, /Invalid extraction key unknown/)
raise_error(Gini::Api::DocumentError, /Invalid extraction key 'unknown'/)
end

end

context 'with missing :value in response' do

it 'raises exception' do
expect { extractions[:invalid] }.to \
raise_error(Gini::Api::DocumentError, /Extraction key 'invalid' has no :value defined/)
end

end
Expand All @@ -118,4 +130,92 @@

end

describe '#method_missing' do

context 'with unknown extraction' do

context 'and only value' do

it 'will set instance variable to new hash' do
expect(extractions).to receive(:instance_variable_set).with('@test', {value: :test})
expect(extractions).to receive(:submit_feedback).with('test', {:value=>:test})
extractions.test = :test
end

end

context 'and hash' do

it 'will set instance variable to supplied hash' do
expect(extractions).to receive(:instance_variable_set).with('@test', {value: 'test', box: {}})
expect(extractions).to receive(:submit_feedback).with('test', {value: 'test', box: {}})
extractions.test = {value: 'test', box: {} }
end

end

end

end

describe '#submit_feedback' do

context 'with valid label' do

before do
allow(api.token).to receive(:put).with(
"#{location}/test",
{
headers: { 'content-type' => header },
body: { value: 'Johnny Bravo' }.to_json
}
).and_return(OAuth2::Response.new(double('Response', status: 204)))
end

it 'succeeds' do
expect(extractions.submit_feedback(:test, {value: 'Johnny Bravo'})).to be_a(OAuth2::Response)
end

end

context 'with invalid label (http code 422)' do

before do
allow(api.token).to receive(:put).with(
"#{location}/test",
{
headers: { 'content-type' => header },
body: { value: 'Johnny Bravo' }.to_json
}
).and_raise(Gini::Api::RequestError.new('dummy', double('xxx', status: 422, env: {}, body: {})))
end

it 'raises Gini::Api::DocumentError' do
expect{extractions.submit_feedback(:test, {value: 'Johnny Bravo'})}.to \
raise_error(Gini::Api::DocumentError, /Failed to submit feedback for label/)
end

end

context 'with undefined error' do

before do
allow(api.token).to receive(:put).with(
"#{location}/test",
{
headers: { 'content-type' => header },
body: { value: 'Johnny Bravo' }.to_json
}
).and_raise(Gini::Api::RequestError.new('dummy', double('xxx', status: 500, env: {}, body: {})))
end

it 'raises Gini::Api::RequestError' do
expect{extractions.submit_feedback(:test, {value: 'Johnny Bravo'})}.to \
raise_error(Gini::Api::RequestError)
end

end

end

end