email_vision 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{email_vision}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
@@ -70,17 +70,7 @@ class EmailVision
70
70
  private
71
71
 
72
72
  def connection
73
- unless connected?
74
- response = client.request :open_api_connection do |r|
75
- r.body = {
76
- :login => options[:login],
77
- :pwd => options[:password],
78
- :key => options[:key]
79
- }
80
- end
81
- @token = response.to_hash[:open_api_connection_response][:return]
82
- @token_requested = Time.now.to_i
83
- end
73
+ connect! unless connected?
84
74
  client
85
75
  end
86
76
 
@@ -94,6 +84,18 @@ class EmailVision
94
84
  @token and @token_requested > (Time.now.to_i - SESSION_TIMEOUT)
95
85
  end
96
86
 
87
+ def connect!
88
+ response = client.request :open_api_connection do |r|
89
+ r.body = {
90
+ :login => options[:login],
91
+ :pwd => options[:password],
92
+ :key => options[:key]
93
+ }
94
+ end
95
+ @token = response.to_hash[:open_api_connection_response][:return]
96
+ @token_requested = Time.now.to_i
97
+ end
98
+
97
99
  def execute_by_email_or_id(method, email_or_id)
98
100
  if email_or_id.to_s.include?('@')
99
101
  execute("#{method}_by_email", :email => email_or_id)
@@ -120,6 +122,21 @@ class EmailVision
120
122
  def execute(method, options={})
121
123
  response = connection.request(method){|r| r.body = options.merge(:token => @token) }
122
124
  response.to_hash["#{method}_response".to_sym][:return]
125
+ rescue Object => e
126
+ if e.respond_to?(:http) and e.http.respond_to?(:body)
127
+ retries ||= -1
128
+ retries += 1
129
+ session_error = (e.http.body =~ /status>(SESSION_RETRIEVING_FAILED|CHECK_SESSION_FAILED)</)
130
+ if session_error
131
+ if retries < 1
132
+ connect!
133
+ retry
134
+ else
135
+ e.message << " -- retried #{retries}"
136
+ end
137
+ end
138
+ end
139
+ raise e
123
140
  end
124
141
 
125
142
  def convert_to_hash(entries, key, value)
@@ -7,6 +7,16 @@ describe "EmailVision" do
7
7
  let(:random_value){rand(11111111111).to_s}
8
8
  let(:expired_token){'Duy-M5FktALawBJ7dZN94s6hLEgLGKXC_j7cCqlDUMXRGw2shqHYbR9Zud_19EBtFkSCbJ0ZmrZ_d0ieBqgR'}
9
9
 
10
+ def error_with_status(status)
11
+ # mock / stub did not work...
12
+ $the_status = status
13
+ http = ""
14
+ def http.error?; true; end
15
+ def http.body; "<status>#{$the_status}</status>"; end
16
+ def http.code; 500; end
17
+ Savon::HTTP::Error.new(http)
18
+ end
19
+
10
20
  # updates need some time to finish on the server...
11
21
  def wait_for_job_to_finish
12
22
  client.wait_for_job_to_finish yield
@@ -63,6 +73,37 @@ describe "EmailVision" do
63
73
  end
64
74
  end
65
75
 
76
+ describe 'error handling' do
77
+ before do
78
+ @connection = client.send(:connection)
79
+ client.stub!(:connection).and_return @connection
80
+ end
81
+
82
+ it "retries if it failed due to session timeout" do
83
+ error = error_with_status("CHECK_SESSION_FAILED")
84
+ @connection.should_receive(:request).exactly(2).and_raise(error)
85
+ lambda{
86
+ client.find('aaaa')
87
+ }.should raise_error#(error)
88
+ end
89
+
90
+ it "retries if it failed due to maximum request per session" do
91
+ error = error_with_status("SESSION_RETRIEVING_FAILED")
92
+ @connection.should_receive(:request).exactly(2).and_raise(error)
93
+ lambda{
94
+ client.find('aaaa')
95
+ }.should raise_error#(error)
96
+ end
97
+
98
+ it "does not retry if it failed otherwise" do
99
+ error = error_with_status("FOO_BAR")
100
+ @connection.should_receive(:request).exactly(1).and_raise(error)
101
+ lambda{
102
+ client.find('aaaa')
103
+ }.should raise_error#(error)
104
+ end
105
+ end
106
+
66
107
  describe :update do
67
108
  it "can update an attribute" do
68
109
  wait_for_job_to_finish do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_vision
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser