rhcf-utils 0.0.2 → 0.0.3
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 +4 -4
- data/lib/rhcf/utils/download_cache.rb +8 -9
- data/lib/rhcf/utils/version.rb +1 -1
- data/spec/lib/rhcf/utils/download_cache_spec.rb +8 -8
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9909d07e5aa545cbf9b72db81d6149001d00e90a
|
4
|
+
data.tar.gz: eae25b18c3232384492b5cae2e580680e360f99e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 811297382e1341960a989f99da58968501a9c3efca6966fb0abb34d07f309f483d759af9ce2acbac0a5866ee9a14767cba60ebc61a2f87b1eca70c3e06e9a812
|
7
|
+
data.tar.gz: 9f1e833fd8bd6acf277febc0cde75fe934ba2918f79e533dbe0e6936a1677585eaffa7f928c83e73201609a8816eda14e4eabfeeca5e807db725c0b9fa40f517
|
@@ -18,18 +18,13 @@ module Rhcf
|
|
18
18
|
outfile = filename_for(url)
|
19
19
|
return outfile if self.class.hit_fname?(outfile, @ttl) # here goes the cache
|
20
20
|
download!(url, outfile)
|
21
|
-
rescue
|
22
|
-
if File.exist?(outfile)
|
23
|
-
File.unlink(outfile)
|
24
|
-
end
|
25
|
-
raise
|
26
21
|
end
|
27
22
|
|
28
23
|
|
29
24
|
def download!(url, outfile)
|
30
|
-
|
31
|
-
|
32
|
-
open(
|
25
|
+
open(url, "rb") do |down|
|
26
|
+
mkdir_p(File.dirname(outfile))
|
27
|
+
File.open(outfile, 'wb') do |fd|
|
33
28
|
fd.write(down.read)
|
34
29
|
end
|
35
30
|
end
|
@@ -58,7 +53,11 @@ module Rhcf
|
|
58
53
|
hash = Digest::MD5.hexdigest(url)
|
59
54
|
uri = URI(url)
|
60
55
|
basename = File.basename(uri.path)
|
61
|
-
File.join(@root_path, @cache_id, hash, basename)
|
56
|
+
File.join(@root_path, @cache_id, hash_tree(hash), basename)
|
57
|
+
end
|
58
|
+
|
59
|
+
def hash_tree(hash)
|
60
|
+
[*(hash[0,3].split('')) ,hash].join('/')
|
62
61
|
end
|
63
62
|
|
64
63
|
end
|
data/lib/rhcf/utils/version.rb
CHANGED
@@ -30,12 +30,12 @@ describe Rhcf::Utils::DownloadCache do
|
|
30
30
|
|
31
31
|
describe "#hit?" do
|
32
32
|
before(:each) do
|
33
|
-
FileUtils.rm_rf "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/aea455b4b37a2d4240a6b054ca0b0d5f8"
|
34
|
-
FileUtils.mkdir_p "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440"
|
35
|
-
FileUtils.touch "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg"
|
33
|
+
FileUtils.rm_rf "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/a/e/a/aea455b4b37a2d4240a6b054ca0b0d5f8"
|
34
|
+
FileUtils.mkdir_p "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e/7/f/e7fb5389b6646ebb89aaa5502b75a440"
|
35
|
+
FileUtils.touch "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e/7/f/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg"
|
36
36
|
end
|
37
37
|
it "should return true if file downloaded and ttl is null" do
|
38
|
-
cache_of_30s.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg'
|
38
|
+
cache_of_30s.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e/7/f/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg'
|
39
39
|
cache_of_30s.hit?(img_url).should be_true
|
40
40
|
end
|
41
41
|
it "should return true if file downloaded and newer then ttl" do
|
@@ -47,7 +47,7 @@ describe Rhcf::Utils::DownloadCache do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
it "should return false if doesn't exist" do
|
50
|
-
FileUtils.rm "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg"
|
50
|
+
FileUtils.rm "/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e/7/f/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg"
|
51
51
|
cache_of_30s.hit?(img_url).should be_false
|
52
52
|
end
|
53
53
|
|
@@ -56,7 +56,7 @@ describe Rhcf::Utils::DownloadCache do
|
|
56
56
|
|
57
57
|
describe "#filename_for" do
|
58
58
|
it "should return a path with cache id , file name hash and file hash component" do
|
59
|
-
subject.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/default/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg'
|
59
|
+
subject.filename_for(img_url).should == '/tmp/Rhcf::Utils::DownloadCache/default/e/7/f/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg'
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
@@ -77,8 +77,8 @@ describe Rhcf::Utils::DownloadCache do
|
|
77
77
|
end
|
78
78
|
|
79
79
|
describe "#get" do
|
80
|
-
let(:expected_path){ '/tmp/Rhcf::Utils::DownloadCache/default/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg' }
|
81
|
-
let(:expected_30s_path){ '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg' }
|
80
|
+
let(:expected_path){ '/tmp/Rhcf::Utils::DownloadCache/default/e/7/f/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg' }
|
81
|
+
let(:expected_30s_path){ '/tmp/Rhcf::Utils::DownloadCache/cache_of_30s/e/7/f/e7fb5389b6646ebb89aaa5502b75a440/imagens-do-dia-20131023-04-original.jpg' }
|
82
82
|
before(:each) do
|
83
83
|
File.unlink(expected_path) rescue nil
|
84
84
|
end
|