disk_cache 0.0.7 → 0.0.8

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/lib/disk_cache.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'disk_cache/version'
2
2
  require 'open-uri'
3
+ require 'openssl'
3
4
  require 'digest/sha1'
4
5
  require 'fileutils'
5
6
 
@@ -21,7 +22,23 @@ module DiskCache
21
22
  return nil if File.exist?(file)
22
23
 
23
24
  FileUtils.mkdir_p path(url)
24
- File.open(file, "wb") { |f| f << open(escape(url)).read }
25
+ trys = 0
26
+ begin
27
+ trys = trys + 1
28
+ data = open(escape(url)).read
29
+ File.open(file, "wb") { |f| f << data }
30
+ rescue OpenURI::HTTPError => httperror
31
+ raise httperror unless trys < 3
32
+ retry
33
+ rescue Timeout::Error => timeout
34
+ raise timeout unless trys < 3
35
+ retry
36
+ rescue OpenSSL::SSL::SSLError => ssl
37
+ raise ssl unless trys < 3
38
+ retry
39
+ rescue => error
40
+ raise error
41
+ end
25
42
  nil
26
43
  end
27
44
 
@@ -1,3 +1,3 @@
1
1
  module DiskCache
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -68,4 +68,55 @@ describe DiskCache do
68
68
  FileUtils.compare_file(path, file_from_cache).should be_true
69
69
  end
70
70
  end
71
+
72
+ context "failure responses" do
73
+ let(:web) { 'http://example.com/Troll%20face.svg' }
74
+
75
+ # fail once
76
+ fail_once = {
77
+ "400" => "Bad Request",
78
+ "401" => "Unauthorized",
79
+ "403" => "Forbidden",
80
+ "404" => "Not Found",
81
+ "405" => "Method Not Allowed",
82
+ "501" => "Not Implemented"
83
+ }
84
+ fail_once.each do |code, msg|
85
+ it "#{code}" do
86
+ stub_request(:get, web).to_return(:status => [code, msg])
87
+ expect { DiskCache.put(web) }.to raise_error(OpenURI::HTTPError)
88
+ DiskCache.get(web).should be_nil
89
+ end
90
+ end
91
+
92
+ # retry!
93
+ retry_on_failure = {
94
+ "408" => "Request Timeout",
95
+ "409" => "Conflict",
96
+ "410" => "Gone",
97
+ "500" => "Internal Server Error",
98
+ "502" => "Bad Gateway",
99
+ "503" => "Service Unavailable",
100
+ "504" => "Gateway Timeout"
101
+ }
102
+ retry_on_failure.each do |code, msg|
103
+ it "#{code}" do
104
+ stub_request(:get, web).to_return(:status => [code, msg])
105
+ expect { DiskCache.put(web) }.to raise_error(OpenURI::HTTPError)
106
+ DiskCache.get(web).should be_nil
107
+ end
108
+ end
109
+
110
+ it "should retry on timeout" do
111
+ stub_request(:get, web).to_timeout
112
+ expect { DiskCache.put(web) }.to raise_error(Timeout::Error)
113
+ DiskCache.get(web).should be_nil
114
+ end
115
+
116
+ it "should retry on SSL Error" do
117
+ stub_request(:get, web).to_raise(OpenSSL::SSL::SSLError)
118
+ expect { DiskCache.put(web) }.to raise_error(OpenSSL::SSL::SSLError)
119
+ DiskCache.get(web).should be_nil
120
+ end
121
+ end
71
122
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: disk_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
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-03 00:00:00.000000000 Z
12
+ date: 2012-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake