Class: Net::NNTPArticle

Inherits:
Object
  • Object
show all
Extended by:
NNTPBodyParser, NNTPHeaderParser
Defined in:
lib/net/nntp/article.rb

Constant Summary

Constants included from NNTPHeaderParser

Net::NNTPHeaderParser::HEADER_PARSE_RE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NNTPBodyParser

parse_body

Methods included from NNTPHeaderParser

parse_headers

Constructor Details

#initializeNNTPArticle

Returns a new instance of NNTPArticle


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

def initialize
  @headers = {}
end

Instance Attribute Details

#bodyString

Returns the parsed article body

Returns:

  • (String)

    the parsed article body


11
12
13
# File 'lib/net/nntp/article.rb', line 11

def body
  @body
end

#headersHash

Returns the parsed key-value article headers

Returns:

  • (Hash)

    the parsed key-value article headers


9
10
11
# File 'lib/net/nntp/article.rb', line 9

def headers
  @headers
end

Class Method Details

.parse(raw_article) ⇒ Object


13
14
15
16
17
18
19
20
# File 'lib/net/nntp/article.rb', line 13

def self.parse(raw_article)
  split = raw_article.index("\r\n\r\n")

  new.tap do |article|
    article.headers = parse_headers(raw_article[(0..split+1)])
    article.body    = parse_body(raw_article)[(split+4..-1)]
  end
end

Instance Method Details

#add_header(key, value) ⇒ void

Note:

this is equivalent Hash#[]=

This method returns an undefined value.

Add an entry to #headers


30
31
32
# File 'lib/net/nntp/article.rb', line 30

def add_header(key, value)
  headers[key] = value
end

#to_sString

Generate an NNTP article

Returns:

  • (String)

    the raw NNTP article


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/net/nntp/article.rb', line 37

def to_s
  raw = ''
  headers.each_pair { |k, v| raw << "#{k}: #{v}\r\n"}
  raw << "\r\n"

  body.each_line do |line|
    orig_line = line
    line = line.chomp

    raw << '.' if line[0] == '.' # dot stuffing
    raw << line
    # don't add a newline if there wasn't one originally
    raw << "\r\n" unless line.eql?(orig_line)
  end

  raw << "\r\n"
end