Class: Gini::Api::Document::Extractions
- Inherits:
-
Object
- Object
- Gini::Api::Document::Extractions
- Defined in:
- lib/gini-api/document/extractions.rb
Overview
Contains document related extractions
Instance Attribute Summary collapse
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
-
#[](item) ⇒ String, Integer
Get filed value for given extraction key.
-
#initialize(api, location, incubator = false) ⇒ Extractions
constructor
Instantiate a new Gini::Api::Extractions object from hash.
-
#method_missing(m, *args, &block) ⇒ Hash, Nil
Create setter and getter dynamically with method_missing.
-
#submit_feedback(label, feedback) ⇒ Object
Submit feedback on extraction label.
-
#update ⇒ Object
Populate instance variables from fetched extractions.
Constructor Details
#initialize(api, location, incubator = false) ⇒ Extractions
Instantiate a new Gini::Api::Extractions object from hash
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gini-api/document/extractions.rb', line 16 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Hash, Nil
Create setter and getter dynamically with method_missing
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/gini-api/document/extractions.rb', line 108 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 |
Instance Attribute Details
#raw ⇒ Object (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
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gini-api/document/extractions.rb', line 64 def [](item) unless instance_variable_get("@#{item}") 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(label, feedback) ⇒ Object
Submit feedback on extraction label
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gini-api/document/extractions.rb', line 84 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 |
#update ⇒ Object
Populate instance variables from fetched extractions
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gini-api/document/extractions.rb', line 31 def update response = @api.request(:get, @location, @req_opts) unless response.status == 200 raise Gini::Api::DocumentError.new( "Failed to fetch extractions from #{@location}", response ) end # 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 instance_variable_set("@candidates", response.parsed[:candidates]) end |