Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #16 from gini/incubator_support
Browse files Browse the repository at this point in the history
Support incubator extractions. Closes #12
  • Loading branch information
dkerwin committed Jan 28, 2015
2 parents c0bdcc4 + a9157ff commit c2d80cb
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 12 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,23 @@ doc.extractions.undefinedLabel
# => nil
```

#### Incubator

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.

```ruby
doc.extractions(incubator=true).amountLiters
# => {:amountLiters=>{:entity=>"volume", :value=>"25.34:l", :box=>{:top=>1774.0, :left=>674.0, :width=>376.0, :height=>48.0, :page=>1}}
doc.extractions(incubator=true)[:amountLiters]
# => "25.34:l"
```

### Submitting feedback

```ruby
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 }
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
8 changes: 6 additions & 2 deletions lib/gini-api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def register_parser
OAuth2::Response.register_parser(:gini_xml, [version_header(:xml)[:accept]]) do |body|
MultiXml.parse(body) rescue body
end
OAuth2::Response.register_parser(:gini_incubator, [version_header(:json, :incubator)[:accept]]) do |body|
MultiJson.load(body, symbolize_keys: true) rescue body
end
end

# Acquire OAuth2 token and popolate @oauth (instance of Gini::Api::OAuth.new)
Expand Down Expand Up @@ -108,11 +111,12 @@ def logout
# Version accept header based on @api_version
#
# @param [Symbol, String] type Expected response type (:xml, :json)
# @param [Symbol, String] version API version (:v1, :incubator)
#
# @return [Hash] Return accept header or empty hash
#
def version_header(type = @api_type)
{ accept: "application/vnd.gini.#{@api_version}+#{type}" }
def version_header(type = @api_type, version = @api_version)
{ accept: "application/vnd.gini.#{version}+#{type}" }
end

# Request wrapper that sets URI and accept header
Expand Down
6 changes: 4 additions & 2 deletions lib/gini-api/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ def processed

# Initialize extractions from @_links and return Gini::Api::Extractions object
#
# @param [Boolean] incubator Return experimental extractions
#
# @return [Gini::Api::Document::Extractions] Return Gini::Api::Document::Extractions object for uploaded document
#
def extractions
@extractions ||= Gini::Api::Document::Extractions.new(@api, @_links[:extractions])
def extractions(incubator = false)
@extractions ||= Gini::Api::Document::Extractions.new(@api, @_links[:extractions], incubator)
end

# Initialize layout from @_links[:layout] and return Gini::Api::Layout object
Expand Down
24 changes: 20 additions & 4 deletions lib/gini-api/document/extractions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ class Document::Extractions
#
# @param [Gini::Api::Client] api Gini::Api::Client object
# @param [String] location Document URL
def initialize(api, location)
@api = api
@location = location
# @param [Boolean] incubator Return experimental extractions
#
def initialize(api, location, incubator = false)
@api = api
@location = location
@incubator = incubator
@req_opts = {}

if incubator
@req_opts = { headers: @api.version_header(:json, :incubator) }
end

update
end

# Populate instance variables from fetched extractions
#
def update
response = @api.request(:get, @location)
response = @api.request(:get, @location, @req_opts)

unless response.status == 200
raise Gini::Api::DocumentError.new(
Expand All @@ -33,6 +41,14 @@ def update
# Entire response
@raw = response.parsed

# raise exception if parsing failed
if response.parsed.nil?
raise Gini::Api::DocumentError.new(
"Failed to parse extractions from #{@location}",
response
)
end

response.parsed[:extractions].each do |k,v|
instance_variable_set("@#{k}", v)
end
Expand Down
32 changes: 32 additions & 0 deletions spec/gini-api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
include(:gini_xml)
end

it do
expect(OAuth2::Response::PARSERS.keys).to \
include(:gini_incubator)
end

end

describe '#login' do
Expand Down Expand Up @@ -140,6 +145,15 @@

end

context 'with incubator' do

it 'returns accept header with incubator version' do
expect(api.version_header(:json, :incubator)).to \
eql({ accept: 'application/vnd.gini.incubator+json' })
end

end

end

context 'being logged in' do
Expand Down Expand Up @@ -223,6 +237,24 @@

end

context 'return JSON on incubator request' do

let(:header) { 'application/vnd.gini.incubator+json' }
let(:body) {
{
a: 1,
b: 2
}.to_json
}

it do
expect(api.token).to \
receive(:get).and_return(OAuth2::Response.new(response))
expect(api.request(:get, '/dummy').parsed).to be_a Hash
end

end

context 'set custom accept header' do

let(:header) { 'application/octet-stream' }
Expand Down
45 changes: 42 additions & 3 deletions spec/gini-api/document/extraction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
)
end

let(:header) { 'application/vnd.gini.v1+json' }
let(:location) { 'https://api.gini.net/document/aaa-bbb-ccc/extractions' }
let(:incubator) { false }
let(:header) { 'application/vnd.gini.v1+json' }
let(:location) { 'https://api.gini.net/document/aaa-bbb-ccc/extractions' }
let(:response) do
double('Response',
status: 200,
Expand Down Expand Up @@ -68,7 +69,7 @@
)
end

subject(:extractions) { Gini::Api::Document::Extractions.new(api, location) }
subject(:extractions) { Gini::Api::Document::Extractions.new(api, location, incubator) }

it { should respond_to(:update) }
it { should respond_to(:[]) }
Expand Down Expand Up @@ -98,6 +99,44 @@

end

context 'failed to parse response' do

let(:response) do
double('Response',
status: 200,
headers: {
'content-type' => 'vnd/gini.not.supported+json'
},
env: {},
body: {}.to_json
)
end

it 'raises exception' do
expect { extractions.update }.to \
raise_error(Gini::Api::DocumentError, /Failed to parse extractions from #{location}/)
end

end

context 'with incubator=true' do

let(:extractions) { Gini::Api::Document::Extractions.new(api, location, incubator=true) }

it do
expect(api.token).to receive(:get).with(
location,
{
headers: {
accept: 'application/vnd.gini.incubator+json'
}
}
).and_return(OAuth2::Response.new(response))
expect(extractions.instance_variable_get(:@req_opts)).to eql({ headers: { accept: 'application/vnd.gini.incubator+json' } })
end

end

end

describe '#[]' do
Expand Down

0 comments on commit c2d80cb

Please sign in to comment.