file_transfer_mixin 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/README +35 -0
- data/Rakefile +2 -0
- data/file_transfer_mixin.gemspec +26 -0
- data/lib/file_transfer_mixin/interfaces/sftp.rb +38 -0
- data/lib/file_transfer_mixin/interfaces.rb +6 -0
- data/lib/file_transfer_mixin/version.rb +3 -0
- data/lib/file_transfer_mixin.rb +17 -0
- data/spec/lib/file_transfer_mixin_spec.rb +33 -0
- data/spec/lib/interfaces/sftp_spec.rb +55 -0
- data/spec/spec_helper.rb +14 -0
- metadata +152 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.9.2@file_transfer_mixin
|
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
== FileTransferMixin ==
|
2
|
+
FileTransferMixin is a module that you can include in a library. It will support various mechanisms long-term, but
|
3
|
+
for now is focused on SFTP servers.
|
4
|
+
|
5
|
+
- sftp_send(key, remote_location, local_file_path)
|
6
|
+
- sftp_fetch(key, remote_path, local_path)
|
7
|
+
|
8
|
+
- It expects an ENV variable named FILE_TRANSFER_MIXIN_CONFIG_PATH to be set.
|
9
|
+
- It expects a yml configuration file in FILE_TRANSFER_MIXIN_CONFIG_PATH that looks like the following:
|
10
|
+
|
11
|
+
sftp:
|
12
|
+
some_key:
|
13
|
+
server: sftp.matrix.com
|
14
|
+
username: neo
|
15
|
+
password: the_one
|
16
|
+
|
17
|
+
Then in a class, you would deal with it thusly:
|
18
|
+
|
19
|
+
class SomeClass
|
20
|
+
include FileTransferMixin
|
21
|
+
|
22
|
+
# Some method that uploads a file
|
23
|
+
def some_method
|
24
|
+
sftp_send(:some_key, remote_path, local_path)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Some method that fetches a file
|
28
|
+
def fetch_method
|
29
|
+
sftp_fetch(:some_key, remote_path, local_path)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
== Motivation ==
|
34
|
+
We have quite a few libraries that interact with remote SFTP servers, and inevitably they share massive swathes of code
|
35
|
+
that should be unnecessary. This intends to be a mixin to make the easy things extremely easy.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "file_transfer_mixin/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "file_transfer_mixin"
|
7
|
+
s.version = FileTransferMixin::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Josh Adams"]
|
10
|
+
s.email = ["josh@isotope11.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A mixin to include in various libraries to allow you to easily send/retrieve files from remote servers.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "file_transfer_mixin"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency 'net-sftp', '~> 2.0.5'
|
22
|
+
s.add_dependency 'enviro', '~> 0.0.4'
|
23
|
+
s.add_development_dependency "bundler", ">= 1.0.0.rc.6"
|
24
|
+
s.add_development_dependency "rspec", "~> 2.4.0"
|
25
|
+
s.add_development_dependency "ruby-debug19"
|
26
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module FileTransferMixin
|
2
|
+
module InstanceMethods
|
3
|
+
extend Forwardable
|
4
|
+
|
5
|
+
def_delegators :file_transfer_mixin_sftp_instance, :sftp_send, :sftp_fetch
|
6
|
+
|
7
|
+
private
|
8
|
+
def file_transfer_mixin_sftp_instance
|
9
|
+
::FileTransferMixin::Interfaces::SFTP.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Interfaces
|
14
|
+
class SFTP
|
15
|
+
def configuration
|
16
|
+
FileTransferMixin.configuration.sftp
|
17
|
+
end
|
18
|
+
|
19
|
+
def sftp_block(key, &block)
|
20
|
+
Net::SFTP.start(configuration[key][:server], configuration[key][:username], :password => configuration[key][:password]) do |sftp|
|
21
|
+
yield(sftp)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def sftp_send(key, remote_path, local_path)
|
26
|
+
sftp_block(key) do |sftp|
|
27
|
+
sftp.upload!(local_path, remote_path)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def sftp_fetch(key, remote_path, local_path)
|
32
|
+
sftp_block(key) do |sftp|
|
33
|
+
sftp.download!(remote_path, local_path)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'enviro'
|
2
|
+
require 'net/sftp'
|
3
|
+
require 'forwardable'
|
4
|
+
|
5
|
+
require 'file_transfer_mixin/interfaces'
|
6
|
+
|
7
|
+
module FileTransferMixin
|
8
|
+
include ::Enviro::Environate
|
9
|
+
configuration_path_env :file_transfer_mixin_config_path
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
base.send :include, InstanceMethods
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FileTransferMixin do
|
4
|
+
before(:each) do
|
5
|
+
Object.send(:remove_const, :TestFileTransferMixin) if defined?(TestFileTransferMixin)
|
6
|
+
|
7
|
+
ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'] = '/tmp/file_transfer_mixin_enviro_config.yml'
|
8
|
+
|
9
|
+
config = {
|
10
|
+
:development => {
|
11
|
+
},
|
12
|
+
:test => {
|
13
|
+
},
|
14
|
+
:production => {
|
15
|
+
},
|
16
|
+
}
|
17
|
+
File.open(ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'], 'w') do |f|
|
18
|
+
f.write(YAML.dump(config))
|
19
|
+
end
|
20
|
+
|
21
|
+
class TestFileTransferMixin
|
22
|
+
include FileTransferMixin
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be valid" do
|
27
|
+
FileTransferMixin.should be_a(Module)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should know where to find its configuration file" do
|
31
|
+
FileTransferMixin.configuration_path.should == ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH']
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::FileTransferMixin::Interfaces::SFTP do
|
4
|
+
before(:each) do
|
5
|
+
Object.send(:remove_const, :TestFileTransferMixin) if defined?(TestFileTransferMixin)
|
6
|
+
ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'] = '/tmp/file_transfer_mixin_enviro_config.yml'
|
7
|
+
ENV['ENVY_ENV'] = 'development'
|
8
|
+
|
9
|
+
@config = {
|
10
|
+
:development => {
|
11
|
+
:sftp => {
|
12
|
+
:some_key => {
|
13
|
+
:server => '127.0.0.1',
|
14
|
+
:username => 'user',
|
15
|
+
:password => 'pass',
|
16
|
+
}
|
17
|
+
}
|
18
|
+
},
|
19
|
+
:test => {
|
20
|
+
},
|
21
|
+
:production => {
|
22
|
+
},
|
23
|
+
}
|
24
|
+
File.open(ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'], 'w') do |f|
|
25
|
+
f.write(YAML.dump(@config))
|
26
|
+
end
|
27
|
+
|
28
|
+
class TestFileTransferMixin
|
29
|
+
include FileTransferMixin
|
30
|
+
end
|
31
|
+
|
32
|
+
sftp_mock = mock('sftp')
|
33
|
+
sftp_mock.stub!(:upload!).and_return(true)
|
34
|
+
sftp_mock.stub!(:download!).and_return(true)
|
35
|
+
Net::SFTP.stub(:start).and_return(sftp_mock)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should know its parent configuration key" do
|
39
|
+
subject.configuration.should == @config[:development][:sftp]
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be able to find the server, username, and password for a given key" do
|
43
|
+
subject.configuration[:some_key][:server].should == '127.0.0.1'
|
44
|
+
subject.configuration[:some_key][:username].should == 'user'
|
45
|
+
subject.configuration[:some_key][:password].should == 'pass'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should respond to sftp_send" do
|
49
|
+
lambda{ TestFileTransferMixin.new.sftp_send(:some_key, 'path', 'file_path') }.should_not raise_error
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should respond to sftp_fetch" do
|
53
|
+
lambda{ TestFileTransferMixin.new.sftp_fetch(:some_key, 'path', 'file_path') }.should_not raise_error
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
Bundler.require :default, :test
|
3
|
+
|
4
|
+
require "rspec"
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
# Remove this line if you don't want RSpec's should and should_not
|
8
|
+
# methods or matchers
|
9
|
+
require 'rspec/expectations'
|
10
|
+
config.include RSpec::Matchers
|
11
|
+
|
12
|
+
# == Mock Framework
|
13
|
+
config.mock_with :rspec
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_transfer_mixin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Josh Adams
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-03-04 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: net-sftp
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 5
|
32
|
+
version: 2.0.5
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: enviro
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
- 0
|
46
|
+
- 4
|
47
|
+
version: 0.0.4
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: bundler
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 0
|
61
|
+
- 0
|
62
|
+
- rc
|
63
|
+
- 6
|
64
|
+
version: 1.0.0.rc.6
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
name: rspec
|
69
|
+
prerelease: false
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 2
|
77
|
+
- 4
|
78
|
+
- 0
|
79
|
+
version: 2.4.0
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id004
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: ruby-debug19
|
84
|
+
prerelease: false
|
85
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
type: :development
|
94
|
+
version_requirements: *id005
|
95
|
+
description:
|
96
|
+
email:
|
97
|
+
- josh@isotope11.com
|
98
|
+
executables: []
|
99
|
+
|
100
|
+
extensions: []
|
101
|
+
|
102
|
+
extra_rdoc_files: []
|
103
|
+
|
104
|
+
files:
|
105
|
+
- .gitignore
|
106
|
+
- .rspec
|
107
|
+
- .rvmrc
|
108
|
+
- Gemfile
|
109
|
+
- README
|
110
|
+
- Rakefile
|
111
|
+
- file_transfer_mixin.gemspec
|
112
|
+
- lib/file_transfer_mixin.rb
|
113
|
+
- lib/file_transfer_mixin/interfaces.rb
|
114
|
+
- lib/file_transfer_mixin/interfaces/sftp.rb
|
115
|
+
- lib/file_transfer_mixin/version.rb
|
116
|
+
- spec/lib/file_transfer_mixin_spec.rb
|
117
|
+
- spec/lib/interfaces/sftp_spec.rb
|
118
|
+
- spec/spec_helper.rb
|
119
|
+
has_rdoc: true
|
120
|
+
homepage: ""
|
121
|
+
licenses: []
|
122
|
+
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
version: "0"
|
144
|
+
requirements: []
|
145
|
+
|
146
|
+
rubyforge_project: file_transfer_mixin
|
147
|
+
rubygems_version: 1.3.7
|
148
|
+
signing_key:
|
149
|
+
specification_version: 3
|
150
|
+
summary: A mixin to include in various libraries to allow you to easily send/retrieve files from remote servers.
|
151
|
+
test_files: []
|
152
|
+
|