Class: Hermeneutics::Cli::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/cli/protocol.rb

Direct Known Subclasses

IMAP, POP3, SMTP

Constant Summary collapse

CRLF =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, timeout) ⇒ Protocol

Returns a new instance of Protocol.



67
68
69
# File 'lib/hermeneutics/cli/protocol.rb', line 67

def initialize socket, timeout
  @socket, @timeout = socket, timeout
end

Instance Attribute Details

#timeout=(value) ⇒ Object (writeonly)

Sets the attribute timeout

Parameters:

  • value

    the value to set the attribute timeout to.



65
66
67
# File 'lib/hermeneutics/cli/protocol.rb', line 65

def timeout=(value)
  @timeout = value
end

Class Method Details

.open(host, port, timeout: nil, ssl: false) ⇒ Object



29
30
31
32
33
34
# File 'lib/hermeneutics/cli/protocol.rb', line 29

def open host, port, timeout: nil, ssl: false
  open_socket host, port, timeout, ssl do |s|
    i = new s, timeout
    yield i
  end
end

Instance Method Details

#done?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/hermeneutics/cli/protocol.rb', line 104

def done?
  not @socket.ready?
end

#read(bytes) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/hermeneutics/cli/protocol.rb', line 96

def read bytes
  @socket.wait @timeout||0
  r = @socket.read bytes
  @trace and $stderr.puts "S- #{r.inspect}"
  r
rescue EOFError
end

#readlineObject



82
83
84
85
86
87
88
89
# File 'lib/hermeneutics/cli/protocol.rb', line 82

def readline
  @socket.wait @timeout||0
  r = @socket.readline
  r.chomp!
  @trace and $stderr.puts "S: #{r}"
  r
rescue EOFError
end

#trace!Object



71
72
73
# File 'lib/hermeneutics/cli/protocol.rb', line 71

def trace!
  @trace = true
end

#write(data) ⇒ Object



91
92
93
94
# File 'lib/hermeneutics/cli/protocol.rb', line 91

def write data
  @trace and $stderr.puts "C- #{data.inspect}"
  @socket.write data
end

#writeline(l) ⇒ Object



75
76
77
78
79
80
# File 'lib/hermeneutics/cli/protocol.rb', line 75

def writeline l
  l.chomp!
  @trace and $stderr.puts "C: #{l}"
  @socket.write l
  @socket.write self.class::CRLF ? "\r\n" : "\n"
end