Class: Gini::Api::Document
- Inherits:
-
Object
- Object
- Gini::Api::Document
- Defined in:
- lib/gini-api/document.rb
Overview
Contains document related data from uploaded or fetched document
Defined Under Namespace
Classes: Extractions, Layout
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
Instance Method Summary collapse
-
#completed? ⇒ Boolean
Indicate if the document has been processed.
-
#extractions ⇒ Gini::Api::Document::Extractions
Initialize extractions from @_links and return Gini::Api::Extractions object.
-
#initialize(api, location, from_data = nil) ⇒ Document
constructor
Instantiate a new Gini::Api::Document object from URL.
-
#layout ⇒ Gini::Api::Document::Layout
Initialize layout from @_links and return Gini::Api::Layout object.
-
#pages ⇒ Object
Override @pages instance variable.
-
#poll(interval, &block) ⇒ Object
Poll document progress and return when state equals COMPLETED Known states are PENDING, COMPLETED and ERROR.
-
#processed ⇒ data
Get processed document.
-
#submit_feedback(label, value) ⇒ Object
Submit feedback on extraction label.
-
#successful? ⇒ Boolean
Was the document processed successfully?.
-
#update(from_data = nil) ⇒ Object
Fetch document resource and populate instance variables.
Constructor Details
#initialize(api, location, from_data = nil) ⇒ Document
Instantiate a new Gini::Api::Document object from URL
18 19 20 21 22 23 |
# File 'lib/gini-api/document.rb', line 18 def initialize(api, location, from_data = nil) @api = api @location = location update(from_data) end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
10 11 12 |
# File 'lib/gini-api/document.rb', line 10 def duration @duration end |
Instance Method Details
#completed? ⇒ Boolean
Indicate if the document has been processed
74 75 76 |
# File 'lib/gini-api/document.rb', line 74 def completed? @progress != 'PENDING' end |
#extractions ⇒ Gini::Api::Document::Extractions
Initialize extractions from @_links and return Gini::Api::Extractions object
109 110 111 |
# File 'lib/gini-api/document.rb', line 109 def extractions @extractions ||= Gini::Api::Document::Extractions.new(@api, @_links[:extractions]) end |
#layout ⇒ Gini::Api::Document::Layout
Initialize layout from @_links and return Gini::Api::Layout object
117 118 119 |
# File 'lib/gini-api/document.rb', line 117 def layout @layout ||= Gini::Api::Document::Layout.new(@api, @_links[:layout]) end |
#pages ⇒ Object
Override @pages instance variable. Removes key :pageNumber, key :images and starts by index 0. Page 1 becomes index 0
124 125 126 |
# File 'lib/gini-api/document.rb', line 124 def pages @pages.map { |page| page[:images] } end |
#poll(interval, &block) ⇒ Object
Poll document progress and return when state equals COMPLETED Known states are PENDING, COMPLETED and ERROR
60 61 62 63 64 65 66 67 68 |
# File 'lib/gini-api/document.rb', line 60 def poll(interval, &block) timers = Timers::Group.new timers.every(interval) do update timers.cancel if @progress =~ /(COMPLETED|ERROR)/ yield self if block_given? end nil end |
#processed ⇒ data
Get processed document
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/gini-api/document.rb', line 90 def processed response = @api.request( :get, @_links[:processed], headers: { accept: 'application/octet-stream' } ) unless response.status == 200 raise Gini::Api::DocumentError.new( "Failed to fetch processed document (code=#{response.status})", response ) end response.body end |
#submit_feedback(label, value) ⇒ Object
Submit feedback on extraction label
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/gini-api/document.rb', line 133 def submit_feedback(label, value) unless extractions.send(label.to_sym) raise Gini::Api::DocumentError.new("Unknown label #{label}: Not found") end response = @api.request( :put, "#{@_links[:extractions]}/#{label}", headers: { 'content-type' => @api.version_header[:accept] }, body: { value: value }.to_json ) unless response.status == 204 raise Gini::Api::DocumentError.new( "Failed to submit feedback for label #{label} (code=#{response.status})", response ) end end |
#successful? ⇒ Boolean
Was the document processed successfully?
82 83 84 |
# File 'lib/gini-api/document.rb', line 82 def successful? @progress == 'COMPLETED' end |
#update(from_data = nil) ⇒ Object
Fetch document resource and populate instance variables
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/gini-api/document.rb', line 29 def update(from_data = nil) data = {} if from_data.nil? response = @api.request(:get, @location) unless response.status == 200 raise Gini::Api::DocumentError.new( "Failed to fetch document data (code=#{response.status})", response ) end data = response.parsed else data = from_data end data.each do |k, v| instance_variable_set("@#{k}", v) # We skip pages as it's rewritted by method pages() next if k == :pages self.class.send(:attr_reader, k) end end |