Class: Gini::Api::Document::Extractions

Inherits:
Object
  • Object
show all
Defined in:
lib/gini-api/document/extractions.rb

Overview

Contains document related extractions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api, location) ⇒ Extractions

Instantiate a new Gini::Api::Extractions object from hash

Parameters:

  • api (Gini::Api::Client)

    Gini::Api::Client object

  • location (String)

    Document URL



14
15
16
17
18
19
# File 'lib/gini-api/document/extractions.rb', line 14

def initialize(api, location)
  @api      = api
  @location = location

  update
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



8
9
10
# File 'lib/gini-api/document/extractions.rb', line 8

def raw
  @raw
end

Instance Method Details

#[](item) ⇒ String, Integer

Get filed value for given extraction key

Parameters:

  • item (String)

    The extractions item to get the value of

Returns:

  • (String, Integer)

    Returns the value from extractions hash



50
51
52
53
54
55
56
# File 'lib/gini-api/document/extractions.rb', line 50

def [](item)
  unless instance_variable_get("@#{item}")
    raise Gini::Api::DocumentError.new("Invalid extraction key #{item}: Not found")
  end

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

#updateObject

Populate instance variables from fetched extractions



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gini-api/document/extractions.rb', line 23

def update
  response = @api.request(:get, @location)

  unless response.status == 200
    raise Gini::Api::DocumentError.new(
      "Failed to fetch extractions from #{@location}",
      response
    )
  end

  # Entire response
  @raw = response.parsed

  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