disk_cache 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.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # DiskCache
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/propertybase/disk_cache.png)](https://travis-ci.org/propertybase/disk_cache)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/propertybase/disk_cache)
4
5
 
5
6
  What does it do? You give DiskCache the URL of a file and DiskCache makes sure
6
7
  that it has the file stored on disk. You can retrieve your files with the same
@@ -61,6 +62,8 @@ DiskCache.del('http://example.com/1234abc.jpg')
61
62
 
62
63
  - option to set the depth of subfolders
63
64
 
65
+ - option to set the Hashing algorithm (e.g. MD5, SHA1, SHA2, etc)
66
+
64
67
 
65
68
  ## Contributing
66
69
 
@@ -69,3 +72,7 @@ DiskCache.del('http://example.com/1234abc.jpg')
69
72
  3. Commit your changes (`git commit -am 'Add some feature'`)
70
73
  4. Push to the branch (`git push origin my-new-feature`)
71
74
  5. Create new Pull Request
75
+
76
+ ## License
77
+
78
+ License is MIT, see [LICENSE.txt](https://github.com/propertybase/disk_cache/blob/master/LICENSE.txt) for details.
data/disk_cache.gemspec CHANGED
@@ -10,13 +10,14 @@ Gem::Specification.new do |gem|
10
10
  gem.email = ["mxhaack@gmail.com"]
11
11
  gem.description = %q{A simple way to cache your files.}
12
12
  gem.summary = %q{Cache files that you have have to use several times.}
13
- gem.homepage = ""
13
+ gem.homepage = "https://github.com/propertybase/disk_cache"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_development_dependency "rake"
21
- gem.add_development_dependency "rspec"
20
+ gem.add_development_dependency 'rake', '~> 10.0.0'
21
+ gem.add_development_dependency 'rspec', '~> 2.12.0'
22
+ gem.add_development_dependency 'webmock', '~> 1.9.0'
22
23
  end
@@ -1,3 +1,3 @@
1
1
  module DiskCache
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/disk_cache.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'disk_cache/version'
2
2
  require 'open-uri'
3
- require 'digest/sha2'
3
+ require 'digest/sha1'
4
4
 
5
5
  module DiskCache
6
6
  extend self
@@ -16,6 +16,7 @@ module DiskCache
16
16
  #
17
17
  # Returns: nil
18
18
  def put(url)
19
+ url = escape(url)
19
20
  file = filepath(url)
20
21
  return nil if File.exist?(file)
21
22
 
@@ -38,6 +39,7 @@ module DiskCache
38
39
  #
39
40
  # Returns the File or nil
40
41
  def get(url)
42
+ url = escape(url)
41
43
  File.open(filepath(url), "rb")
42
44
  rescue Errno::ENOENT
43
45
  nil
@@ -54,6 +56,7 @@ module DiskCache
54
56
  #
55
57
  # Returns a File
56
58
  def pget(url)
59
+ url = escape(url)
57
60
  put(url)
58
61
  get(url)
59
62
  end
@@ -69,9 +72,30 @@ module DiskCache
69
72
  #
70
73
  # Returns nil
71
74
  def del(url)
75
+ url = escape(url)
72
76
  FileUtils.rm filepath(url), force: true
73
77
  end
74
78
 
79
+ # Public: calculate the path/filename of a file's URL
80
+ #
81
+ # url - the URL of the file
82
+ #
83
+ # Example:
84
+ #
85
+ # DiskCache.filepath('http://example.com/test123.jpg')
86
+ # # => "cache/9a/e2/74d94c34542ddd1b64667c1d4e392211ff67"
87
+ #
88
+ # Returns a Sting with the full path and filename
89
+ def filepath(url)
90
+ hsh = hash url
91
+ path_h(hsh) + filename_h(hsh)
92
+ end
93
+
94
+ # Public: this removes all contents of the cache.
95
+ def clear!
96
+ FileUtils.rm_r cache_dir if File.exists? cache_dir
97
+ end
98
+
75
99
  private
76
100
 
77
101
  # Internal: Hashes a given piece of data with SHA1
@@ -85,8 +109,7 @@ module DiskCache
85
109
  #
86
110
  # Returns String with hexdigest
87
111
  def hash(datum)
88
- @hasher ||= Digest::SHA1.new
89
- @hasher.hexdigest(datum)
112
+ Digest::SHA1.new.hexdigest(datum)
90
113
  end
91
114
 
92
115
  # Internal: Return the directory of the cache as a String
@@ -132,18 +155,17 @@ module DiskCache
132
155
  hsh[4..-1]
133
156
  end
134
157
 
135
- # Internal: calculate the path/filename of a file's URL
158
+ # Internal: escape a given url
136
159
  #
137
- # url - the URL of the file
160
+ # url - the URL to escape
138
161
  #
139
162
  # Example:
140
163
  #
141
- # filepath!('http://example.com/test123.jpg')
142
- # # => "cache/9a/e2/74d94c34542ddd1b64667c1d4e392211ff67"
164
+ # url_escape('http://example.com/hello world')
165
+ # # => "http://example.com/hello%20world"
143
166
  #
144
- # Returns a Sting with the full path and filename
145
- def filepath(url)
146
- hsh = hash url
147
- path_h(hsh) + filename_h(hsh)
167
+ # Returns a String with the escaped url
168
+ def escape(url)
169
+ URI.escape(url)
148
170
  end
149
171
  end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe DiskCache do
4
+ let(:path) { 'spec/asset/Trollface.svg' }
5
+
6
+ before(:each) { DiskCache.clear! }
7
+
8
+ context "storing and accessing" do
9
+ it ".put" do
10
+ DiskCache.put(path)
11
+ DiskCache.get(path).should_not be_nil
12
+ end
13
+
14
+ it ".get" do
15
+ DiskCache.put(path)
16
+ file_from_cache = DiskCache.get(path)
17
+ FileUtils.compare_file(path, file_from_cache).should be_true
18
+ end
19
+
20
+ it ".pget" do
21
+ DiskCache.del(path)
22
+ file_from_cache = DiskCache.pget(path)
23
+ FileUtils.compare_file(path, file_from_cache).should be_true
24
+ end
25
+
26
+ it ".filepath" do
27
+ DiskCache.put(path)
28
+ DiskCache.filepath(path).should include(
29
+ 'cache/fb/2b/228b717ea0a569bc7d12187ff55e5e3a0180')
30
+ end
31
+ end
32
+
33
+ context "removing data" do
34
+ before(:each) { DiskCache.put(path) }
35
+
36
+ it ".delete" do
37
+ DiskCache.del(path)
38
+ DiskCache.get(path).should be_nil
39
+ end
40
+
41
+ it ".clear" do
42
+ DiskCache.clear!
43
+ DiskCache.get(path).should be_nil
44
+ end
45
+ end
46
+
47
+ context "sanity" do
48
+ let(:web) { 'http://example.com/Troll face.svg' }
49
+ let(:cto) { File.read(path) }
50
+
51
+ before(:each) do
52
+ stub_request(:get, "http://example.com/Troll face.svg").
53
+ to_return(body: cto)
54
+ end
55
+
56
+ it "should handle urls with spaces" do
57
+ DiskCache.put(web)
58
+ file_from_cache = DiskCache.get(web)
59
+ FileUtils.compare_file(path, file_from_cache).should be_true
60
+ end
61
+ end
62
+ end
data/spec/spec_helper.rb CHANGED
@@ -1 +1,2 @@
1
1
  require 'disk_cache'
2
+ require 'webmock/rspec'
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,40 +9,56 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 10.0.0
22
22
  type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ! '>='
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: 10.0.0
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 2.12.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 2.12.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: webmock
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.9.0
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.9.0
46
62
  description: A simple way to cache your files.
47
63
  email:
48
64
  - mxhaack@gmail.com
@@ -60,9 +76,9 @@ files:
60
76
  - lib/disk_cache.rb
61
77
  - lib/disk_cache/version.rb
62
78
  - spec/asset/Trollface.svg
63
- - spec/file_cache/file_cache_spec.rb
79
+ - spec/disk_cache/disk_cache_spec.rb
64
80
  - spec/spec_helper.rb
65
- homepage: ''
81
+ homepage: https://github.com/propertybase/disk_cache
66
82
  licenses: []
67
83
  post_install_message:
68
84
  rdoc_options: []
@@ -76,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
92
  version: '0'
77
93
  segments:
78
94
  - 0
79
- hash: 804760917571637690
95
+ hash: -1244554166794176728
80
96
  required_rubygems_version: !ruby/object:Gem::Requirement
81
97
  none: false
82
98
  requirements:
@@ -85,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
101
  version: '0'
86
102
  segments:
87
103
  - 0
88
- hash: 804760917571637690
104
+ hash: -1244554166794176728
89
105
  requirements: []
90
106
  rubyforge_project:
91
107
  rubygems_version: 1.8.24
@@ -94,5 +110,5 @@ specification_version: 3
94
110
  summary: Cache files that you have have to use several times.
95
111
  test_files:
96
112
  - spec/asset/Trollface.svg
97
- - spec/file_cache/file_cache_spec.rb
113
+ - spec/disk_cache/disk_cache_spec.rb
98
114
  - spec/spec_helper.rb
@@ -1,34 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DiskCache do
4
- let(:path) { 'spec/asset/Trollface.svg' }
5
- let(:file) { File.open(path, 'r') }
6
-
7
- context "storing and accessing" do
8
- it ".put" do
9
- DiskCache.put(path)
10
- DiskCache.get(path).should_not be_nil
11
- end
12
-
13
- it ".get" do
14
- DiskCache.put(path)
15
- file_from_cache = DiskCache.get(path)
16
- FileUtils.compare_file(file, file_from_cache).should be_true
17
- end
18
-
19
- it ".pget" do
20
- DiskCache.del(path)
21
- file_from_cache = DiskCache.pget(path)
22
- FileUtils.compare_file(file, file_from_cache).should be_true
23
- end
24
- end
25
-
26
- context "removing data" do
27
- before(:each) { DiskCache.put(path) }
28
-
29
- it ".delete" do
30
- DiskCache.del(path)
31
- DiskCache.get(path).should be_nil
32
- end
33
- end
34
- end