borrower 0.2.1 → 0.3.0

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.
@@ -0,0 +1,90 @@
1
+ module Borrower
2
+ class Content
3
+ class << self
4
+
5
+ # take the contents of the file at the path
6
+ # @param [String] path
7
+ # @return [String] the contents
8
+ def get path
9
+ path = Borrower.find(path)
10
+ obj = Item.new( path )
11
+
12
+ if obj.remote? && obj.exists?
13
+ return obj.body
14
+ else
15
+ return IO.read(path)
16
+ end
17
+
18
+ raise "nothing exists at the provided path '#{path}'"
19
+ end
20
+
21
+ def put content, destination, args
22
+ FileUtils.mkdir_p( File.dirname(destination) )
23
+ File.open( destination, 'w', internal_encoding: content.encoding ) do |file|
24
+ content = Borrower.merge(content) if args[0].fetch(:merge) { false }
25
+ file.write content
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ class Item
32
+ def initialize path
33
+ @path = path
34
+ end
35
+
36
+ def remote?
37
+ @_remote ||= is_remote?
38
+ end
39
+
40
+ def exists?
41
+ if remote?
42
+ return ( fetch_from_remote(@path).msg.include? "OK" )
43
+ else
44
+ return File.exists? @path
45
+ end
46
+ end
47
+
48
+ def body
49
+ @_response.body
50
+ end
51
+
52
+ private
53
+
54
+ def is_remote?
55
+ if @path.match /http[s]?:\/\//
56
+ return true
57
+ end
58
+ return false
59
+ end
60
+
61
+ def fetch_from_remote path, limit=10
62
+ return @_response unless @_response.nil?
63
+
64
+ raise Error, 'HTTP redirect too deep' if limit == 0
65
+ response = get_response(path)
66
+
67
+ case response
68
+ when Net::HTTPSuccess then @_response = response
69
+ when Net::HTTPRedirection then fetch_from_remote(response['location'], limit-1 )
70
+ else
71
+ @_response = response
72
+ end
73
+ end
74
+
75
+ def get_response path
76
+ uri = URI.parse(path)
77
+ request = Net::HTTP.new(uri.host, uri.port)
78
+
79
+ if uri.scheme == "https"
80
+ request.use_ssl = true
81
+ request.verify_mode = OpenSSL::SSL::VERIFY_NONE
82
+ request.ssl_version = :SSLv3
83
+ end
84
+
85
+ return request.get(uri.request_uri)
86
+ end
87
+
88
+ end
89
+ end
90
+ end
@@ -18,7 +18,8 @@ module Borrower
18
18
  end
19
19
 
20
20
  def find file
21
- return file if Path.remote?(file) || Path.exists?(file)
21
+ obj = Content::Item.new(file)
22
+ return file if obj.remote? || obj.exists?
22
23
 
23
24
  path = check_for_file_in_manifest_files(file) || false
24
25
  return path if path
@@ -23,7 +23,7 @@ module Borrower
23
23
  end
24
24
 
25
25
  def contents_from_file path
26
- Path.contents(path)
26
+ Content.get(path)
27
27
  end
28
28
 
29
29
  end
@@ -8,7 +8,7 @@ module Borrower
8
8
  # @param [String] from the path to the file
9
9
  # @return [String] contents of the file
10
10
  def take from
11
- Transport.take from
11
+ Borrower::Content.get from
12
12
  end
13
13
 
14
14
  # write the content to a destination file
@@ -16,8 +16,8 @@ module Borrower
16
16
  # @param [String] content content for the file
17
17
  # @param [String] to path to write contents to
18
18
  # @return [Void]
19
- def put content, to
20
- Transport.put content, to
19
+ def put content, to, *args
20
+ Borrower::Content.put content, to, args
21
21
  end
22
22
 
23
23
  # parse through the content and merge
@@ -1,3 +1,3 @@
1
1
  module Borrower
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/borrower.rb CHANGED
@@ -6,10 +6,9 @@ require "uri"
6
6
 
7
7
  # borrower
8
8
  require 'borrower/version'
9
+ require 'borrower/content'
9
10
  require 'borrower/manifest'
10
11
  require 'borrower/merge'
11
- require 'borrower/path'
12
- require 'borrower/transport'
13
12
  require 'borrower/public_api'
14
13
 
15
14
  module Borrower::DSL
@@ -31,7 +30,7 @@ module Borrower::DSL
31
30
  # @return [Void]
32
31
  def borrow path, args
33
32
  destination = args.delete(:to) { raise ArgumentError, "missing 'to:' argument" }
34
- Borrower::Transport.move path, destination, args
33
+ Borrower.put Borrower.take(path), destination , args
35
34
  end
36
35
 
37
36
  end
@@ -34,7 +34,7 @@ describe "moving files with a merge" do
34
34
  m.file 'woo.txt', File.join( TMP, 'woo.txt' )
35
35
  end
36
36
 
37
- Borrower.merge(Borrower::Transport.take(Borrower.find("multiple.txt"))).should == <<-output
37
+ Borrower.merge( Borrower.take(Borrower.find("multiple.txt"))).should == <<-output
38
38
  hello I'm merged text
39
39
  woo
40
40
 
@@ -1,48 +1,48 @@
1
- require 'spec_helper'
2
- require 'borrower/transport'
1
+ # require 'spec_helper'
2
+ # require 'borrower/transport'
3
3
 
4
- describe Borrower::Transport do
5
- include Borrower::Transport
4
+ # describe Borrower::Transport do
5
+ # include Borrower::Transport
6
6
 
7
- before :each do
8
- given_file "file.txt", "Hi I'm a file"
9
- end
7
+ # before :each do
8
+ # given_file "file.txt", "Hi I'm a file"
9
+ # end
10
10
 
11
- after :each do
12
- cleanup_tmp
13
- end
11
+ # after :each do
12
+ # cleanup_tmp
13
+ # end
14
14
 
15
- let (:local_file) { File.join( TMP, 'file.txt' ) }
16
- let (:destination_path) { File.join( TMP, 'destination/file.txt' ) }
15
+ # let (:local_file) { File.join( TMP, 'file.txt' ) }
16
+ # let (:destination_path) { File.join( TMP, 'destination/file.txt' ) }
17
17
 
18
18
 
19
- describe "#take" do
20
- it "returns the correct contents" do
21
- take( local_file ).should == "Hi I'm a file"
22
- end
23
- end
19
+ # describe "#take" do
20
+ # it "returns the correct contents" do
21
+ # take( local_file ).should == "Hi I'm a file"
22
+ # end
23
+ # end
24
24
 
25
- describe "#put" do
25
+ # describe "#put" do
26
26
 
27
- it "puts a string to a file" do
28
- put "hello", destination_path
29
- take( destination_path ).should == "hello"
30
- end
31
- end
27
+ # it "puts a string to a file" do
28
+ # put "hello", destination_path
29
+ # take( destination_path ).should == "hello"
30
+ # end
31
+ # end
32
32
 
33
- describe "#move" do
33
+ # describe "#move" do
34
34
 
35
- it "moves the file" do
36
- move( local_file, destination_path )
37
- take( destination_path ).should == "Hi I'm a file"
38
- end
35
+ # it "moves the file" do
36
+ # move( local_file, destination_path )
37
+ # take( destination_path ).should == "Hi I'm a file"
38
+ # end
39
39
 
40
- it "overwrites an existing file" do
41
- put "hello", destination_path
42
- take( destination_path ).should == "hello" # just to make sure it was written to begin with
43
- move( local_file, destination_path )
44
- take( destination_path ).should == "Hi I'm a file"
45
- end
46
- end
40
+ # it "overwrites an existing file" do
41
+ # put "hello", destination_path
42
+ # take( destination_path ).should == "hello" # just to make sure it was written to begin with
43
+ # move( local_file, destination_path )
44
+ # take( destination_path ).should == "Hi I'm a file"
45
+ # end
46
+ # end
47
47
 
48
- end
48
+ # end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: borrower
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-17 00:00:00.000000000 Z
12
+ date: 2013-07-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-ssh
@@ -35,11 +35,10 @@ executables: []
35
35
  extensions: []
36
36
  extra_rdoc_files: []
37
37
  files:
38
+ - lib/borrower/content.rb
38
39
  - lib/borrower/manifest.rb
39
40
  - lib/borrower/merge.rb
40
- - lib/borrower/path.rb
41
41
  - lib/borrower/public_api.rb
42
- - lib/borrower/transport.rb
43
42
  - lib/borrower/version.rb
44
43
  - lib/borrower.rb
45
44
  - spec/borrower/manifest_spec.rb
data/lib/borrower/path.rb DELETED
@@ -1,79 +0,0 @@
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
- path = Borrower.find(path)
10
-
11
- if remote?(path) && exists?(path)
12
- return fetch_from_remote(path).body
13
- else
14
- return IO.read(path)
15
- end
16
-
17
- raise "nothing exists at the provided path '#{path}'"
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.include? "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
@@ -1,35 +0,0 @@
1
- module Borrower
2
- module Transport
3
-
4
- # routes a file from -> to
5
- # @param [String] from
6
- # @param [String] to
7
- # @param [Hash] args
8
- def move from, to, args={}
9
- content = take(from)
10
- content = Borrower.merge(content) if args[:merge]
11
- put( content, to )
12
- end
13
-
14
- # handles taking a file
15
- # @param [String] from
16
- # @return [String]
17
- def take from
18
- Path.contents(from)
19
- end
20
-
21
- # puts a file somewhere
22
- # @param [String] content
23
- # @param [String] to
24
- def put content, to
25
- FileUtils.mkdir_p( File.dirname(to) )
26
- File.open( to, 'w', internal_encoding: content.encoding ) do |file|
27
- file.write content
28
- end
29
- end
30
-
31
- module_function :move
32
- module_function :take
33
- module_function :put
34
- end
35
- end