ocrsdk 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -25,6 +25,14 @@ Configuration
25
25
  OCRSDK.setup do |config|
26
26
  config.application_id = '99bottlesofbeer'
27
27
  config.password = '98bottlesofbeer'
28
+
29
+ # How much time in seconds wait between requests
30
+ config.default_poll_time = 3 # default
31
+
32
+ # How many times retry before rendering request as failed
33
+ config.number_or_retries = 3 # default
34
+ # How much time wait before retries
35
+ config.retry_wait_time = 3 # default
28
36
  end
29
37
  ```
30
38
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -1,6 +1,7 @@
1
1
  require 'cgi'
2
2
  require 'uri'
3
3
  require 'nokogiri'
4
+ require 'retryable'
4
5
  require 'pdf-reader'
5
6
  require 'rest-client'
6
7
  require 'active_support/inflector'
@@ -11,14 +12,31 @@ require 'active_support/configurable'
11
12
  module OCRSDK
12
13
  include ActiveSupport::Configurable
13
14
 
14
- DEFAULT_POLL_TIME = 3
15
- SERVICE_URL = 'cloud.ocrsdk.com'
16
-
17
15
  def self.setup
18
16
  yield config
19
17
  end
20
18
  end
21
19
 
20
+ # Default configuration values
21
+ OCRSDK.setup do |config|
22
+ # These two should be set by user
23
+ config.application_id = nil
24
+ config.password = nil
25
+
26
+ # Generally this is not the thing you would want to change
27
+ # but in some rare cases like access to test servers
28
+ # it might be useful
29
+ config.service_url = 'cloud.ocrsdk.com'
30
+
31
+ # How much time in seconds wait between requests
32
+ config.default_poll_time = 3 # seconds
33
+
34
+ # How many times retry before rendering request as failed
35
+ config.number_or_retries = 3 # times
36
+ # How much time wait before retries
37
+ config.retry_wait_time = 3 # seconds
38
+ end
39
+
22
40
  require 'ocrsdk/errors'
23
41
  require 'ocrsdk/verifiers'
24
42
  require 'ocrsdk/abstract_entity'
@@ -6,6 +6,6 @@ class OCRSDK::AbstractEntity
6
6
  private
7
7
 
8
8
  def prepare_url(app_id, pass)
9
- URI("http://#{CGI.escape app_id}:#{CGI.escape pass}@#{OCRSDK::SERVICE_URL}")
9
+ URI("http://#{CGI.escape app_id}:#{CGI.escape pass}@#{OCRSDK.config.service_url}")
10
10
  end
11
11
  end
@@ -14,7 +14,7 @@ class OCRSDK::Image < OCRSDK::AbstractEntity
14
14
  OCRSDK::Promise.from_response xml_string
15
15
  end
16
16
 
17
- def as_text_sync(languages, wait_interval=OCRSDK::DEFAULT_POLL_TIME)
17
+ def as_text_sync(languages, wait_interval=OCRSDK.config.default_poll_time)
18
18
  as_text(languages).wait(wait_interval).result.force_encoding('utf-8')
19
19
  end
20
20
 
@@ -24,7 +24,7 @@ class OCRSDK::Image < OCRSDK::AbstractEntity
24
24
  OCRSDK::Promise.from_response xml_string
25
25
  end
26
26
 
27
- def as_pdf_sync(languages, out_path=nil, wait_interval=OCRSDK::DEFAULT_POLL_TIME)
27
+ def as_pdf_sync(languages, out_path=nil, wait_interval=OCRSDK.config.default_poll_time)
28
28
  result = as_pdf(languages).wait(wait_interval).result
29
29
 
30
30
  if out_path.nil?
@@ -1,3 +1,4 @@
1
+ require 'ocrsdk'
1
2
  require 'webmock'
2
3
 
3
4
  module OCRSDK::Mock
@@ -36,8 +36,10 @@ class OCRSDK::Promise < OCRSDK::AbstractEntity
36
36
  self
37
37
  end
38
38
 
39
- def update
40
- parse_response api_update_status
39
+ def update(retry_sleep=OCRSDK.config.retry_wait_time)
40
+ retryable tries: OCRSDK.config.number_or_retries, on: OCRSDK::NetworkError, sleep: retry_sleep do
41
+ parse_response api_update_status
42
+ end
41
43
  end
42
44
 
43
45
  def completed?
@@ -52,12 +54,14 @@ class OCRSDK::Promise < OCRSDK::AbstractEntity
52
54
  [:submitted, :queued, :in_progress].include? @status
53
55
  end
54
56
 
55
- def result
57
+ def result(retry_sleep=OCRSDK.config.retry_wait_time)
56
58
  raise OCRSDK::ProcessingFailed if failed?
57
- api_get_result
59
+ retryable tries: OCRSDK.config.number_or_retries, on: OCRSDK::NetworkError, sleep: retry_sleep do
60
+ api_get_result
61
+ end
58
62
  end
59
63
 
60
- def wait(seconds=OCRSDK::DEFAULT_POLL_TIME)
64
+ def wait(seconds=OCRSDK.config.default_poll_time)
61
65
  while processing? do
62
66
  sleep seconds
63
67
  update
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.add_runtime_dependency "nokogiri"
17
17
  s.add_runtime_dependency "pdf-reader"
18
18
  s.add_runtime_dependency "activesupport"
19
+ s.add_runtime_dependency "retryable"
19
20
 
20
21
  s.add_development_dependency "rake", ">= 0.8"
21
22
  if RUBY_VERSION >= '1.9'
@@ -79,7 +79,7 @@ describe OCRSDK::Image do
79
79
 
80
80
  it "should do a post request with correct url and file attached" do
81
81
  RestClient.stub(:post) do |uri, params|
82
- uri.to_s.should == "http://app_id:pass@#{OCRSDK::SERVICE_URL}/processImage?language=Russian%2CEnglish&exportFormat=txt&profile=documentConversion"
82
+ uri.to_s.should == "http://app_id:pass@cloud.ocrsdk.com/processImage?language=Russian%2CEnglish&exportFormat=txt&profile=documentConversion"
83
83
  params[:upload][:file].should be_kind_of(File)
84
84
  end
85
85
  RestClient.should_receive(:post).once
@@ -60,6 +60,15 @@ describe OCRSDK::Promise do
60
60
 
61
61
  its(:task_id) { should == 'update-task-id' }
62
62
  its(:status) { should == :in_progress }
63
+
64
+ it "should raise a NetworkError in case REST request fails, and retry 3 times" do
65
+ RestClient.stub(:get) {|url| raise RestClient::ExceptionWithResponse }
66
+ RestClient.should_receive(:get).exactly(3)
67
+
68
+ expect {
69
+ subject.result(0)
70
+ }.to raise_error(OCRSDK::NetworkError)
71
+ end
63
72
  end
64
73
 
65
74
  describe ".api_update_status" do
@@ -67,7 +76,7 @@ describe OCRSDK::Promise do
67
76
 
68
77
  it "should make an api call with correct url" do
69
78
  RestClient.stub(:get) do |url|
70
- url.to_s.should == "http://app_id:pass@#{OCRSDK::SERVICE_URL}/getTaskStatus?taskId=test"
79
+ url.to_s.should == "http://app_id:pass@cloud.ocrsdk.com/getTaskStatus?taskId=test"
71
80
  end
72
81
  RestClient.should_receive(:get).once
73
82
  subject.instance_eval { api_update_status }
@@ -90,12 +99,12 @@ describe OCRSDK::Promise do
90
99
 
91
100
  its(:result) { should == 'meow' }
92
101
 
93
- it "should raise NetworkError in case getting file fails" do
102
+ it "should raise NetworkError in case getting file fails, but retry 3 times before failing" do
94
103
  RestClient.stub(:get) {|url| raise RestClient::ExceptionWithResponse }
95
- RestClient.should_receive(:get).once
104
+ RestClient.should_receive(:get).exactly(3)
96
105
 
97
106
  expect {
98
- subject.result
107
+ subject.result(0)
99
108
  }.to raise_error(OCRSDK::NetworkError)
100
109
  end
101
110
  end
@@ -2,10 +2,6 @@
2
2
  require 'spec_helper'
3
3
 
4
4
  describe OCRSDK do
5
- it "should have service url" do
6
- OCRSDK::SERVICE_URL.length.should > 0
7
- end
8
-
9
5
  it "should be configurable" do
10
6
  OCRSDK.setup do |config|
11
7
  config.application_id = 'meow'
@@ -15,4 +11,4 @@ describe OCRSDK do
15
11
  OCRSDK.config.application_id.should == 'meow'
16
12
  OCRSDK.config.password.should == 'purr'
17
13
  end
18
- end
14
+ end
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.2.0
4
+ version: 0.3.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-15 00:00:00.000000000 Z
12
+ date: 2012-12-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ! '>='
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: retryable
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  - !ruby/object:Gem::Dependency
79
95
  name: rake
80
96
  requirement: !ruby/object:Gem::Requirement