Class: NNTP::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nntp/connection.rb

Overview

Handles communication with the NNTP server.

Most communication with an NNTP server happens in a back-and-forth
style.

The client sends a article to the server.  The server will respond with a status response, and sometimes with extra data. See {https://tools.ietf.org/html/rfc3977 RFC 3977} for more details.

This class handles this communication by providing two methods,
one for use when additional data is expected, and one for when it is not.

Direct Known Subclasses

SSLConnection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Object



25
26
27
# File 'lib/nntp/connection.rb', line 25

def initialize(options)
  @socket = build_socket(options)
end

Instance Attribute Details

#socketObject (readonly)

The object upon which all IO takes place.



22
23
24
# File 'lib/nntp/connection.rb', line 22

def socket
  @socket
end

#statusObject (readonly)

Returns the value of attribute status.



22
# File 'lib/nntp/connection.rb', line 22

attr_reader :socket, :status

Instance Method Details

#command(command, *args) ⇒ NNTP::Status

Sends a message to the server and collects the status response.

Parameters:

  • query (Symbol, String)

    The command to send

  • args

    Any additional parameters are passed along with the command.

Returns:



66
67
68
69
70
# File 'lib/nntp/connection.rb', line 66

def command(command, *args)
  command = form_message(command, args)
  send_message(command)
  get_status
end

#get_statusNNTP::Status

Fetch a status line from the server.

Returns:



74
75
76
77
# File 'lib/nntp/connection.rb', line 74

def get_status
  code, message = get_line.split(' ', 2)
  @status = status_factory(code.to_i, message)
end

#query(query, *args) {|status, data| ... } ⇒ Hash

Sends a message to the server, collects the the status and additional data, if successful. A Hash is returned containing two keys: :status and :data. :status is an Status, and :data is an array containing the lines from the response See example for details.

Examples:

nntp.query(:list) do |status, data|
  $stdout.puts status
  data.each? do |line|
    $stdout.puts line
  end
end

=> 215 Information follows
=> alt.bin.foo
=> alt.bin.bar

Parameters:

  • query (Symbol, String)

    The command to send

  • args

    Any additional parameters are passed along with the command.

Yields:

  • The status and data from the server.

Yield Parameters:

  • status (NNTP::Status)
  • data (Array<String>, nil)

    An array with the lines from the server. Nil if the query failed.

Returns:

  • (Hash)

    The status and requested data.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/nntp/connection.rb', line 50

def query(query, *args)
  command = form_message(query, args)
  send_message(command)
  status = get_status
  data = if (400..599).include? status.code
    nil
  else
    get_block_data
  end
  yield status, data if block_given?
  {:status => status, :data => data}
end

#quitvoid

This method returns an undefined value.

Sends “QUIT\r\n” to the server, disconnects the socket.



81
82
83
84
# File 'lib/nntp/connection.rb', line 81

def quit
  command(:quit)
  socket.close
end