local_copy 0.0.5

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b7d2c8b3474bfc87abd22e42799684ea8ced576
4
+ data.tar.gz: 7773258bd24df4cf49d24c4a2c8e18d9225f00e5
5
+ SHA512:
6
+ metadata.gz: e633d35fb291cb2343aafc37cf36d0c2ad33433bbdf695bfd2296680d8fed222f80e3b2d612c47361b252129e00e1f2b0960881d6baf5b264e1699a19864e1e2
7
+ data.tar.gz: 796bf02fd8af21a43635fba085f98827b2052564c90364742dfec09599c67a5d09d6788a8f7ea9f796383cd3bcaeb448c29cf26f33aaf73948a2c87d998aca09
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in local_copy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Helge Rausch
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # LocalCopy
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'local_copy'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install local_copy
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/local_copy/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/circle.yml ADDED
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,3 @@
1
+ class LocalCopy
2
+ VERSION = "0.0.5"
3
+ end
data/lib/local_copy.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "local_copy/version"
2
+ require 'digest'
3
+ require 'open-uri'
4
+
5
+ class LocalCopy
6
+ def self.fetch(url)
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
+ File.open(path, 'wb') do |file|
12
+ open(url) do |url|
13
+ file << url.read
14
+ end
15
+ end
16
+ end
17
+ path
18
+ end
19
+
20
+ def self.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
29
+ end
30
+
31
+ def self.local_dir
32
+ File.join('', 'tmp', 'local_copy_gem')
33
+ end
34
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'local_copy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "local_copy"
8
+ spec.version = LocalCopy::VERSION
9
+ spec.authors = ["ad2games GmbH"]
10
+ spec.email = ["developers@ad2games.com"]
11
+ spec.summary = %q{Creates a local copy of a remote file}
12
+ spec.description = %q{Creates a local copy of a remote file if it doesn't already exist}
13
+ spec.homepage = "http://ad2games.com/"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.3"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "vcr", "~> 2.9"
25
+ spec.add_development_dependency "webmock", "~> 1.18"
26
+ end
@@ -0,0 +1,84 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: get
5
+ uri: https://d1mtas6xutddcj.cloudfront.net/assets/foxface-20px-f2cc33236f614dcaa60634cb910767d8.png
6
+ body:
7
+ encoding: US-ASCII
8
+ string: ''
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ response:
17
+ status:
18
+ code: 200
19
+ message: OK
20
+ headers:
21
+ Content-Type:
22
+ - image/png
23
+ Content-Length:
24
+ - '1370'
25
+ Connection:
26
+ - keep-alive
27
+ Accept-Ranges:
28
+ - bytes
29
+ Cache-Control:
30
+ - max-age=315360000
31
+ - public
32
+ Date:
33
+ - Thu, 28 Aug 2014 20:48:08 GMT
34
+ Expires:
35
+ - Thu, 31 Dec 2037 23:55:55 GMT
36
+ Last-Modified:
37
+ - Tue, 26 Aug 2014 09:25:27 GMT
38
+ Server:
39
+ - nginx/1.4.5
40
+ Age:
41
+ - '929667'
42
+ X-Cache:
43
+ - Hit from cloudfront
44
+ Via:
45
+ - 1.1 6b49ed933929eb2e1520ea9943c5b88c.cloudfront.net (CloudFront)
46
+ X-Amz-Cf-Id:
47
+ - 1bzeNmWRV9kibvRpoEyA2Bj4UySBAAeN3P-lmeNEEl7wNNFxnVE11Q==
48
+ body:
49
+ encoding: ASCII-8BIT
50
+ string: !binary |-
51
+ iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAGXRFWHRTb2Z0
52
+ d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3hpVFh0WE1MOmNvbS5hZG9i
53
+ ZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2Vo
54
+ aUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6
55
+ bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDIxIDc5
56
+ LjE1NTc3MiwgMjAx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QgZW5kPSJyIj8+lOixGgAAAXhJREFUeNpi/P//PwNDFNdd
73
+ BgaGk0A8lWHZt6MMpIAoLk8gmQrEhkC9ioxQA/8jKdkExF1A/AGI+YGYF0r/
74
+ A+KPQPwZiD8BsTwQVwGxDVTfH6CBrCxQzikgNoOy/aCYVHAYRDBBObcYKAfn
75
+ kQ08SgUDdyMb+JxEzb+hYYoMHiAb6IAmuROI4/EYqAfELWhidsgGGiFJzAPi
76
+ cGCMLQLTDAzf0FxhAsSrgJgLzSEmyAYKoHnnN5T9Hc0V7OD0xsDwCIiZoS6F
77
+ AUEQwYJFYzoQSwHT5hIgvRLNQEkgng31UTQQT0KS+4Zs4BUgNkeS9IViXOAc
78
+ oWQzkwrJZhnCwGXfTgPJRRQYVgI04xWyC0GGxiMZ+owIQ15B6Rag3l6YIKRw
79
+ QC09FKAGLgTiCByGVQNxPxCLAw17gCyBaSDCYEUgeQ+LzE8g5gQahFUjE+4g
80
+ /nYfSN7EIrMFl2H4DYSAiVjEJuPTgNvLCK9PgYblb3BCXvatnTIDSQQAAQYA
81
+ CI1rRRHjAiQAAAAASUVORK5CYII=
82
+ http_version:
83
+ recorded_at: Mon, 08 Sep 2014 15:02:07 GMT
84
+ recorded_with: VCR 2.9.2
@@ -0,0 +1,22 @@
1
+ describe LocalCopy do
2
+ let(:url) do
3
+ 'https://d1mtas6xutddcj.cloudfront.net/assets/foxface-20px-f2cc33236f614dcaa60634cb910767d8.png'
4
+ end
5
+ let(:local_file) do
6
+ File.join('', 'tmp', 'local_copy_gem', Digest::MD5.hexdigest(url))
7
+ end
8
+
9
+ before do
10
+ FileUtils.rm_r(LocalCopy.local_dir) if File.exist?(LocalCopy.local_dir)
11
+ end
12
+
13
+ it 'downloads a remote file and returns the local path', :vcr do
14
+ expect(File.exist?(local_file)).to be(false)
15
+ described_class.flush
16
+ described_class.fetch(url)
17
+ expect(File.exist?(local_file)).to be(true)
18
+ described_class.fetch(url)
19
+ described_class.flush
20
+ expect(File.exist?(local_file)).to be(false)
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ describe LocalCopy do
2
+ let(:url) { 'http://example.com/path/to/file.xyz' }
3
+ let(:expected_path) { '/tmp/local_copy_gem/63abf0aea8b8d04c1ed49baf234fb696' }
4
+ let(:open) { double('open') }
5
+ let(:file_content) { 'foo' }
6
+ let(:file) { double('file') }
7
+
8
+ describe '.fetch' do
9
+ subject { described_class.fetch(url) }
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)
21
+ end
22
+
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)
26
+
27
+ expect(subject).to eq(expected_path)
28
+ end
29
+ end
30
+
31
+ describe '.flush' do
32
+ it 'deletes the contents of the local directory' do
33
+ expect(Dir).to receive(:entries).and_return(['.', '..', 'foo'])
34
+ expect(File).to receive(:delete).with('/tmp/local_copy_gem/foo')
35
+
36
+ expect(described_class.flush).to eq(['/tmp/local_copy_gem/foo'])
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,18 @@
1
+ require './lib/local_copy'
2
+ require 'vcr'
3
+
4
+ RSpec.configure do |config|
5
+ config.expect_with :rspec do |expectations|
6
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
7
+ end
8
+
9
+ config.mock_with :rspec do |mocks|
10
+ mocks.verify_partial_doubles = true
11
+ end
12
+ end
13
+
14
+ VCR.configure do |c|
15
+ c.cassette_library_dir = 'spec/fixtures/vcr'
16
+ c.hook_into :webmock # or :fakeweb
17
+ c.configure_rspec_metadata!
18
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: local_copy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - ad2games GmbH
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.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: '2.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.18'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.18'
83
+ description: Creates a local copy of a remote file if it doesn't already exist
84
+ email:
85
+ - developers@ad2games.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - circle.yml
97
+ - lib/local_copy.rb
98
+ - lib/local_copy/version.rb
99
+ - local_copy.gemspec
100
+ - spec/fixtures/vcr/LocalCopy/downloads_a_remote_file_and_returns_the_local_path.yml
101
+ - spec/integration/download_and_delete_spec.rb
102
+ - spec/lib/local_copy_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: http://ad2games.com/
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Creates a local copy of a remote file
128
+ test_files:
129
+ - spec/fixtures/vcr/LocalCopy/downloads_a_remote_file_and_returns_the_local_path.yml
130
+ - spec/integration/download_and_delete_spec.rb
131
+ - spec/lib/local_copy_spec.rb
132
+ - spec/spec_helper.rb