remote-session 0.0.3 → 0.0.4

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,51 @@
1
+ module Remote
2
+
3
+ class Session
4
+
5
+ class Send
6
+
7
+ attr_accessor :remote_path
8
+ attr_accessor :chunk_size
9
+
10
+ def initialize( remote_path )
11
+ @remote_path = remote_path
12
+ @chunk_size = 1024
13
+ @file = nil
14
+ end
15
+
16
+ def read
17
+ open if ! open?
18
+
19
+ @file.read( @chunk_size )
20
+ end
21
+
22
+ def eof?
23
+ return true if ! open?
24
+ @file.eof?
25
+ end
26
+
27
+ def open?
28
+ ! @file.nil?
29
+ end
30
+
31
+ def open
32
+ if open?
33
+ @file.rewind
34
+ return
35
+ end
36
+
37
+ _open
38
+ end
39
+
40
+ def close
41
+ return if ! open?
42
+ @file.close
43
+ @file = nil
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
@@ -2,42 +2,18 @@ module Remote
2
2
 
3
3
  class Session
4
4
 
5
- class SendFile
5
+ class SendFile < Send
6
6
 
7
7
  attr_accessor :local_path
8
- attr_accessor :remote_path
9
- attr_accessor :chunk_size
10
8
 
11
9
  def initialize( local_path, remote_path )
12
10
  @local_path = local_path
13
- @remote_path = remote_path
14
- @chunk_size = 1024
15
- @file = nil
11
+ super( remote_path )
16
12
  end
17
13
 
18
- def open?
19
- ! @file.nil?
20
- end
21
-
22
- def eof?
23
- return true if ! open?
24
- @file.eof?
25
- end
26
-
27
- def read
28
- open if ! open?
29
-
30
- @file.read( @chunk_size )
31
- end
32
-
33
- def close
34
- return if ! open?
35
- @file.close
36
- @file = nil
37
- end
14
+ private
38
15
 
39
- def open
40
- close if open?
16
+ def _open
41
17
  @file = File.open( @local_path, 'r' )
42
18
  end
43
19
 
@@ -0,0 +1,27 @@
1
+ require 'stringio'
2
+
3
+ module Remote
4
+
5
+ class Session
6
+
7
+ class SendString < Send
8
+
9
+ attr_accessor :string
10
+
11
+ def initialize( string, remote_path )
12
+ @string = string
13
+ super( remote_path )
14
+ end
15
+
16
+ private
17
+
18
+ def _open
19
+ @file = StringIO.new( @string )
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
@@ -1,6 +1,9 @@
1
1
  module Remote
2
2
  class Session
3
- VERSION = "0.0.3"
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ REVISION = 4
6
+ VERSION = [ MAJOR, MINOR, REVISION ].map( &:to_s ).join( '.' )
4
7
  end
5
8
  end
6
9
 
@@ -1,4 +1,6 @@
1
+ require 'remote/session/send'
1
2
  require 'remote/session/send_file'
3
+ require 'remote/session/send_string'
2
4
  require 'remote/session/version'
3
5
  require 'net/sftp'
4
6
  require 'net/ssh'
@@ -45,7 +47,7 @@ module Remote
45
47
 
46
48
  def sudo( commands )
47
49
  raise "Session is closed" if @session.nil?
48
- commands = [ *commands ]
50
+ commands = [ *commands ] + [ 'exit' ]
49
51
 
50
52
  @session.open_channel do |ch|
51
53
  ch.request_pty do |ch, success|
@@ -113,13 +115,10 @@ module Remote
113
115
 
114
116
  $stdout.write( data )
115
117
 
116
- if ch[ :commands ].size == 0
117
- ch.send_data "exit\n"
118
- return
119
- end
118
+ return if ch[ :commands ].size == 0
120
119
 
121
120
  command = ch[ :commands ].shift
122
- if command.is_a?( Remote::Session::SendFile )
121
+ if command.is_a?( Remote::Session::Send )
123
122
  send_file_chunk( ch, command )
124
123
  else
125
124
  ch.send_data "#{command}\n"
@@ -5,7 +5,7 @@ describe Remote::Session::SendFile do
5
5
 
6
6
  context '#initialize' do
7
7
 
8
- it 'should require two parameters'do
8
+ it 'should require two parameters' do
9
9
  expect do
10
10
  Remote::Session::SendFile.new( 'foo' )
11
11
  end.to raise_error( ArgumentError, 'wrong number of arguments (1 for 2)' )
@@ -18,16 +18,12 @@ describe Remote::Session::SendFile do
18
18
 
19
19
  specify( 'local_path' ) { subject.local_path.should == 'foo' }
20
20
  specify( 'remote_path' ) { subject.remote_path.should == 'bar' }
21
- specify( 'chunk_size' ) { subject.chunk_size.should == 1024 }
21
+ specify( 'chunk_size' ) { subject.chunk_size.should == 1024 }
22
22
  end
23
23
 
24
24
  context 'instance_methods' do
25
25
  subject { Remote::Session::SendFile.new( '/local/path', '/remote/path' ) }
26
26
 
27
- context '#open?' do
28
- specify { subject.open?.should be_false }
29
- end
30
-
31
27
  context '#eof?' do
32
28
  specify { subject.eof?.should be_true }
33
29
 
@@ -42,6 +38,18 @@ describe Remote::Session::SendFile do
42
38
  end
43
39
  end
44
40
 
41
+ context '#open' do
42
+ it 'should rewind the file, if already open' do
43
+ @file = stub( 'file', :eof? => false )
44
+ File.should_receive( :open ).with( '/local/path', 'r' ).and_return( @file )
45
+ @file.should_receive( :rewind )
46
+
47
+ subject.open
48
+
49
+ subject.open
50
+ end
51
+ end
52
+
45
53
  context '#close' do
46
54
 
47
55
  before :each do
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ load File.expand_path( '../spec_helper.rb', File.dirname(__FILE__) )
3
+
4
+ describe Remote::Session::Send do
5
+
6
+ context '#initialize' do
7
+
8
+ it 'should require one parameter'do
9
+ expect do
10
+ Remote::Session::Send.new( 'foo', 'bar' )
11
+ end.to raise_error( ArgumentError, 'wrong number of arguments (2 for 1)' )
12
+ end
13
+
14
+ end
15
+
16
+ context 'attributes' do
17
+ subject { Remote::Session::Send.new( 'foo' ) }
18
+
19
+ specify( 'remote_path' ) { subject.remote_path.should == 'foo' }
20
+ specify( 'chunk_size' ) { subject.chunk_size.should == 1024 }
21
+ end
22
+
23
+ context 'instance_methods' do
24
+ subject { Remote::Session::Send.new( '/remote/path' ) }
25
+
26
+ context '#open?' do
27
+ specify { subject.open?.should be_false }
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ load File.expand_path( '../spec_helper.rb', File.dirname(__FILE__) )
3
+
4
+ describe Remote::Session::SendString do
5
+
6
+ context 'attributes' do
7
+ subject { Remote::Session::SendString.new( 'foo', 'bar' ) }
8
+
9
+ specify( 'string' ) { subject.string.should == 'foo' }
10
+ specify( 'remote_path' ) { subject.remote_path.should == 'bar' }
11
+ specify( 'chunk_size' ) { subject.chunk_size.should == 1024 }
12
+ end
13
+
14
+ context 'instance_methods' do
15
+
16
+ subject { Remote::Session::SendString.new( 'foo', 'remote/path' ) }
17
+
18
+ context '#open' do
19
+
20
+ it 'should instantiate a StringIO' do
21
+ @stringio = stub( 'stringio' )
22
+ StringIO.should_receive( :new ).with( 'foo' ).and_return( @stringio )
23
+
24
+ subject.open
25
+ end
26
+
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
@@ -288,7 +288,7 @@ describe Remote::Session do
288
288
  block.call( @ch, 'root_prompt#' )
289
289
  end
290
290
 
291
- @sf.should_receive( :is_a? ).with( Remote::Session::SendFile ).twice.and_return( true )
291
+ @sf.should_receive( :is_a? ).with( Remote::Session::Send ).twice.and_return( true )
292
292
  @sf.should_receive( :remote_path ).twice.and_return( '/remote/path' )
293
293
 
294
294
  chunk = 0
@@ -338,7 +338,7 @@ describe Remote::Session do
338
338
  block.call( @ch, 'root_prompt#' )
339
339
  end
340
340
 
341
- @sf.stub!( :is_a? ).with( Remote::Session::SendFile ).and_return( true )
341
+ @sf.stub!( :is_a? ).with( Remote::Session::Send ).and_return( true )
342
342
  @sf.stub!( :open? => false )
343
343
  @sf.stub!( :remote_path ).and_return( '/remote/path' )
344
344
  @sf.stub!( :eof? => true )
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remote-session
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joe Yates
@@ -110,12 +110,16 @@ files:
110
110
  - README.md
111
111
  - Rakefile
112
112
  - lib/remote/session.rb
113
+ - lib/remote/session/send.rb
113
114
  - lib/remote/session/send_file.rb
115
+ - lib/remote/session/send_string.rb
114
116
  - lib/remote/session/version.rb
115
117
  - remote-session.gemspec
116
118
  - spec/gather_rspec_coverage.rb
117
119
  - spec/spec_helper.rb
118
120
  - spec/unit/send_file_spec.rb
121
+ - spec/unit/send_spec.rb
122
+ - spec/unit/send_string_spec.rb
119
123
  - spec/unit/session_spec.rb
120
124
  homepage: ""
121
125
  licenses: []
@@ -154,4 +158,6 @@ test_files:
154
158
  - spec/gather_rspec_coverage.rb
155
159
  - spec/spec_helper.rb
156
160
  - spec/unit/send_file_spec.rb
161
+ - spec/unit/send_spec.rb
162
+ - spec/unit/send_string_spec.rb
157
163
  - spec/unit/session_spec.rb