local_copy 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6e2c98eaff47998d7fcbd1ad280715b1cbdb374
4
- data.tar.gz: 46e63f3f40f2e40e91c0e61254db85f20f6eddc7
3
+ metadata.gz: 745805229060edd1b1561bedc0e9c5e2c8369f7c
4
+ data.tar.gz: 7ca559130f7c0ab86e3d2849b35306302ac41196
5
5
  SHA512:
6
- metadata.gz: 9b6f10a8b5bce390f86d8a990b5c0431de0e85ee2b8d8024c41ef092e70622bca8d9c4cafc093de56c4715936677d2efb495fec4dd28d4d4fbf5c67161b1f653
7
- data.tar.gz: 42cb886c0e8280129c321518855eecf927f2f2b0beb540d428684527557b1c6150f4f9a01dcc51799c77a8e37aa408665b612f2035b1a4bf228b47d45bd586c6
6
+ metadata.gz: 1d3786916ae60293f3a0163f3a1d73bbbc2ad9b1751165ce56a0061a8ddc22cd7340cc88c0f9ef25b2e724c97affb367d98a3a80aa42289e4fd3126297ad84ef
7
+ data.tar.gz: 254a6fce669f7b8910b5ace4bf0655da4fb2e475c7ff4a726180203075074e42156ac2f461e149cd716e05d121c8312598f9fc53b6a1fe75f2f478dafab1a3ca
@@ -3,31 +3,33 @@ require 'digest'
3
3
  require 'open-uri'
4
4
 
5
5
  class LocalCopy
6
- def self.fetch(url, &block)
7
- path = File.join(local_dir, Digest::MD5.hexdigest(url))
8
- unless File.exist?(path)
9
- dirname = File.dirname(path)
10
- Dir.mkdir(dirname) unless File.exist?(dirname)
11
- content = ''
12
- open(url) { |url| content << url.read }
13
- content = block.call(content) if block
14
- File.open(path, 'wb') { |file| file << content }
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
- def self.flush
20
- deleted_files = []
21
- return deleted_files unless File.exist?(local_dir)
22
- (Dir.entries(local_dir) - %w(. ..)).each do |file|
23
- path = File.join(local_dir, file)
24
- File.delete(path)
25
- deleted_files << path
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
- def self.local_dir
31
- File.join('', 'tmp', 'local_copy_gem')
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
+
@@ -1,3 +1,3 @@
1
1
  class LocalCopy
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -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.6
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-09 00:00:00.000000000 Z
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