ocrsdk 0.1.2 → 0.2.0

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.
data/README.md CHANGED
@@ -76,6 +76,34 @@ else
76
76
  end
77
77
  ```
78
78
 
79
+ Testing
80
+ -------
81
+
82
+ In order to make tests determenistic and fast you might want to mock all network interactions. For this purpose OCRSDK introduce Mock module built on top of [Webmock](https://github.com/bblimke/webmock) gem. It can be used with any testing environment including RSpec, Capybara, Cucumber. To mock all gem request you need to call on of those functions before running tests:
83
+
84
+ * `OCRSDK::Mock.success` - image was correctly submitted, promise will return `:completed` status and you will be able to retrieve a result;
85
+ * `OCRSDK::Mock.in_progress` - image was correctly submitted, promise will return `:in_progress` status for every request, which means you wouldn't be able to get the result;
86
+ * `OCRSDK::Mock.not_enough_credits` - submission of image would raise `OCRSDK::NotEnoughCredits` error.
87
+
88
+ For example you can mock ocrsdk in controller test like this:
89
+
90
+ ```ruby
91
+ # spec_helper.rb
92
+ require 'ocrsdk/mock'
93
+
94
+ # some_controller_spec.rb
95
+
96
+ require 'spec_helper'
97
+
98
+ describe SomeController do
99
+ before { OCRSDK::Mock.success }
100
+
101
+ describe "POST recognize" do
102
+ # ...
103
+ end
104
+ end
105
+ ```
106
+
79
107
  Copyright
80
108
  =========
81
109
  Copytright © 2012 Andrey Korzhuev. See LICENSE for details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -0,0 +1,46 @@
1
+ require 'webmock'
2
+
3
+ module OCRSDK::Mock
4
+ MOCKS_PATH = File.realpath(File.join(File.dirname(__FILE__), '..', '..', 'mocks'))
5
+
6
+ class << self
7
+ def success
8
+ stub_process_image response(:process_image, :success)
9
+ stub_get_task_status response(:get_task_status, :completed)
10
+ stub_result response(:result, :simple)
11
+ end
12
+
13
+ def in_progress
14
+ success
15
+ stub_get_task_status response(:get_task_status, :in_progress)
16
+ end
17
+
18
+ def not_enough_credits
19
+ success
20
+ stub_process_image response(:process_image, :not_enough_credits)
21
+ stub_get_task_status response(:get_task_status, :not_enough_credits)
22
+ end
23
+
24
+ def stub_process_image(response)
25
+ WebMock::API.stub_request(:post, /.*:.*@cloud.ocrsdk.com\/processImage/).to_return(body: response)
26
+ end
27
+
28
+ def stub_get_task_status(response)
29
+ WebMock::API.stub_request(:get, /.*:.*@cloud.ocrsdk.com\/getTaskStatus\?taskId=.*/).to_return(body: response)
30
+ end
31
+
32
+ def stub_result(response)
33
+ WebMock::API.stub_request(:get, 'http://cloud.ocrsdk.com/result_url').to_return(body: response)
34
+ end
35
+
36
+ def response(method, status)
37
+ path = File.join(MOCKS_PATH, method.to_s, "#{status.to_s}.xml")
38
+
39
+ unless File.exist? path
40
+ warn "No predefined xml response for #{method}/#{status} found. Path: #{path}"
41
+ end
42
+
43
+ File.new(path).read
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <response>
3
+ <task id="update-task-id"
4
+ registrationTime="2001-01-01T13:18:22Z"
5
+ statusChangeTime="2001-03-01T13:18:22Z"
6
+ status="Completed"
7
+ error="{An error message.}"
8
+ filesCount="10"
9
+ credits="10"
10
+ estimatedProcessingTime="3600"
11
+ resultUrl="http://cloud.ocrsdk.com/result_url"
12
+ description="My first OCR task"/>
13
+ <task/>
14
+ </response>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <response>
3
+ <task id="update-task-id"
4
+ registrationTime="2001-01-01T13:18:22Z"
5
+ statusChangeTime="2001-03-01T13:18:22Z"
6
+ status="ProcessingFailed"
7
+ error="{An error message.}"
8
+ filesCount="10"
9
+ credits="10"
10
+ estimatedProcessingTime="3600"
11
+ resultUrl="http://cloud.ocrsdk.com/result_url"
12
+ description="My first OCR task"/>
13
+ <task/>
14
+ </response>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <response>
3
+ <task id="update-task-id"
4
+ registrationTime="2001-01-01T13:18:22Z"
5
+ statusChangeTime="2001-02-01T13:18:22Z"
6
+ status="InProgress"
7
+ error="{An error message.}"
8
+ filesCount="10"
9
+ credits="10"
10
+ estimatedProcessingTime="3600"
11
+ resultUrl="http://cloud.ocrsdk.com/result_url"
12
+ description="My first OCR task"/>
13
+ <task/>
14
+ </response>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <response>
3
+ <task id="22345200-abe8-4f60-90c8-0d43c5f6c0f6"
4
+ registrationTime="2001-01-01T13:18:22Z"
5
+ statusChangeTime="2001-01-01T13:18:22Z"
6
+ status="NotEnoughCredits"
7
+ error="{An error message.}"
8
+ filesCount="10"
9
+ credits="0"
10
+ estimatedProcessingTime="3600"
11
+ resultUrl="http://cloud.ocrsdk.com/result_url"
12
+ description="My first OCR task"/>
13
+ <task/>
14
+ </response>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <response>
3
+ <task id="22345200-abe8-4f60-90c8-0d43c5f6c0f6"
4
+ registrationTime="2001-01-01T13:18:22Z"
5
+ statusChangeTime="2001-01-01T13:18:22Z"
6
+ status="Submitted"
7
+ error="{An error message.}"
8
+ filesCount="10"
9
+ credits="10"
10
+ estimatedProcessingTime="3600"
11
+ resultUrl="http://cloud.ocrsdk.com/result_url"
12
+ description="My first OCR task"/>
13
+ <task/>
14
+ </response>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2
+ <response>
3
+ <task id="22345200-abe8-4f60-90c8-0d43c5f6c0f6"
4
+ registrationTime="2001-01-01T13:18:22Z"
5
+ statusChangeTime="2001-01-01T13:18:22Z"
6
+ status="Submitted"
7
+ error="{An error message.}"
8
+ filesCount="10"
9
+ credits="10"
10
+ estimatedProcessingTime="3600"
11
+ resultUrl="http://cloud.ocrsdk.com/result_url"
12
+ description="My first OCR task"/>
13
+ <task/>
14
+ </response>
@@ -0,0 +1 @@
1
+ meow
@@ -0,0 +1 @@
1
+ meow
@@ -6,40 +6,31 @@ describe OCRSDK::Image do
6
6
  OCRSDK.setup do |config|
7
7
  config.application_id = 'app_id'
8
8
  config.password = 'pass'
9
- end
9
+ end
10
+ OCRSDK::Mock.success
10
11
  end
11
12
 
13
+ subject { OCRSDK::Image.new TestFiles.russian_jpg_path }
14
+
12
15
  describe ".as_text" do
13
- subject { OCRSDK::Image.new TestFiles.russian_jpg_path }
14
- before { mock_ocrsdk }
15
-
16
16
  it "should call api and return Promise" do
17
17
  subject.as_text([:russian]).should be_kind_of(OCRSDK::Promise)
18
18
  end
19
19
  end
20
20
 
21
21
  describe ".as_text_sync" do
22
- subject { OCRSDK::Image.new TestFiles.russian_jpg_path }
23
- before { mock_ocrsdk }
24
-
25
22
  it "should wait till Promise is done and return result" do
26
23
  subject.as_text_sync([:russian], 0).should == 'meow'
27
24
  end
28
25
  end
29
26
 
30
27
  describe ".as_pdf" do
31
- subject { OCRSDK::Image.new TestFiles.russian_jpg_path }
32
- before { mock_ocrsdk }
33
-
34
28
  it "should call api and return Promise" do
35
29
  subject.as_pdf([:russian]).should be_kind_of(OCRSDK::Promise)
36
30
  end
37
31
  end
38
32
 
39
33
  describe ".as_pdf_sync" do
40
- subject { OCRSDK::Image.new TestFiles.russian_jpg_path }
41
- before { mock_ocrsdk }
42
-
43
34
  it "should wait till Promise is done and return result if output file isn't specified" do
44
35
  subject.as_pdf_sync([:russian], nil, 0).should == 'meow'
45
36
  end
@@ -53,8 +44,6 @@ describe OCRSDK::Image do
53
44
  end
54
45
 
55
46
  describe ".api_process_image" do
56
- subject { OCRSDK::Image.new TestFiles.russian_jpg_path }
57
-
58
47
  it "should raise UnsupportedLanguage on unsupported language" do
59
48
  expect {
60
49
  subject.instance_eval { api_process_image TestFiles.russian_jpg_path, [:meow] }
@@ -0,0 +1,18 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe OCRSDK::Mock do
5
+
6
+ describe ".response" do
7
+ it "should read response from file" do
8
+ OCRSDK::Mock.response(:test, :test).should == 'meow'
9
+ end
10
+
11
+ it "should raise an exception if response is not found" do
12
+ expect {
13
+ OCRSDK::Mock.response(:test, :non_existant_file)
14
+ }.to raise_error(Errno::ENOENT)
15
+ end
16
+ end
17
+
18
+ end
@@ -11,7 +11,7 @@ describe OCRSDK::Promise do
11
11
 
12
12
  describe ".parse_response" do
13
13
  context "correct response" do
14
- subject { OCRSDK::Promise.new(nil).parse_response process_image_response }
14
+ subject { OCRSDK::Promise.new(nil).parse_response OCRSDK::Mock.response(:get_task_status, :submitted) }
15
15
 
16
16
  its(:task_id) { should == '22345200-abe8-4f60-90c8-0d43c5f6c0f6' }
17
17
  its(:status) { should == :submitted }
@@ -35,14 +35,14 @@ describe OCRSDK::Promise do
35
35
 
36
36
  it "should raise an OCRSDK::NotEnoughCredits error" do
37
37
  expect {
38
- subject.parse_response process_image_response_credits
38
+ subject.parse_response OCRSDK::Mock.response(:get_task_status, :not_enough_credits)
39
39
  }.to raise_error(OCRSDK::NotEnoughCredits)
40
40
  end
41
41
  end
42
42
  end
43
43
 
44
44
  describe "self.from_response" do
45
- subject { OCRSDK::Promise.from_response process_image_response }
45
+ subject { OCRSDK::Promise.from_response OCRSDK::Mock.response(:get_task_status, :submitted) }
46
46
 
47
47
  its(:task_id) { should == '22345200-abe8-4f60-90c8-0d43c5f6c0f6' }
48
48
  its(:status) { should == :submitted }
@@ -54,7 +54,7 @@ describe OCRSDK::Promise do
54
54
  describe ".update" do
55
55
  subject { OCRSDK::Promise.new 'update-task-id' }
56
56
  before do
57
- subject.stub(:api_update_status) { process_image_updated_response }
57
+ OCRSDK::Mock.in_progress
58
58
  subject.update
59
59
  end
60
60
 
@@ -85,21 +85,10 @@ describe OCRSDK::Promise do
85
85
 
86
86
  describe ".result" do
87
87
  context "processing completed without errors" do
88
- subject { OCRSDK::Promise.from_response process_image_completed_response }
88
+ before { OCRSDK::Mock.success }
89
+ subject { OCRSDK::Promise.from_response OCRSDK::Mock.response(:get_task_status, :completed) }
89
90
 
90
- it "should call api method" do
91
- subject.should_receive(:api_get_result).once
92
- subject.result
93
- end
94
-
95
- it "should get file with coorect url and return its contents" do
96
- RestClient.stub(:get) do |url|
97
- url.to_s.should == "http://cloud.ocrsdk.com/result_url"
98
- "meow"
99
- end
100
- RestClient.should_receive(:get).once
101
- subject.result.should == 'meow'
102
- end
91
+ its(:result) { should == 'meow' }
103
92
 
104
93
  it "should raise NetworkError in case getting file fails" do
105
94
  RestClient.stub(:get) {|url| raise RestClient::ExceptionWithResponse }
@@ -112,7 +101,7 @@ describe OCRSDK::Promise do
112
101
  end
113
102
 
114
103
  context "processing failed" do
115
- subject { OCRSDK::Promise.from_response process_image_failed_response }
104
+ subject { OCRSDK::Promise.from_response OCRSDK::Mock.response(:get_task_status, :failed) }
116
105
 
117
106
  it "should raise an ProcessingFailed" do
118
107
  expect {
@@ -124,7 +113,7 @@ describe OCRSDK::Promise do
124
113
 
125
114
  describe ".completed? and .failed?" do
126
115
  context "processed job" do
127
- subject { OCRSDK::Promise.from_response process_image_updated_response }
116
+ subject { OCRSDK::Promise.from_response OCRSDK::Mock.response(:get_task_status, :in_progress) }
128
117
 
129
118
  its(:processing?) { should be_true }
130
119
  its(:completed?) { should be_false }
@@ -132,7 +121,7 @@ describe OCRSDK::Promise do
132
121
  end
133
122
 
134
123
  context "completed job" do
135
- subject { OCRSDK::Promise.from_response process_image_completed_response }
124
+ subject { OCRSDK::Promise.from_response OCRSDK::Mock.response(:get_task_status, :completed) }
136
125
 
137
126
  its(:processing?) { should be_false }
138
127
  its(:completed?) { should be_true }
@@ -140,7 +129,7 @@ describe OCRSDK::Promise do
140
129
  end
141
130
 
142
131
  context "failed job" do
143
- subject { OCRSDK::Promise.from_response process_image_failed_response }
132
+ subject { OCRSDK::Promise.from_response OCRSDK::Mock.response(:get_task_status, :failed) }
144
133
 
145
134
  its(:processing?) { should be_false }
146
135
  its(:completed?) { should be_false }
@@ -149,13 +138,13 @@ describe OCRSDK::Promise do
149
138
  end
150
139
 
151
140
  describe ".wait" do
152
- subject { OCRSDK::Promise.from_response process_image_response }
141
+ subject { OCRSDK::Promise.from_response OCRSDK::Mock.response(:get_task_status, :in_progress) }
153
142
 
154
143
  it "should check the status as many times as needed waiting while ocr is completed" do
155
144
  called_once = false
156
145
  subject.stub(:update) do
157
146
  if called_once
158
- subject.parse_response process_image_completed_response
147
+ subject.parse_response OCRSDK::Mock.response(:get_task_status, :completed)
159
148
  else
160
149
  called_once = true
161
150
  end
data/spec/spec_helper.rb CHANGED
@@ -5,5 +5,5 @@ end
5
5
  require 'rspec'
6
6
  require 'webmock/rspec'
7
7
  require 'ocrsdk'
8
- require 'helpers/ocrsdk_helpers'
8
+ require 'ocrsdk/mock'
9
9
  require 'support/test_files'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocrsdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-11 00:00:00.000000000 Z
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -159,6 +159,7 @@ files:
159
159
  - lib/ocrsdk/document.rb
160
160
  - lib/ocrsdk/errors.rb
161
161
  - lib/ocrsdk/image.rb
162
+ - lib/ocrsdk/mock.rb
162
163
  - lib/ocrsdk/pdf.rb
163
164
  - lib/ocrsdk/promise.rb
164
165
  - lib/ocrsdk/verifiers.rb
@@ -166,6 +167,14 @@ files:
166
167
  - lib/ocrsdk/verifiers/language.rb
167
168
  - lib/ocrsdk/verifiers/profile.rb
168
169
  - lib/ocrsdk/verifiers/status.rb
170
+ - mocks/get_task_status/completed.xml
171
+ - mocks/get_task_status/failed.xml
172
+ - mocks/get_task_status/in_progress.xml
173
+ - mocks/get_task_status/not_enough_credits.xml
174
+ - mocks/get_task_status/submitted.xml
175
+ - mocks/process_image/success.xml
176
+ - mocks/result/simple.xml
177
+ - mocks/test/test.xml
169
178
  - ocrsdk.gemspec
170
179
  - spec/abstract_entity_spec.rb
171
180
  - spec/fixtures/files/lorem.complex.pdf
@@ -174,8 +183,8 @@ files:
174
183
  - spec/fixtures/files/recognizeable.pdf
175
184
  - spec/fixtures/files/russian.jpg
176
185
  - spec/fixtures/files/searchable.malformed.pdf
177
- - spec/helpers/ocrsdk_helpers.rb
178
186
  - spec/ocrsdk/image_spec.rb
187
+ - spec/ocrsdk/mock_spec.rb
179
188
  - spec/ocrsdk/pdf_spec.rb
180
189
  - spec/ocrsdk/promise_spec.rb
181
190
  - spec/ocrsdk/verifiers/format_spec.rb
@@ -219,8 +228,8 @@ test_files:
219
228
  - spec/fixtures/files/recognizeable.pdf
220
229
  - spec/fixtures/files/russian.jpg
221
230
  - spec/fixtures/files/searchable.malformed.pdf
222
- - spec/helpers/ocrsdk_helpers.rb
223
231
  - spec/ocrsdk/image_spec.rb
232
+ - spec/ocrsdk/mock_spec.rb
224
233
  - spec/ocrsdk/pdf_spec.rb
225
234
  - spec/ocrsdk/promise_spec.rb
226
235
  - spec/ocrsdk/verifiers/format_spec.rb
@@ -1,106 +0,0 @@
1
- module OCRSDKHelpers
2
-
3
- def process_image_response
4
- <<-XML
5
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
6
- <response>
7
- <task id="22345200-abe8-4f60-90c8-0d43c5f6c0f6"
8
- registrationTime="2001-01-01T13:18:22Z"
9
- statusChangeTime="2001-01-01T13:18:22Z"
10
- status="Submitted"
11
- error="{An error message.}"
12
- filesCount="10"
13
- credits="10"
14
- estimatedProcessingTime="3600"
15
- resultUrl="http://cloud.ocrsdk.com/result_url"
16
- description="My first OCR task"/>
17
- <task/>
18
- </response>
19
- XML
20
- end
21
-
22
- def process_image_response_credits
23
- <<-XML
24
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
25
- <response>
26
- <task id="22345200-abe8-4f60-90c8-0d43c5f6c0f6"
27
- registrationTime="2001-01-01T13:18:22Z"
28
- statusChangeTime="2001-01-01T13:18:22Z"
29
- status="NotEnoughCredits"
30
- error="{An error message.}"
31
- filesCount="10"
32
- credits="0"
33
- estimatedProcessingTime="3600"
34
- resultUrl="http://cloud.ocrsdk.com/result_url"
35
- description="My first OCR task"/>
36
- <task/>
37
- </response>
38
- XML
39
- end
40
-
41
- def process_image_updated_response
42
- <<-XML
43
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
44
- <response>
45
- <task id="update-task-id"
46
- registrationTime="2001-01-01T13:18:22Z"
47
- statusChangeTime="2001-02-01T13:18:22Z"
48
- status="InProgress"
49
- error="{An error message.}"
50
- filesCount="10"
51
- credits="10"
52
- estimatedProcessingTime="3600"
53
- resultUrl="http://cloud.ocrsdk.com/result_url"
54
- description="My first OCR task"/>
55
- <task/>
56
- </response>
57
- XML
58
- end
59
-
60
- def process_image_completed_response
61
- <<-XML
62
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
63
- <response>
64
- <task id="update-task-id"
65
- registrationTime="2001-01-01T13:18:22Z"
66
- statusChangeTime="2001-03-01T13:18:22Z"
67
- status="Completed"
68
- error="{An error message.}"
69
- filesCount="10"
70
- credits="10"
71
- estimatedProcessingTime="3600"
72
- resultUrl="http://cloud.ocrsdk.com/result_url"
73
- description="My first OCR task"/>
74
- <task/>
75
- </response>
76
- XML
77
- end
78
-
79
- def process_image_failed_response
80
- <<-XML
81
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
82
- <response>
83
- <task id="update-task-id"
84
- registrationTime="2001-01-01T13:18:22Z"
85
- statusChangeTime="2001-03-01T13:18:22Z"
86
- status="ProcessingFailed"
87
- error="{An error message.}"
88
- filesCount="10"
89
- credits="10"
90
- estimatedProcessingTime="3600"
91
- resultUrl="http://cloud.ocrsdk.com/result_url"
92
- description="My first OCR task"/>
93
- <task/>
94
- </response>
95
- XML
96
- end
97
-
98
- def mock_ocrsdk
99
- stub_request(:post, /.*:.*@cloud.ocrsdk.com\/processImage/).to_return(body: process_image_response)
100
- stub_request(:get, /.*:.*@cloud.ocrsdk.com\/getTaskStatus\?taskId=.*/).to_return(body: process_image_completed_response )
101
- stub_request(:get, 'http://cloud.ocrsdk.com/result_url').to_return(body: 'meow')
102
- end
103
-
104
- end
105
-
106
- RSpec.configuration.include OCRSDKHelpers