local_copy 0.0.6 → 0.0.7
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/local_copy.rb +24 -22
- data/lib/local_copy/vcr_with_rspec.rb +18 -0
- data/lib/local_copy/version.rb +1 -1
- data/spec/lib/local_copy_spec.rb +4 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 745805229060edd1b1561bedc0e9c5e2c8369f7c
|
4
|
+
data.tar.gz: 7ca559130f7c0ab86e3d2849b35306302ac41196
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d3786916ae60293f3a0163f3a1d73bbbc2ad9b1751165ce56a0061a8ddc22cd7340cc88c0f9ef25b2e724c97affb367d98a3a80aa42289e4fd3126297ad84ef
|
7
|
+
data.tar.gz: 254a6fce669f7b8910b5ace4bf0655da4fb2e475c7ff4a726180203075074e42156ac2f461e149cd716e05d121c8312598f9fc53b6a1fe75f2f478dafab1a3ca
|
data/lib/local_copy.rb
CHANGED
@@ -3,31 +3,33 @@ require 'digest'
|
|
3
3
|
require 'open-uri'
|
4
4
|
|
5
5
|
class LocalCopy
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
class << self
|
7
|
+
def fetch(url, &block)
|
8
|
+
path = File.join(local_dir, Digest::MD5.hexdigest(url))
|
9
|
+
unless File.exist?(path)
|
10
|
+
dirname = File.dirname(path)
|
11
|
+
Dir.mkdir(dirname) unless File.exist?(dirname)
|
12
|
+
content = ''
|
13
|
+
open(url) { |url| content << url.read }
|
14
|
+
content = block.call(content) if block
|
15
|
+
File.open(path, 'wb') { |file| file << content }
|
16
|
+
end
|
17
|
+
path
|
15
18
|
end
|
16
|
-
path
|
17
|
-
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
def flush
|
21
|
+
deleted_files = []
|
22
|
+
return deleted_files unless File.exist?(local_dir)
|
23
|
+
(Dir.entries(local_dir) - %w(. ..)).each do |file|
|
24
|
+
path = File.join(local_dir, file)
|
25
|
+
File.delete(path)
|
26
|
+
deleted_files << path
|
27
|
+
end
|
28
|
+
deleted_files
|
26
29
|
end
|
27
|
-
deleted_files
|
28
|
-
end
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
def local_dir
|
32
|
+
File.join('', 'tmp', 'local_copy_gem')
|
33
|
+
end
|
32
34
|
end
|
33
35
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# If you use VCR and RSpec, you probably want to require this file in your spec
|
2
|
+
# helper. It adds some RSpec configuration, that makes VCR record requests made
|
3
|
+
# by LocalCopy in a defined place and only once.
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.before(:suite) do
|
6
|
+
LocalCopy.flush
|
7
|
+
end
|
8
|
+
|
9
|
+
config.before(:example) do
|
10
|
+
LocalCopy.singleton_class.send(:alias_method, :fetch_original, :fetch)
|
11
|
+
allow(LocalCopy).to receive(:fetch) do |url, &block|
|
12
|
+
VCR.use_cassette("local_copy_#{Digest::MD5.hexdigest(url)}", exclusive: true) do
|
13
|
+
LocalCopy.fetch_original(url, &block)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
data/lib/local_copy/version.rb
CHANGED
data/spec/lib/local_copy_spec.rb
CHANGED
@@ -16,20 +16,22 @@ describe LocalCopy do
|
|
16
16
|
expect(described_class).to receive(:open).with(url).and_yield(open)
|
17
17
|
expect(File).to receive(:open).with(expected_path, 'wb').and_yield(file)
|
18
18
|
expect(open).to receive(:read).and_return(file_content)
|
19
|
-
expect(file).to receive(:<<).with(file_content)
|
20
19
|
end
|
21
20
|
|
22
21
|
it 'fetches the remote file and returns the local path' do
|
22
|
+
expect(file).to receive(:<<).with(file_content)
|
23
23
|
expect(subject).to eq(expected_path)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "runs an optional block, passing the file's content into it" do
|
27
|
+
expect(file).to receive(:<<).with('oof')
|
27
28
|
block = double('block')
|
28
29
|
expect(block).to receive(:call)
|
30
|
+
|
29
31
|
LocalCopy.fetch(url) do |content|
|
30
32
|
block.call
|
31
33
|
expect(content).to eq('foo')
|
32
|
-
content
|
34
|
+
content.reverse
|
33
35
|
end
|
34
36
|
end
|
35
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: local_copy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Helge Rausch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -95,6 +95,7 @@ files:
|
|
95
95
|
- Rakefile
|
96
96
|
- circle.yml
|
97
97
|
- lib/local_copy.rb
|
98
|
+
- lib/local_copy/vcr_with_rspec.rb
|
98
99
|
- lib/local_copy/version.rb
|
99
100
|
- local_copy.gemspec
|
100
101
|
- spec/fixtures/vcr/LocalCopy/downloads_a_remote_file_and_returns_the_local_path.yml
|