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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b7d2c8b3474bfc87abd22e42799684ea8ced576
4
- data.tar.gz: 7773258bd24df4cf49d24c4a2c8e18d9225f00e5
3
+ metadata.gz: b6e2c98eaff47998d7fcbd1ad280715b1cbdb374
4
+ data.tar.gz: 46e63f3f40f2e40e91c0e61254db85f20f6eddc7
5
5
  SHA512:
6
- metadata.gz: e633d35fb291cb2343aafc37cf36d0c2ad33433bbdf695bfd2296680d8fed222f80e3b2d612c47361b252129e00e1f2b0960881d6baf5b264e1699a19864e1e2
7
- data.tar.gz: 796bf02fd8af21a43635fba085f98827b2052564c90364742dfec09599c67a5d09d6788a8f7ea9f796383cd3bcaeb448c29cf26f33aaf73948a2c87d998aca09
6
+ metadata.gz: 9b6f10a8b5bce390f86d8a990b5c0431de0e85ee2b8d8024c41ef092e70622bca8d9c4cafc093de56c4715936677d2efb495fec4dd28d4d4fbf5c67161b1f653
7
+ data.tar.gz: 42cb886c0e8280129c321518855eecf927f2f2b0beb540d428684527557b1c6150f4f9a01dcc51799c77a8e37aa408665b612f2035b1a4bf228b47d45bd586c6
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # LocalCopy
2
2
 
3
- TODO: Write a gem description
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
- $ bundle
15
+ ```sh
16
+ bundle
17
+ ```
16
18
 
17
19
  Or install it yourself as:
18
20
 
19
- $ gem install local_copy
21
+ ```sh
22
+ gem install local_copy
23
+ ```
20
24
 
21
25
  ## Usage
22
26
 
23
- TODO: Write usage instructions here
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/[my-github-username]/local_copy/fork )
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
@@ -1,7 +1,3 @@
1
- deployment:
2
- production:
3
- branch: master
4
- commands:
5
- - gem install geminabox
6
- - gem build local_copy.gemspec
7
- - gem inabox local_copy-*.gem --host $GEMINABOX_URL
1
+ dependencies:
2
+ pre:
3
+ - gem update bundler
@@ -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
- File.open(path, 'wb') do |file|
12
- open(url) do |url|
13
- file << url.read
14
- end
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
@@ -1,3 +1,3 @@
1
1
  class LocalCopy
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -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 = ["ad2games GmbH"]
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}
@@ -8,23 +8,50 @@ describe LocalCopy do
8
8
  describe '.fetch' do
9
9
  subject { described_class.fetch(url) }
10
10
 
11
- it 'fetches the remote file and returns the local path' do
12
- expect(File).to receive(:exist?).with(expected_path).and_return(false)
13
- expect(File).to receive(:exist?).with(File.dirname(expected_path)).and_return(false)
14
- expect(Dir).to receive(:mkdir).with(File.dirname(expected_path))
15
- expect(described_class).to receive(:open).with(url).and_yield(open)
16
- expect(File).to receive(:open).with(expected_path, 'wb').and_yield(file)
17
- expect(open).to receive(:read).and_return(file_content)
18
- expect(file).to receive(:<<).with(file_content)
19
-
20
- expect(subject).to eq(expected_path)
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
- it 'only returns the local path if the file already exists' do
24
- expect(File).to receive(:exist?).with(expected_path).and_return(true)
25
- expect(described_class).not_to receive(:open)
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
- expect(subject).to eq(expected_path)
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
 
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: local_copy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
- - ad2games GmbH
7
+ - Helge Rausch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []