Class: Redwood::MBox::SSHLoader

Inherits:
Source show all
Defined in:
lib/sup/mbox/ssh-loader.rb

Instance Attribute Summary collapse

Attributes inherited from Source

#cur_offset, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Source

#==, #check, #done?, #each, #file_path, #is_source_for?, #reset!, #seek_to!, #usual

Constructor Details

#initialize(uri, username = nil, password = nil, start_offset = nil, usual = true, archived = false, id = nil, labels = []) ⇒ SSHLoader

Returns a new instance of SSHLoader.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sup/mbox/ssh-loader.rb', line 12

def initialize uri, username=nil, password=nil, start_offset=nil, usual=true, archived=false, id=nil, labels=[]
  raise ArgumentError, "not an mbox+ssh uri: #{uri.inspect}" unless uri =~ %r!^mbox\+ssh://!

  super uri, start_offset, usual, archived, id

  @parsed_uri = URI(uri)
  @username = username
  @password = password
  @uri = uri
  @cur_offset = start_offset
  @labels = (labels || []).freeze

  opts = {}
  opts[:username] = @username if @username
  opts[:password] = @password if @password
  
  @f = SSHFile.new host, filename, opts
  @loader = Loader.new @f, start_offset, usual, archived, id
  
  ## heuristic: use the filename as a label, unless the file
  ## has a path that probably represents an inbox.
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/sup/mbox/ssh-loader.rb', line 7

def password
  @password
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/sup/mbox/ssh-loader.rb', line 7

def username
  @username
end

Class Method Details

.suggest_labels_for(path) ⇒ Object



35
# File 'lib/sup/mbox/ssh-loader.rb', line 35

def self.suggest_labels_for path; Loader.suggest_labels_for(path) end

Instance Method Details

#connectObject



37
# File 'lib/sup/mbox/ssh-loader.rb', line 37

def connect; safely { @f.connect }; end

#cur_offset=(o) ⇒ Object



53
# File 'lib/sup/mbox/ssh-loader.rb', line 53

def cur_offset= o; @cur_offset = @loader.cur_offset = o; @dirty = true; end

#end_offsetObject



49
50
51
# File 'lib/sup/mbox/ssh-loader.rb', line 49

def end_offset
  safely { @f.size }
end

#filenameObject



39
# File 'lib/sup/mbox/ssh-loader.rb', line 39

def filename; @parsed_uri.path[1..-1] end

#hostObject



38
# File 'lib/sup/mbox/ssh-loader.rb', line 38

def host; @parsed_uri.host; end

#idObject



54
# File 'lib/sup/mbox/ssh-loader.rb', line 54

def id; @loader.id; end

#id=(o) ⇒ Object



55
# File 'lib/sup/mbox/ssh-loader.rb', line 55

def id= o; @id = @loader.id = o; end

#nextObject



41
42
43
44
45
46
47
# File 'lib/sup/mbox/ssh-loader.rb', line 41

def next
  safely do
    offset, labels = @loader.next
    self.cur_offset = @loader.cur_offset # superclass keeps @cur_offset which is used by yaml
    [offset, (labels + @labels).uniq] # add our labels
  end
end

#safelyObject



59
60
61
62
63
64
65
66
# File 'lib/sup/mbox/ssh-loader.rb', line 59

def safely
  begin
    yield
  rescue Net::SSH::Exception, SocketError, SSHFileError, SystemCallError, IOError => e
    m = "error communicating with SSH server #{host} (#{e.class.name}): #{e.message}"
    raise FatalSourceError, m
  end
end

#to_sObject

def cur_offset; @loader.cur_offset; end # think we’ll be ok without this



57
# File 'lib/sup/mbox/ssh-loader.rb', line 57

def to_s; @parsed_uri.to_s; end