local_copy 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -5
- data/circle.yml +3 -7
- data/lib/local_copy.rb +5 -6
- data/lib/local_copy/version.rb +1 -1
- data/local_copy.gemspec +1 -1
- data/spec/lib/local_copy_spec.rb +41 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6e2c98eaff47998d7fcbd1ad280715b1cbdb374
|
4
|
+
data.tar.gz: 46e63f3f40f2e40e91c0e61254db85f20f6eddc7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b6f10a8b5bce390f86d8a990b5c0431de0e85ee2b8d8024c41ef092e70622bca8d9c4cafc093de56c4715936677d2efb495fec4dd28d4d4fbf5c67161b1f653
|
7
|
+
data.tar.gz: 42cb886c0e8280129c321518855eecf927f2f2b0beb540d428684527557b1c6150f4f9a01dcc51799c77a8e37aa408665b612f2035b1a4bf228b47d45bd586c6
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# LocalCopy
|
2
2
|
|
3
|
-
|
3
|
+
Keep local copies of remote files.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -12,19 +12,31 @@ gem 'local_copy'
|
|
12
12
|
|
13
13
|
And then execute:
|
14
14
|
|
15
|
-
|
15
|
+
```sh
|
16
|
+
bundle
|
17
|
+
```
|
16
18
|
|
17
19
|
Or install it yourself as:
|
18
20
|
|
19
|
-
|
21
|
+
```sh
|
22
|
+
gem install local_copy
|
23
|
+
```
|
20
24
|
|
21
25
|
## Usage
|
22
26
|
|
23
|
-
|
27
|
+
```ruby
|
28
|
+
# Fetch the remote file and return a local path. For now it just
|
29
|
+
# stores it in /tmp/local_copy_gem with the MD5 hash of the URL as a
|
30
|
+
# name. When called again it does not download the file again.
|
31
|
+
local_path = LocalCopy.fetch('http://example.com/some/file.xyz')
|
32
|
+
|
33
|
+
# Empty the local file directory.
|
34
|
+
LocalCopy.flush
|
35
|
+
```
|
24
36
|
|
25
37
|
## Contributing
|
26
38
|
|
27
|
-
1. Fork it ( https://github.com/
|
39
|
+
1. Fork it ( https://github.com/ad2games/local_copy/fork )
|
28
40
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
41
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
42
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/circle.yml
CHANGED
data/lib/local_copy.rb
CHANGED
@@ -3,16 +3,15 @@ require 'digest'
|
|
3
3
|
require 'open-uri'
|
4
4
|
|
5
5
|
class LocalCopy
|
6
|
-
def self.fetch(url)
|
6
|
+
def self.fetch(url, &block)
|
7
7
|
path = File.join(local_dir, Digest::MD5.hexdigest(url))
|
8
8
|
unless File.exist?(path)
|
9
9
|
dirname = File.dirname(path)
|
10
10
|
Dir.mkdir(dirname) unless File.exist?(dirname)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
11
|
+
content = ''
|
12
|
+
open(url) { |url| content << url.read }
|
13
|
+
content = block.call(content) if block
|
14
|
+
File.open(path, 'wb') { |file| file << content }
|
16
15
|
end
|
17
16
|
path
|
18
17
|
end
|
data/lib/local_copy/version.rb
CHANGED
data/local_copy.gemspec
CHANGED
@@ -6,7 +6,7 @@ require 'local_copy/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "local_copy"
|
8
8
|
spec.version = LocalCopy::VERSION
|
9
|
-
spec.authors = ["
|
9
|
+
spec.authors = ["Helge Rausch"]
|
10
10
|
spec.email = ["developers@ad2games.com"]
|
11
11
|
spec.summary = %q{Creates a local copy of a remote file}
|
12
12
|
spec.description = %q{Creates a local copy of a remote file if it doesn't already exist}
|
data/spec/lib/local_copy_spec.rb
CHANGED
@@ -8,23 +8,50 @@ describe LocalCopy do
|
|
8
8
|
describe '.fetch' do
|
9
9
|
subject { described_class.fetch(url) }
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
11
|
+
context 'file does not exist locally' do
|
12
|
+
before do
|
13
|
+
expect(File).to receive(:exist?).with(expected_path).and_return(false)
|
14
|
+
expect(File).to receive(:exist?).with(File.dirname(expected_path)).and_return(false)
|
15
|
+
expect(Dir).to receive(:mkdir).with(File.dirname(expected_path))
|
16
|
+
expect(described_class).to receive(:open).with(url).and_yield(open)
|
17
|
+
expect(File).to receive(:open).with(expected_path, 'wb').and_yield(file)
|
18
|
+
expect(open).to receive(:read).and_return(file_content)
|
19
|
+
expect(file).to receive(:<<).with(file_content)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'fetches the remote file and returns the local path' do
|
23
|
+
expect(subject).to eq(expected_path)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "runs an optional block, passing the file's content into it" do
|
27
|
+
block = double('block')
|
28
|
+
expect(block).to receive(:call)
|
29
|
+
LocalCopy.fetch(url) do |content|
|
30
|
+
block.call
|
31
|
+
expect(content).to eq('foo')
|
32
|
+
content
|
33
|
+
end
|
34
|
+
end
|
21
35
|
end
|
22
36
|
|
23
|
-
|
24
|
-
|
25
|
-
|
37
|
+
context 'the file already exists locally' do
|
38
|
+
before do
|
39
|
+
expect(File).to receive(:exist?).with(expected_path).and_return(true)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'only returns the local path if the file already exists' do
|
43
|
+
expect(described_class).not_to receive(:open)
|
44
|
+
|
45
|
+
expect(subject).to eq(expected_path)
|
46
|
+
end
|
26
47
|
|
27
|
-
|
48
|
+
it 'does not run the block' do
|
49
|
+
block = double('block')
|
50
|
+
expect(block).not_to receive(:call)
|
51
|
+
LocalCopy.fetch(url) do |content|
|
52
|
+
block.call
|
53
|
+
end
|
54
|
+
end
|
28
55
|
end
|
29
56
|
end
|
30
57
|
|