borrower 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ # utils
2
+ require 'fileutils'
3
+ require 'net/http'
4
+ require "net/https"
5
+ require "uri"
6
+
7
+ # borrower
8
+ require 'borrower/version'
9
+ require 'borrower/path'
10
+ require 'borrower/transport'
11
+
12
+ module Borrower::DSL
13
+
14
+ # borrow a file and put it somewhere
15
+ # call syntax:
16
+ #
17
+ # Borrower.borrow "path/to/file", to: "place/to/put/file"
18
+ #
19
+ # @param path [String]
20
+ # @param args [Hash] only to: is looked at
21
+ #
22
+ def borrow path, args
23
+ unless args.has_key? :to
24
+ raise ArgumentError, "missing 'to:' argument"
25
+ end
26
+
27
+ Borrower::Transport.move path, args[:to]
28
+ end
29
+
30
+ end
31
+ include Borrower::DSL
@@ -0,0 +1,79 @@
1
+ module Borrower
2
+ module Path
3
+ class << self
4
+
5
+ # take the contents of the file at the path
6
+ # @param path [String]
7
+ # @return [String] the contents
8
+ def contents path
9
+ if exists? path
10
+ if remote? path
11
+ fetch_from_remote(path).body
12
+ else
13
+ return IO.read(path)
14
+ end
15
+ else
16
+ raise "nothing exists at the provided path '#{path}'"
17
+ end
18
+ end
19
+
20
+ # check if the file exists for borrowing
21
+ # @param path [String]
22
+ # @return [Boolean]
23
+ def exists? path
24
+ if remote? path
25
+ return ( fetch_from_remote(path).msg == "OK" )
26
+ else
27
+ return File.exists? path
28
+ end
29
+ end
30
+
31
+ # check if the path is remote or local
32
+ # @param path [String]
33
+ # @return [Boolean]
34
+ def remote? path
35
+ pattern = /http[s]?:\/\//
36
+ if path.match(pattern)
37
+ return true
38
+ end
39
+
40
+ return false
41
+ end
42
+
43
+ # get the contents of a remote file, handling redirection
44
+ # and ssh protocols
45
+ # @param path [String]
46
+ # @param limit [Integer] (optional)
47
+ # @return [String]
48
+ def fetch_from_remote path, limit=10
49
+ raise Error, 'HTTP redirect too deep' if limit == 0
50
+ response = get_response(path)
51
+
52
+ case response
53
+ when Net::HTTPSuccess then response
54
+ when Net::HTTPRedirection then fetch_from_remote(response['location'], limit-1 )
55
+ else
56
+ response
57
+ end
58
+ end
59
+
60
+ # build a Net::HTTP request object and
61
+ # returns the response
62
+ # @param path[String]
63
+ # @return [Net::HTTPResponse]
64
+ def get_response path
65
+ uri = URI.parse(path)
66
+ request = Net::HTTP.new(uri.host, uri.port)
67
+
68
+ if uri.scheme == "https"
69
+ request.use_ssl = true
70
+ request.verify_mode = OpenSSL::SSL::VERIFY_NONE
71
+ request.ssl_version = :SSLv3
72
+ end
73
+
74
+ return request.get(uri.request_uri)
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,31 @@
1
+ module Borrower
2
+ module Transport
3
+
4
+ # routes a file from -> to
5
+ # @param from [String]
6
+ def move from, to
7
+ put( take(from), to )
8
+ end
9
+
10
+ # handles taking a file
11
+ # @param from [String]
12
+ # @return [String]
13
+ def take from
14
+ Path.contents(from)
15
+ end
16
+
17
+ # puts a file somewhere
18
+ # @param content [String]
19
+ # @param to [String]
20
+ def put content, to
21
+ FileUtils.mkdir_p( File.dirname(to) )
22
+ File.open( to, 'w' ) do |file|
23
+ file.write content
24
+ end
25
+ end
26
+
27
+ module_function :move
28
+ module_function :take
29
+ module_function :put
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Borrower
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Borrower do
4
+
5
+ describe "#borrow" do
6
+ path = File.join( Dir.pwd, 'tmp', 'file.txt' )
7
+
8
+ after(:each) do
9
+ `rm -rf #{File.dirname(path)}`
10
+ end
11
+
12
+ it "borrows local files" do
13
+ borrow File.join( Dir.pwd, 'spec/fixture', 'file.txt'), to: path
14
+ Borrower::Transport.take( path ).should == "Hi I'm a file"
15
+ end
16
+
17
+ it "borrows remote files" do
18
+ borrow "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt", to: path
19
+ Borrower::Transport.take( path ).should == "Hello I'm a file"
20
+ end
21
+
22
+ it "raises an error if missing to:" do
23
+ expect{ borrow( "foo", away: "bar" )}.to raise_error(ArgumentError)
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Borrower::Path do
4
+
5
+ describe "#remote?" do
6
+ it "realizes a remote vs. local paths" do
7
+ Borrower::Path.remote?("http://google.com").should be_true
8
+ Borrower::Path.remote?("/path/to/file.rb").should be_false
9
+ end
10
+ end
11
+
12
+ describe "#exists?" do
13
+
14
+ it "recognizes missing files" do
15
+ Borrower::Path.exists?("this/file/is/missing.rb").should be_false
16
+ Borrower::Path.exists?("http://www.google.com/this/is/missing").should be_false
17
+ end
18
+
19
+ it "recognizes existing files" do
20
+ Borrower::Path.exists?( "http://www.google.com" ).should be_true
21
+ Borrower::Path.exists?( File.join( Dir.pwd, 'spec/fixture', "file.txt" ) ).should be_true
22
+ end
23
+
24
+ end
25
+
26
+ describe "#contents" do
27
+
28
+ it "returns the content of the files" do
29
+ Borrower::Path.contents( File.join( Dir.pwd, 'spec/fixture', "file.txt" ) ).should == "Hi I'm a file"
30
+ end
31
+
32
+ it "returns the content of a remote file" do
33
+ Borrower::Path.contents( "https://gist.github.com/stevenosloan/5578606/raw/97ab1305184bdeac33472f9f1fcc1c9e278a1bb3/dummy.txt" ).should == "Hello I'm a file"
34
+ end
35
+
36
+ it "raises an error for a missing file" do
37
+ expect{ Borrower::Path.contents( "this/file/is/missing.rb" ) }.to raise_error(RuntimeError)
38
+ expect{ Borrower::Path.contents("http://www.google.com/this/is/missing") }.to raise_error(RuntimeError)
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,2 @@
1
+ require 'rspec'
2
+ require 'borrower'
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+ require 'borrower/transport'
3
+
4
+ describe Borrower::Transport do
5
+ include Borrower::Transport
6
+
7
+ describe "#take" do
8
+ it "returns the correct contents" do
9
+ take( File.join( Dir.pwd, 'spec/fixture', "file.txt" ) ).should == "Hi I'm a file"
10
+ end
11
+ end
12
+
13
+ describe "#put" do
14
+ path = File.join( Dir.pwd, 'tmp', 'file.txt' )
15
+
16
+ after(:each) do
17
+ `rm -rf #{File.dirname(path)}`
18
+ end
19
+
20
+ it "puts a string to a file" do
21
+ put "hello", path
22
+ take( path ).should == "hello"
23
+ end
24
+ end
25
+
26
+ describe "#move" do
27
+ path = File.join( Dir.pwd, 'tmp', 'file.txt' )
28
+
29
+ after(:each) do
30
+ `rm -rf #{File.dirname(path)}`
31
+ end
32
+
33
+ it "moves the file" do
34
+ move( File.join( Dir.pwd, 'spec/fixture', 'file.txt'), path )
35
+ take( path ).should == "Hi I'm a file"
36
+ end
37
+
38
+ it "overwrites an existing file" do
39
+ put "hello", path
40
+ take( path ).should == "hello" # just to make sure it was written to begin with
41
+ move( File.join( Dir.pwd, 'spec/fixture', 'file.txt'), path )
42
+ take( path ).should == "Hi I'm a file"
43
+ end
44
+ end
45
+
46
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: borrower
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Steven Sloan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: net-ssh
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.6.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.6.5
30
+ description: ! ' For borrowing little snippets of the web, or files, or really any
31
+ snippet of string. '
32
+ email:
33
+ - stevenosloan@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - lib/borrower/path.rb
39
+ - lib/borrower/transport.rb
40
+ - lib/borrower/version.rb
41
+ - lib/borrower.rb
42
+ - spec/borrower_spec.rb
43
+ - spec/path_spec.rb
44
+ - spec/spec_helper.rb
45
+ - spec/transport_spec.rb
46
+ homepage: http://github.com/stevenosloan/borrower
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.25
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: For borrowing little snippets of the web, or files, or really any snippet
70
+ of string.
71
+ test_files:
72
+ - spec/borrower_spec.rb
73
+ - spec/path_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/transport_spec.rb