fetch_it 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +15 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/fetch_it.rb +45 -0
- data/test/helper.rb +10 -0
- data/test/test_fetch_it.rb +63 -0
- metadata +72 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Matt Swasey
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
= fetch_it
|
2
|
+
|
3
|
+
Small library to fetch an asset over http and dump it to the local filesystem.
|
4
|
+
|
5
|
+
fetch_it will create directories for you if supplied path does not exist.
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
|
9
|
+
require 'fetch_it'
|
10
|
+
|
11
|
+
FetchIt.go('http://example.com/images/happy_face.png', '/home/me/images/happy_face.png')
|
12
|
+
|
13
|
+
== Copyright
|
14
|
+
|
15
|
+
Copyright (c) 2010 Matt Swasey. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "fetch_it"
|
8
|
+
gem.summary = %Q{Fetch an asset over http and save locally}
|
9
|
+
gem.description = gem.summary
|
10
|
+
gem.email = "matt.swasey@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/mig/fetch_it"
|
12
|
+
gem.authors = ["Matt Swasey"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "fetch_it #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/fetch_it.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'net/http'
|
3
|
+
require 'open-uri'
|
4
|
+
|
5
|
+
class FetchIt
|
6
|
+
def self.go(url, target)
|
7
|
+
new(url, target).get
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(url, target)
|
11
|
+
@url, @target = url, target
|
12
|
+
end
|
13
|
+
|
14
|
+
def parsed_url
|
15
|
+
URI.parse(@url)
|
16
|
+
end
|
17
|
+
|
18
|
+
def http_start(&block)
|
19
|
+
Net::HTTP.start(parsed_url.host, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def http_get
|
23
|
+
http_start {|http| http.get(parsed_url.path)}
|
24
|
+
end
|
25
|
+
|
26
|
+
def http_body
|
27
|
+
http_get.body
|
28
|
+
end
|
29
|
+
|
30
|
+
def open_file_for_writing
|
31
|
+
File.open(@target, "w+") {|f| f << http_body}
|
32
|
+
end
|
33
|
+
|
34
|
+
def make_directory_if_needed
|
35
|
+
dir = File.dirname(@target)
|
36
|
+
unless Kernel.test(?d, dir)
|
37
|
+
FileUtils.mkdir_p(dir)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def get
|
42
|
+
make_directory_if_needed
|
43
|
+
open_file_for_writing
|
44
|
+
end
|
45
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestFetchIt < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@url = "http://example.com/face.png"
|
6
|
+
@target = "/somewhere/weird/face.png"
|
7
|
+
@fetch_it = FetchIt.new(@url, @target)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_parsed_url_uses_URL_parse_on_passed_url
|
11
|
+
URI.expects(:parse).with(@url)
|
12
|
+
@fetch_it.parsed_url
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_http_start_opens_HTTP_connection_with_host
|
16
|
+
@fetch_it.stubs(:parsed_url).returns(stub(:host => "example.com"))
|
17
|
+
Net::HTTP.expects(:start).with("example.com")
|
18
|
+
@fetch_it.http_start
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_http_get_calls_get_on_http_connection_and_url_path
|
22
|
+
@fetch_it.stubs(:parsed_url).returns(stub(:host => "example.com", :path => "/face.png"))
|
23
|
+
http_connection = mock()
|
24
|
+
http_connection.expects(:get).with("/face.png")
|
25
|
+
@fetch_it.stubs(:http_start).yields(http_connection)
|
26
|
+
@fetch_it.http_get
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_http_body_calls_body_on_http_gets_return
|
30
|
+
@fetch_it.stubs(:http_get).returns(mock(:body => "DATA"))
|
31
|
+
@fetch_it.http_body
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_open_file_for_writing_takes_body_and_writes_to_target
|
35
|
+
body = mock()
|
36
|
+
@fetch_it.stubs(:http_body).returns(body)
|
37
|
+
io = mock()
|
38
|
+
io.expects(:<<).with(body)
|
39
|
+
File.expects(:open).with(@target, "w+").yields(io)
|
40
|
+
@fetch_it.open_file_for_writing
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_make_directory_if_needed_should_mkdir_if_dir_does_not_exists
|
44
|
+
Kernel.expects(:test).with(?d, "/somewhere/weird").returns(false)
|
45
|
+
FileUtils.expects(:mkdir_p).with("/somewhere/weird")
|
46
|
+
@fetch_it.make_directory_if_needed
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_make_directory_if_needed_should_not_mkdir_if_dir_exists
|
50
|
+
Kernel.expects(:test).with(?d, "/somewhere/weird").returns(true)
|
51
|
+
FileUtils.expects(:mkdir_p).with("/somewhere/weird").never
|
52
|
+
@fetch_it.make_directory_if_needed
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_go_calls_make_directory_and_open_file_for_writing
|
56
|
+
Kernel.stubs(:test).returns(true)
|
57
|
+
File.stubs(:open)
|
58
|
+
@fetch_it.expects(:make_directory_if_needed)
|
59
|
+
@fetch_it.expects(:open_file_for_writing)
|
60
|
+
FetchIt.expects(:new).with(@url, @target).returns(@fetch_it)
|
61
|
+
FetchIt.go(@url, @target)
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fetch_it
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Matt Swasey
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-11 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Fetch an asset over http and save locally
|
22
|
+
email: matt.swasey@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- lib/fetch_it.rb
|
38
|
+
- test/helper.rb
|
39
|
+
- test/test_fetch_it.rb
|
40
|
+
has_rdoc: true
|
41
|
+
homepage: http://github.com/mig/fetch_it
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options:
|
46
|
+
- --charset=UTF-8
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.6
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Fetch an asset over http and save locally
|
70
|
+
test_files:
|
71
|
+
- test/helper.rb
|
72
|
+
- test/test_fetch_it.rb
|