gini-api 0.9.8 → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80c9d8ad0c0fc68dbbc20a990cb40e3e9f60cec7
4
- data.tar.gz: 3f8a4c2cfd1f2e6c7064be4c0faa36747f2c7084
3
+ metadata.gz: f482221a91704b834f741e93bc66ed8d387f673b
4
+ data.tar.gz: 7cabfa0aa95303038767caf0058ac02e6684d9cc
5
5
  SHA512:
6
- metadata.gz: 9f1b0d52409011c3edc7499324032ac25f9fed96d2df59d3fcf8ab7d0d4ac0a418e5e018cc706f62a61adc1c54243e77e5e90d0e879cb7e4193b9f77a461f130
7
- data.tar.gz: b47646a4ee76a5a0d48c9012f6c80e40b1bee32337beaeeaaf7fb31ded6923158f61a67b72dda0b9679d91c2ab3a970f2bff0873be5ebf144b47dca4cb7817ea
6
+ metadata.gz: 0819422ce704bd75d4ef24b46ee3566a980453e79819f541312cd6c08a49477f5178c2e7da36eda92c9c541b6dd4b2370ca2e57013c3974ae45477bf7d385c4e
7
+ data.tar.gz: b068d3ef18cf8daa7c9514c85c0fd69004e83f65c0f84f6b8bd4189a0589cfcf17b43ce7ab53366ffec32c201eee5b41d979343a9d767e88b4790a8f13144321
data/.gitignore CHANGED
@@ -4,4 +4,3 @@ pkg
4
4
  vendor
5
5
  spec/reports
6
6
  .yardoc
7
- Gemfile.lock
data/Gemfile.lock ADDED
@@ -0,0 +1,133 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gini-api (0.9.9)
5
+ logger
6
+ oauth2
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.3.6)
12
+ builder (3.2.2)
13
+ celluloid (0.16.0)
14
+ timers (~> 4.0.0)
15
+ ci_reporter (2.0.0)
16
+ builder (>= 2.1.2)
17
+ ci_reporter_rspec (1.0.0)
18
+ ci_reporter (~> 2.0)
19
+ rspec (>= 2.14, < 4)
20
+ coderay (1.1.0)
21
+ coveralls (0.7.2)
22
+ multi_json (~> 1.3)
23
+ rest-client (= 1.6.7)
24
+ simplecov (>= 0.7)
25
+ term-ansicolor (= 1.2.2)
26
+ thor (= 0.18.1)
27
+ crack (0.4.2)
28
+ safe_yaml (~> 1.0.0)
29
+ diff-lcs (1.2.5)
30
+ docile (1.1.5)
31
+ faraday (0.9.1)
32
+ multipart-post (>= 1.2, < 3)
33
+ ffi (1.9.6)
34
+ formatador (0.2.5)
35
+ guard (2.11.1)
36
+ formatador (>= 0.2.4)
37
+ listen (~> 2.7)
38
+ lumberjack (~> 1.0)
39
+ nenv (~> 0.1)
40
+ notiffany (~> 0.0)
41
+ pry (>= 0.9.12)
42
+ shellany (~> 0.0)
43
+ thor (>= 0.18.1)
44
+ guard-compat (1.2.0)
45
+ guard-rspec (4.5.0)
46
+ guard (~> 2.1)
47
+ guard-compat (~> 1.1)
48
+ rspec (>= 2.99.0, < 4.0)
49
+ hitimes (1.2.2)
50
+ jwt (1.2.0)
51
+ listen (2.8.5)
52
+ celluloid (>= 0.15.2)
53
+ rb-fsevent (>= 0.9.3)
54
+ rb-inotify (>= 0.9)
55
+ logger (1.2.8)
56
+ lumberjack (1.0.9)
57
+ method_source (0.8.2)
58
+ mime-types (2.4.3)
59
+ multi_json (1.10.1)
60
+ multi_xml (0.5.5)
61
+ multipart-post (2.0.0)
62
+ nenv (0.1.1)
63
+ notiffany (0.0.2)
64
+ nenv (~> 0.1)
65
+ shellany (~> 0.0)
66
+ oauth2 (1.0.0)
67
+ faraday (>= 0.8, < 0.10)
68
+ jwt (~> 1.0)
69
+ multi_json (~> 1.3)
70
+ multi_xml (~> 0.5)
71
+ rack (~> 1.2)
72
+ pry (0.10.1)
73
+ coderay (~> 1.1.0)
74
+ method_source (~> 0.8.1)
75
+ slop (~> 3.4)
76
+ rack (1.6.0)
77
+ rake (10.4.2)
78
+ rb-fsevent (0.9.4)
79
+ rb-inotify (0.9.5)
80
+ ffi (>= 0.5.0)
81
+ redcarpet (3.2.2)
82
+ rest-client (1.6.7)
83
+ mime-types (>= 1.16)
84
+ rspec (3.1.0)
85
+ rspec-core (~> 3.1.0)
86
+ rspec-expectations (~> 3.1.0)
87
+ rspec-mocks (~> 3.1.0)
88
+ rspec-core (3.1.7)
89
+ rspec-support (~> 3.1.0)
90
+ rspec-expectations (3.1.2)
91
+ diff-lcs (>= 1.2.0, < 2.0)
92
+ rspec-support (~> 3.1.0)
93
+ rspec-mocks (3.1.3)
94
+ rspec-support (~> 3.1.0)
95
+ rspec-support (3.1.2)
96
+ safe_yaml (1.0.4)
97
+ shellany (0.0.1)
98
+ simplecov (0.9.1)
99
+ docile (~> 1.1.0)
100
+ multi_json (~> 1.0)
101
+ simplecov-html (~> 0.8.0)
102
+ simplecov-html (0.8.0)
103
+ simplecov-rcov (0.2.3)
104
+ simplecov (>= 0.4.1)
105
+ slop (3.6.0)
106
+ term-ansicolor (1.2.2)
107
+ tins (~> 0.8)
108
+ thor (0.18.1)
109
+ timers (4.0.1)
110
+ hitimes
111
+ tins (0.13.2)
112
+ webmock (1.20.4)
113
+ addressable (>= 2.3.6)
114
+ crack (>= 0.3.2)
115
+ yard (0.8.7.6)
116
+
117
+ PLATFORMS
118
+ ruby
119
+
120
+ DEPENDENCIES
121
+ bundler
122
+ ci_reporter_rspec
123
+ coveralls
124
+ gini-api!
125
+ guard-rspec
126
+ pry
127
+ rake
128
+ redcarpet
129
+ rspec
130
+ simplecov
131
+ simplecov-rcov
132
+ webmock
133
+ yard
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- <img src="https://www.gini.net/wp-content/uploads/2014/02/Gini-API_logo.svg" width="222" alt="Gini API logo" />
1
+ <img src="https://www.gini.net/assets/gini_api_logo.svg" width="222" alt="Gini API logo" />
2
2
 
3
3
  # Gini API Ruby client
4
4
 
@@ -144,14 +144,18 @@ doc.extractions.candidates[:dates]
144
144
  # => Array of all found candidates
145
145
  doc.extractions.raw
146
146
  # => {:extractions=>{...
147
+ doc.extractions.undefinedLabel
148
+ # => nil
147
149
  ```
148
150
 
149
151
  ### Submitting feedback
150
152
 
151
153
  ```ruby
152
- doc.submit_feedback(:bic, 'XXXXXXXX')
153
- # => nil
154
- doc.submit_feedback(:unknownlabel, 'XXXXXXX')
154
+ doc.extractions.bic = 'XXXXXXXX'
155
+ # => 'XXXXXXXX'
156
+ doc.extractions.bic = { value: 'XXXXXXXX', :box=>{:top=>2176.0, :left=>2000.0, :width=>173.0, :height=>50.0, :page=>1 }
157
+ # => { value: 'XXXXXXXX', box: { top: 2176.0, left: 2000.0, width: 173.0, height: 50.0, page: 1 }
158
+ doc.extractions.unknownLabel = 'XXXXXXXX'
155
159
  # => raises Gini::Api::DocumentError
156
160
  ```
157
161
 
@@ -35,11 +35,9 @@ module Gini
35
35
 
36
36
  response.parsed[:extractions].each do |k,v|
37
37
  instance_variable_set("@#{k}", v)
38
- self.class.send(:attr_reader, k)
39
38
  end
40
39
 
41
40
  instance_variable_set("@candidates", response.parsed[:candidates])
42
- self.class.send(:attr_reader, :candidates)
43
41
  end
44
42
 
45
43
  # Get filed value for given extraction key
@@ -49,11 +47,66 @@ module Gini
49
47
  #
50
48
  def [](item)
51
49
  unless instance_variable_get("@#{item}")
52
- raise Gini::Api::DocumentError.new("Invalid extraction key #{item}: Not found")
50
+ raise Gini::Api::DocumentError.new("Invalid extraction key '#{item}': Not found")
51
+ end
52
+
53
+ # method_missing requires some additional checks
54
+ label = instance_variable_get("@#{item}")
55
+
56
+ unless label.is_a? Hash and label.has_key? :value
57
+ raise Gini::Api::DocumentError.new("Extraction key '#{item}' has no :value defined")
53
58
  end
54
59
 
55
60
  instance_variable_get("@#{item}")[:value]
56
61
  end
62
+
63
+ # Submit feedback on extraction label
64
+ #
65
+ # @param [String] label Extraction label to submit feedback on
66
+ # @param [Hash] feedback Hash containing at least key :value (:box is optional)
67
+ #
68
+ def submit_feedback(label, feedback)
69
+ response = @api.request(
70
+ :put,
71
+ "#{@location}/#{label}",
72
+ headers: { 'content-type' => @api.version_header[:accept] },
73
+ body: feedback.to_json
74
+ )
75
+ rescue Gini::Api::RequestError => e
76
+ if e.api_status == 422
77
+ raise Gini::Api::DocumentError.new(
78
+ "Failed to submit feedback for label '#{label}' (code=#{e.api_status}, msg=#{e.api_response.body})",
79
+ response
80
+ )
81
+ end
82
+ raise
83
+ end
84
+
85
+ # Create setter and getter dynamically with method_missing
86
+ #
87
+ # @param [Symbol] m method name
88
+ # @param [Array] args method arguments
89
+ # @param [Block] block Block passed to the missing method
90
+ # @return [Hash, Nil] Return extraction hash or nil
91
+ #
92
+ def method_missing(m, *args, &block)
93
+ m_name = m.to_s
94
+ label = m_name.split('=')[0]
95
+
96
+ if m_name.end_with? '='
97
+ # setter method. Set instance variable and submit feedback
98
+ if args[0].is_a? Hash
99
+ feedback = args[0]
100
+ else
101
+ feedback = { value: args[0] }
102
+ end
103
+ instance_variable_set("@#{label}", feedback)
104
+ submit_feedback(label, feedback)
105
+ else
106
+ # getter. return instance variable or nil
107
+ instance_variable_get("@#{label}")
108
+ end
109
+ end
57
110
  end
58
111
  end
59
112
  end
@@ -124,6 +124,7 @@ module Gini
124
124
 
125
125
  # Submit feedback on extraction label
126
126
  #
127
+ # @deprecated Use 'doc.extractions.LABEL = VALUE' instead. Will be removed in next version
127
128
  # @param [String] label Extraction label to submit feedback on
128
129
  # @param [String] value The new value for the given label
129
130
  #
@@ -1,6 +1,6 @@
1
1
  module Gini
2
2
  module Api
3
3
  # Package version
4
- VERSION = "0.9.8"
4
+ VERSION = "0.9.9"
5
5
  end
6
6
  end
@@ -267,7 +267,7 @@ describe Gini::Api::Client do
267
267
  headers: {
268
268
  location: 'LOC'
269
269
  },
270
- body: {}
270
+ body: '{}'.to_json
271
271
  )
272
272
  end
273
273
 
@@ -47,7 +47,10 @@ describe Gini::Api::Document::Extractions do
47
47
  entity: 'date',
48
48
  value: '2012-06-20',
49
49
  candidates: 'dates'
50
- }
50
+ },
51
+ invalid: {
52
+ this_is: 'wrong'
53
+ },
51
54
  },
52
55
  candidates: {
53
56
  dates: [
@@ -99,11 +102,20 @@ describe Gini::Api::Document::Extractions do
99
102
 
100
103
  describe '#[]' do
101
104
 
102
- context 'with invalid key' do
105
+ context 'with missing key' do
103
106
 
104
107
  it 'raises exception' do
105
108
  expect { extractions[:unknown] }.to \
106
- raise_error(Gini::Api::DocumentError, /Invalid extraction key unknown/)
109
+ raise_error(Gini::Api::DocumentError, /Invalid extraction key 'unknown'/)
110
+ end
111
+
112
+ end
113
+
114
+ context 'with missing :value in response' do
115
+
116
+ it 'raises exception' do
117
+ expect { extractions[:invalid] }.to \
118
+ raise_error(Gini::Api::DocumentError, /Extraction key 'invalid' has no :value defined/)
107
119
  end
108
120
 
109
121
  end
@@ -118,4 +130,92 @@ describe Gini::Api::Document::Extractions do
118
130
 
119
131
  end
120
132
 
133
+ describe '#method_missing' do
134
+
135
+ context 'with unknown extraction' do
136
+
137
+ context 'and only value' do
138
+
139
+ it 'will set instance variable to new hash' do
140
+ expect(extractions).to receive(:instance_variable_set).with('@test', {value: :test})
141
+ expect(extractions).to receive(:submit_feedback).with('test', {:value=>:test})
142
+ extractions.test = :test
143
+ end
144
+
145
+ end
146
+
147
+ context 'and hash' do
148
+
149
+ it 'will set instance variable to supplied hash' do
150
+ expect(extractions).to receive(:instance_variable_set).with('@test', {value: 'test', box: {}})
151
+ expect(extractions).to receive(:submit_feedback).with('test', {value: 'test', box: {}})
152
+ extractions.test = {value: 'test', box: {} }
153
+ end
154
+
155
+ end
156
+
157
+ end
158
+
159
+ end
160
+
161
+ describe '#submit_feedback' do
162
+
163
+ context 'with valid label' do
164
+
165
+ before do
166
+ allow(api.token).to receive(:put).with(
167
+ "#{location}/test",
168
+ {
169
+ headers: { 'content-type' => header },
170
+ body: { value: 'Johnny Bravo' }.to_json
171
+ }
172
+ ).and_return(OAuth2::Response.new(double('Response', status: 204)))
173
+ end
174
+
175
+ it 'succeeds' do
176
+ expect(extractions.submit_feedback(:test, {value: 'Johnny Bravo'})).to be_a(OAuth2::Response)
177
+ end
178
+
179
+ end
180
+
181
+ context 'with invalid label (http code 422)' do
182
+
183
+ before do
184
+ allow(api.token).to receive(:put).with(
185
+ "#{location}/test",
186
+ {
187
+ headers: { 'content-type' => header },
188
+ body: { value: 'Johnny Bravo' }.to_json
189
+ }
190
+ ).and_raise(Gini::Api::RequestError.new('dummy', double('xxx', status: 422, env: {}, body: {})))
191
+ end
192
+
193
+ it 'raises Gini::Api::DocumentError' do
194
+ expect{extractions.submit_feedback(:test, {value: 'Johnny Bravo'})}.to \
195
+ raise_error(Gini::Api::DocumentError, /Failed to submit feedback for label/)
196
+ end
197
+
198
+ end
199
+
200
+ context 'with undefined error' do
201
+
202
+ before do
203
+ allow(api.token).to receive(:put).with(
204
+ "#{location}/test",
205
+ {
206
+ headers: { 'content-type' => header },
207
+ body: { value: 'Johnny Bravo' }.to_json
208
+ }
209
+ ).and_raise(Gini::Api::RequestError.new('dummy', double('xxx', status: 500, env: {}, body: {})))
210
+ end
211
+
212
+ it 'raises Gini::Api::RequestError' do
213
+ expect{extractions.submit_feedback(:test, {value: 'Johnny Bravo'})}.to \
214
+ raise_error(Gini::Api::RequestError)
215
+ end
216
+
217
+ end
218
+
219
+ end
220
+
121
221
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gini-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.8
4
+ version: 0.9.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Kerwin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-22 00:00:00.000000000 Z
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -217,6 +217,7 @@ files:
217
217
  - ".rspec"
218
218
  - ".travis.yml"
219
219
  - Gemfile
220
+ - Gemfile.lock
220
221
  - Guardfile
221
222
  - README.md
222
223
  - Rakefile