disk_store 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +38 -0
- data/README.md +70 -0
- data/Rakefile +7 -0
- data/disk_store.gemspec +28 -0
- data/lib/disk_store/version.rb +3 -0
- data/lib/disk_store.rb +84 -0
- data/spec/cassettes/DiskStore/web_resources/should_cache_the_result_of_a_web_resource_in_a_file.json +1 -0
- data/spec/disk_store_spec.rb +84 -0
- data/spec/spec_helper.rb +32 -0
- metadata +127 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe DiskStore do
|
4
|
+
let(:cache) { DiskStore.new @tmpdir }
|
5
|
+
let(:file_contents) { "Hello, Doge" }
|
6
|
+
let(:file) { Tempfile.new("My Temp File.psd") }
|
7
|
+
let(:key) { "doge" }
|
8
|
+
|
9
|
+
around(:each) do |example|
|
10
|
+
Dir.mktmpdir do |tmpdir|
|
11
|
+
@tmpdir = tmpdir
|
12
|
+
example.run
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
before(:each) do
|
17
|
+
file.write file_contents
|
18
|
+
file.flush
|
19
|
+
end
|
20
|
+
|
21
|
+
context "initializing" do
|
22
|
+
subject { DiskStore.new }
|
23
|
+
it { should respond_to(:read) }
|
24
|
+
it { should respond_to(:write) }
|
25
|
+
it { should respond_to(:fetch) }
|
26
|
+
it { should respond_to(:delete) }
|
27
|
+
it { should respond_to(:exist?) }
|
28
|
+
end
|
29
|
+
|
30
|
+
context "#read" do
|
31
|
+
it "reads from disk" do
|
32
|
+
cache.write(key, file)
|
33
|
+
file.close
|
34
|
+
file.unlink # ensure the tempfile is deleted
|
35
|
+
expect(cache.read(key)).to be_an_instance_of(File)
|
36
|
+
expect(cache.read(key).read).to eq file_contents
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "#write" do
|
41
|
+
it "writes a file to disk" do
|
42
|
+
expect{
|
43
|
+
cache.write(key, file)
|
44
|
+
}.to change{ Dir[File.join(@tmpdir, "**/*")].size }.by(3)
|
45
|
+
|
46
|
+
file_path = Dir[File.join(@tmpdir, "**/*")].last
|
47
|
+
expect(File.read(file_path)).to eq file_contents
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "#delete" do
|
52
|
+
it "deletes the file from disk" do
|
53
|
+
cache.write(key, file)
|
54
|
+
expect{
|
55
|
+
cache.delete(key)
|
56
|
+
}.to change{ Dir[File.join(@tmpdir, "**/*")].size }.by(-3)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "#exist?" do
|
61
|
+
it "returns correct value" do
|
62
|
+
expect(cache.exist?(key)).to be_false
|
63
|
+
cache.write(key, file)
|
64
|
+
expect(cache.exist?(key)).to be_true
|
65
|
+
cache.delete(key)
|
66
|
+
expect(cache.exist?(key)).to be_false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "web resources", :vcr do
|
71
|
+
let(:url) { "http://media.giphy.com/media/KXD1pSzGb3Khi/giphy.gif" }
|
72
|
+
let(:key) { "lolololol" }
|
73
|
+
|
74
|
+
it "should cache the result of a web resource in a file" do
|
75
|
+
expect(cache.exist?(key)).to be_false
|
76
|
+
|
77
|
+
cache.fetch(key) do
|
78
|
+
open(url)
|
79
|
+
end
|
80
|
+
|
81
|
+
expect(cache.read(key).read).to eq open(url).read
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path './lib/disk_store'
|
2
|
+
require 'rspec'
|
3
|
+
require 'tempfile'
|
4
|
+
require 'tmpdir'
|
5
|
+
require 'vcr'
|
6
|
+
|
7
|
+
VCR.configure do |c|
|
8
|
+
c.configure_rspec_metadata!
|
9
|
+
|
10
|
+
c.default_cassette_options = {
|
11
|
+
serialize_with: :json,
|
12
|
+
preserve_exact_body_bytes: true,
|
13
|
+
decode_compressed_response: true,
|
14
|
+
match_requests_on: [:method]
|
15
|
+
}
|
16
|
+
|
17
|
+
c.cassette_library_dir = 'spec/cassettes'
|
18
|
+
c.hook_into :webmock
|
19
|
+
c.ignore_localhost = true
|
20
|
+
end
|
21
|
+
|
22
|
+
RSpec.configure do |config|
|
23
|
+
config.color_enabled = true
|
24
|
+
config.tty = true
|
25
|
+
|
26
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
27
|
+
|
28
|
+
config.around(:each, :vcr) do |example|
|
29
|
+
name = example.metadata[:full_description].gsub(/\//, "_").split(/\s+/, 2).join("/").gsub(/[^\w\/]+/, "_").downcase
|
30
|
+
VCR.use_cassette(name) { example.call }
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: disk_store
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kelly Sutton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: multi_json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: vcr
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Cache files the smart way.
|
84
|
+
email:
|
85
|
+
- kelly@layervault.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- Gemfile
|
91
|
+
- Gemfile.lock
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- disk_store.gemspec
|
95
|
+
- lib/disk_store.rb
|
96
|
+
- lib/disk_store/version.rb
|
97
|
+
- spec/cassettes/DiskStore/web_resources/should_cache_the_result_of_a_web_resource_in_a_file.json
|
98
|
+
- spec/disk_store_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
homepage: http://cosmos.layervault.com/cache.html
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.1.9
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Cache fiels the smart way.
|
124
|
+
test_files:
|
125
|
+
- spec/cassettes/DiskStore/web_resources/should_cache_the_result_of_a_web_resource_in_a_file.json
|
126
|
+
- spec/disk_store_spec.rb
|
127
|
+
- spec/spec_helper.rb
|