file_transfer_mixin 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,88 @@
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 and FTP servers.
4
+
5
+ It provides the following methods for now:
6
+
7
+ - sftp_send(key, remote_location, local_file_path)
8
+ - sftp_fetch(key, remote_path, local_path)
9
+ - sftp_move(key, original_remote_path, new_remote_path)
10
+ - sftp_block(key)
11
+ - ftp_send(key, remote_location, local_file_path)
12
+ - ftp_fetch(key, remote_path, local_path)
13
+ - ftp_move(key, original_remote_path, new_remote_path)
14
+ - ftp_block(key)
15
+
16
+ It expects an ENV variable named FILE_TRANSFER_MIXIN_CONFIG_PATH to be set.
17
+ It expects a yml configuration file in FILE_TRANSFER_MIXIN_CONFIG_PATH that looks like the following:
18
+
19
+ :development:
20
+ :sftp:
21
+ :some_key:
22
+ :server: 127.0.0.1
23
+ :username: user
24
+ :password: pass
25
+ :ftp:
26
+ :some_key:
27
+ :server: 127.0.0.1
28
+ :username: user
29
+ :password: pass
30
+
31
+ :test: {}
32
+
33
+ :production: {}
34
+
35
+ Then in a class, you would deal with it thusly:
36
+
37
+ class SomeClass
38
+ include FileTransferMixin
39
+
40
+ # Some method that uploads a file via sftp
41
+ def some_method
42
+ sftp_send(:some_key, remote_path, local_path)
43
+ end
44
+
45
+ # Some method that fetches a file via sftp
46
+ def fetch_method
47
+ sftp_fetch(:some_key, remote_path, local_path)
48
+ end
49
+
50
+ # Some method that moves a file via sftp
51
+ def move_method
52
+ sftp_move(:some_key, original_remote_path, new_remote_path)
53
+ end
54
+
55
+ # Some method that otherwise uses Net::SFTP commands but still uses our config block
56
+ def sftp_detailed_method
57
+ sftp_block(:some_key) do |sftp|
58
+ sftp.rename!('foo', 'bar')
59
+ end
60
+ end
61
+
62
+ # Some method that uploads a file via ftp
63
+ def some_method
64
+ ftp_send(:some_key, remote_path, local_path)
65
+ end
66
+
67
+ # Some method that fetches a file via ftp
68
+ def fetch_method
69
+ ftp_fetch(:some_key, remote_path, local_path)
70
+ end
71
+
72
+ # Some method that moves a file via ftp
73
+ def move_method
74
+ ftp_move(:some_key, original_remote_path, new_remote_path)
75
+ end
76
+
77
+ # Some method that otherwise uses Net::FTP commands but still uses our config block
78
+ def ftp_detailed_method
79
+ ftp_block(:some_key) do |ftp|
80
+ puts "foo's last modification time: #{ftp.mtime('foo')}"
81
+ end
82
+ end
83
+ end
84
+
85
+ ## Motivation ##
86
+ We have quite a few libraries that interact with remote SFTP and FTP servers, and inevitably they share massive swathes of code
87
+ that should be unnecessary. This intends to be a mixin to make the easy things extremely easy.
88
+
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
8
+
9
+ desc "Run specs"
10
+ RSpec::Core::RakeTask.new
@@ -19,7 +19,8 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
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"
22
+ s.add_dependency 'enviro', '~> 0.0.5'
23
+ s.add_development_dependency "bundler", ">= 1.0.0"
24
24
  s.add_development_dependency "rspec", "~> 2.4.0"
25
+ s.add_development_dependency "ruby-debug19"
25
26
  end
@@ -1,5 +1,6 @@
1
1
  require 'enviro'
2
2
  require 'net/sftp'
3
+ require 'net/ftp'
3
4
  require 'forwardable'
4
5
 
5
6
  require 'file_transfer_mixin/interfaces'
@@ -1,4 +1,5 @@
1
1
  require 'file_transfer_mixin/interfaces/sftp'
2
+ require 'file_transfer_mixin/interfaces/ftp'
2
3
 
3
4
  module FileTransferMixin
4
5
  module Interfaces
@@ -0,0 +1,52 @@
1
+ module FileTransferMixin
2
+ module InstanceMethods
3
+ extend Forwardable
4
+
5
+ def_delegators :file_transfer_mixin_ftp_instance, :ftp_send, :ftp_fetch, :ftp_move, :ftp_block
6
+
7
+ private
8
+ def file_transfer_mixin_ftp_instance
9
+ ::FileTransferMixin::Interfaces::FTP.new
10
+ end
11
+ end
12
+
13
+ module Interfaces
14
+ class FTP
15
+ def configuration
16
+ FileTransferMixin.configuration.ftp
17
+ end
18
+
19
+ def ftp_block(key, &block)
20
+ if perform_network_operations?
21
+ c = configuration[key]
22
+ Net::FTP.open(c[:server]) do |ftp|
23
+ ftp.login(c[:username], c[:password])
24
+ yield(ftp)
25
+ end
26
+ end
27
+ end
28
+
29
+ def ftp_send(key, remote_path, local_path)
30
+ perform :putbinaryfile, key, local_path, remote_path
31
+ end
32
+
33
+ def ftp_fetch(key, remote_path, local_path)
34
+ perform :getbinaryfile, key, remote_path, local_path
35
+ end
36
+
37
+ def ftp_move(key, original_remote_path, new_remote_path)
38
+ perform :rename, key, original_remote_path, new_remote_path
39
+ end
40
+
41
+ def perform(action, key, *args)
42
+ ftp_block(key) do |ftp|
43
+ ftp.send(action, *args)
44
+ end
45
+ end
46
+
47
+ def perform_network_operations?
48
+ FileTransferMixin.env.to_s != 'test'
49
+ end
50
+ end
51
+ end
52
+ end
@@ -18,7 +18,8 @@ module FileTransferMixin
18
18
 
19
19
  def sftp_block(key, &block)
20
20
  if perform_network_operations?
21
- Net::SFTP.start(configuration[key][:server], configuration[key][:username], :password => configuration[key][:password]) do |sftp|
21
+ c = configuration[key]
22
+ Net::SFTP.start(c[:server], c[:username], :password => c[:password]) do |sftp|
22
23
  yield(sftp)
23
24
  end
24
25
  end
@@ -38,7 +39,7 @@ module FileTransferMixin
38
39
 
39
40
  def perform(action, key, *args)
40
41
  sftp_block(key) do |sftp|
41
- sftp.send(action, args)
42
+ sftp.send(action, *args)
42
43
  end
43
44
  end
44
45
 
@@ -1,3 +1,3 @@
1
1
  module FileTransferMixin
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe ::FileTransferMixin::Interfaces::FTP do
4
+
5
+ it "should know its parent configuration key" do
6
+ subject.configuration.should == @config[:development][:ftp]
7
+ end
8
+
9
+ it "should be able to find the server, username, and password for a given key" do
10
+ subject.configuration[:some_key][:server].should == '127.0.0.1'
11
+ subject.configuration[:some_key][:username].should == 'user'
12
+ subject.configuration[:some_key][:password].should == 'pass'
13
+ end
14
+
15
+ describe "network operations" do
16
+ before(:each) do
17
+ ftp_mock = mock('ftp')
18
+ ftp_mock.stub!(:put_binaryfile).and_return(true)
19
+ ftp_mock.stub!(:get_binaryfile).and_return(true)
20
+ ftp_mock.stub!(:login).and_return(true)
21
+ Net::FTP.stub(:open).and_return(ftp_mock)
22
+ @ftp_interface = TestFileTransferMixin.new
23
+ @ftp_interface.stub!(:perform_network_operations?).and_return(true)
24
+ end
25
+
26
+ it "should respond to ftp_send" do
27
+ lambda{ @ftp_interface.ftp_send(:some_key, 'path', 'file_path') }.should_not raise_error
28
+ end
29
+
30
+ it "should respond to ftp_fetch" do
31
+ lambda{ @ftp_interface.ftp_fetch(:some_key, 'path', 'file_path') }.should_not raise_error
32
+ end
33
+ end
34
+
35
+ it "should not perform network operations in the test environment" do
36
+ FileTransferMixin.stub!(:env).and_return('test')
37
+ TestFileTransferMixin::Interfaces::SFTP.new.perform_network_operations?.should == false
38
+ end
39
+ end
@@ -1,34 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
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
- end
32
4
 
33
5
  it "should know its parent configuration key" do
34
6
  subject.configuration.should == @config[:development][:sftp]
data/spec/spec_helper.rb CHANGED
@@ -11,4 +11,39 @@ RSpec.configure do |config|
11
11
 
12
12
  # == Mock Framework
13
13
  config.mock_with :rspec
14
+
15
+ config.before(:each) do
16
+ ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'] = '/tmp/file_transfer_mixin_enviro_config.yml'
17
+ ENV['ENVY_ENV'] = 'development'
18
+
19
+ @config = {
20
+ :development => {
21
+ :sftp => {
22
+ :some_key => {
23
+ :server => '127.0.0.1',
24
+ :username => 'user',
25
+ :password => 'pass',
26
+ }
27
+ },
28
+ :ftp => {
29
+ :some_key => {
30
+ :server => '127.0.0.1',
31
+ :username => 'user',
32
+ :password => 'pass',
33
+ }
34
+ },
35
+ },
36
+ :test => {
37
+ },
38
+ :production => {
39
+ },
40
+ }
41
+ File.open(ENV['FILE_TRANSFER_MIXIN_CONFIG_PATH'], 'w') do |f|
42
+ f.write(YAML.dump(@config))
43
+ end
44
+ Object.send(:remove_const, :TestFileTransferMixin) if defined?(TestFileTransferMixin)
45
+ class TestFileTransferMixin
46
+ include FileTransferMixin
47
+ end
48
+ end
14
49
  end
metadata CHANGED
@@ -1,139 +1,121 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: file_transfer_mixin
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 5
9
- version: 0.0.5
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Josh Adams
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-03-08 00:00:00 -06:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2011-11-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: net-sftp
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &12474640 !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
18
+ requirements:
26
19
  - - ~>
27
- - !ruby/object:Gem::Version
28
- segments:
29
- - 2
30
- - 0
31
- - 5
20
+ - !ruby/object:Gem::Version
32
21
  version: 2.0.5
33
22
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: enviro
37
23
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: *12474640
25
+ - !ruby/object:Gem::Dependency
26
+ name: enviro
27
+ requirement: &12474140 !ruby/object:Gem::Requirement
39
28
  none: false
40
- requirements:
29
+ requirements:
41
30
  - - ~>
42
- - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
- - 0
46
- - 4
47
- version: 0.0.4
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.5
48
33
  type: :runtime
49
- version_requirements: *id002
50
- - !ruby/object:Gem::Dependency
51
- name: bundler
52
34
  prerelease: false
53
- requirement: &id003 !ruby/object:Gem::Requirement
35
+ version_requirements: *12474140
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &12473680 !ruby/object:Gem::Requirement
54
39
  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
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 1.0.0
65
44
  type: :development
66
- version_requirements: *id003
67
- - !ruby/object:Gem::Dependency
68
- name: rspec
69
45
  prerelease: false
70
- requirement: &id004 !ruby/object:Gem::Requirement
46
+ version_requirements: *12473680
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &12473220 !ruby/object:Gem::Requirement
71
50
  none: false
72
- requirements:
51
+ requirements:
73
52
  - - ~>
74
- - !ruby/object:Gem::Version
75
- segments:
76
- - 2
77
- - 4
78
- - 0
53
+ - !ruby/object:Gem::Version
79
54
  version: 2.4.0
80
55
  type: :development
81
- version_requirements: *id004
56
+ prerelease: false
57
+ version_requirements: *12473220
58
+ - !ruby/object:Gem::Dependency
59
+ name: ruby-debug19
60
+ requirement: &12472840 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *12472840
82
69
  description:
83
- email:
70
+ email:
84
71
  - josh@isotope11.com
85
72
  executables: []
86
-
87
73
  extensions: []
88
-
89
74
  extra_rdoc_files: []
90
-
91
- files:
75
+ files:
92
76
  - .gitignore
93
77
  - .rspec
94
78
  - .rvmrc
95
79
  - Gemfile
96
- - README
80
+ - README.md
97
81
  - Rakefile
98
82
  - file_transfer_mixin.gemspec
99
83
  - lib/file_transfer_mixin.rb
100
84
  - lib/file_transfer_mixin/interfaces.rb
85
+ - lib/file_transfer_mixin/interfaces/ftp.rb
101
86
  - lib/file_transfer_mixin/interfaces/sftp.rb
102
87
  - lib/file_transfer_mixin/version.rb
103
88
  - spec/lib/file_transfer_mixin_spec.rb
89
+ - spec/lib/interfaces/ftp_spec.rb
104
90
  - spec/lib/interfaces/sftp_spec.rb
105
91
  - spec/spec_helper.rb
106
- has_rdoc: true
107
- homepage: ""
92
+ homepage: ''
108
93
  licenses: []
109
-
110
94
  post_install_message:
111
95
  rdoc_options: []
112
-
113
- require_paths:
96
+ require_paths:
114
97
  - lib
115
- required_ruby_version: !ruby/object:Gem::Requirement
98
+ required_ruby_version: !ruby/object:Gem::Requirement
116
99
  none: false
117
- requirements:
118
- - - ">="
119
- - !ruby/object:Gem::Version
120
- segments:
121
- - 0
122
- version: "0"
123
- required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
105
  none: false
125
- requirements:
126
- - - ">="
127
- - !ruby/object:Gem::Version
128
- segments:
129
- - 0
130
- version: "0"
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
131
110
  requirements: []
132
-
133
111
  rubyforge_project: file_transfer_mixin
134
- rubygems_version: 1.3.7
112
+ rubygems_version: 1.8.6
135
113
  signing_key:
136
114
  specification_version: 3
137
- summary: A mixin to include in various libraries to allow you to easily send/retrieve files from remote servers.
138
- test_files: []
139
-
115
+ summary: A mixin to include in various libraries to allow you to easily send/retrieve
116
+ files from remote servers.
117
+ test_files:
118
+ - spec/lib/file_transfer_mixin_spec.rb
119
+ - spec/lib/interfaces/ftp_spec.rb
120
+ - spec/lib/interfaces/sftp_spec.rb
121
+ - spec/spec_helper.rb
data/README DELETED
@@ -1,55 +0,0 @@
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
- It provides the following methods for now:
6
-
7
- - sftp_send(key, remote_location, local_file_path)
8
- - sftp_fetch(key, remote_path, local_path)
9
- - sftp_move(key, original_remote_path, new_remote_path)
10
- - sftp_block(key)
11
-
12
- - It expects an ENV variable named FILE_TRANSFER_MIXIN_CONFIG_PATH to be set.
13
- - It expects a yml configuration file in FILE_TRANSFER_MIXIN_CONFIG_PATH that looks like the following:
14
-
15
- :development:
16
- :sftp:
17
- :some_key:
18
- :server: 127.0.0.1
19
- :username: user
20
- :password: pass
21
- :test: {}
22
-
23
- :production: {}
24
-
25
- Then in a class, you would deal with it thusly:
26
-
27
- class SomeClass
28
- include FileTransferMixin
29
-
30
- # Some method that uploads a file
31
- def some_method
32
- sftp_send(:some_key, remote_path, local_path)
33
- end
34
-
35
- # Some method that fetches a file
36
- def fetch_method
37
- sftp_fetch(:some_key, remote_path, local_path)
38
- end
39
-
40
- # Some method that moves a file
41
- def move_method
42
- sftp_move(:some_key, original_remote_path, new_remote_path)
43
- end
44
-
45
- # Some method that otherwise uses Net::SFTP commands but still uses our config block
46
- def sftp_detailed_method
47
- sftp_block(:some_key) do |ftp|
48
- ftp.rename!('foo', 'bar')
49
- end
50
- end
51
- end
52
-
53
- == Motivation ==
54
- We have quite a few libraries that interact with remote SFTP servers, and inevitably they share massive swathes of code
55
- that should be unnecessary. This intends to be a mixin to make the easy things extremely easy.