Class: Headers
Overview
an object of this class represents the headers of a news-article
Constant Summary collapse
- @@config =
class-level configuration object
Configuration.instance
- @@log =
class-level logger as configured.
init_logger(@@config.log_target, @@config.log_level)
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#newsgroups ⇒ Object
readonly
Returns the value of attribute newsgroups.
Instance Method Summary collapse
-
#header(name) ⇒ Object
returns the value of header ‘name’.
-
#initialize(article_text) ⇒ Headers
constructor
read the headers from the article.
-
#join ⇒ Object
return the headers as a String.
-
#method_missing(method, args = nil) ⇒ Object
basically a replacement for header(name), above.
-
#remove(name) ⇒ Object
remove a header.
-
#update(article_text) ⇒ Object
Modify headers, if need be.
Methods included from Logging
init_logger, log_level=, log_target=
Constructor Details
#initialize(article_text) ⇒ Headers
read the headers from the article
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/headers.rb', line 38 def initialize(article_text) @log = @@log line = nil # transform the article to an array. line_array = article_text.split($LN) # find the first empty line end_index = line_array.index('') # keep the preceding lines. @lines = line_array.slice!(0, end_index) @log.debug('headers: ' << @lines.to_s) # headername: headervalue @headers = {} # fill the headers Hash from the header-lines. @lines.each do |l| h = l.split(':') @headers[h[0].strip.to_sym] = h[1...h.size].join(':').strip end @log.debug('headers are ' << @headers.to_s) @newsgroups = Newsgroups.new(header(:Newsgroups)) @log.debug('Newsgroups is ' << @newsgroups.inspect) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, args = nil) ⇒ Object
basically a replacement for header(name), above. But you can call self.Newsgroups or self.From etc.
97 98 99 100 |
# File 'lib/headers.rb', line 97 def method_missing(method, args = nil) return @headers[method] if @headers[method] @log.error("unknown symbol '#{method}'") end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
110 111 112 |
# File 'lib/headers.rb', line 110 def lines @lines end |
#newsgroups ⇒ Object (readonly)
Returns the value of attribute newsgroups.
110 111 112 |
# File 'lib/headers.rb', line 110 def newsgroups @newsgroups end |
Instance Method Details
#header(name) ⇒ Object
returns the value of header ‘name’
66 67 68 69 70 71 72 73 74 |
# File 'lib/headers.rb', line 66 def header(name) # name must be a symbol. if name.respond_to?(:to_sym) @headers[name] else @log.error(name.to_s << ' is not a symbol!') nil end end |
#join ⇒ Object
return the headers as a String.
103 104 105 106 107 108 |
# File 'lib/headers.rb', line 103 def join htext = '' @headers.each_pair {|h, t| htext << h.to_s << ': ' << t << $LN } @log.debug('joined headers: ' << htext) htext end |
#remove(name) ⇒ Object
remove a header
91 92 93 |
# File 'lib/headers.rb', line 91 def remove(name) @headers.delete(name) if @headers[name] end |
#update(article_text) ⇒ Object
Modify headers, if need be.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/headers.rb', line 77 def update(article_text) @headers.merge( @newsgroups.xnay) if @newsgroups.xnay ch = @@config.CUSTOM_HEADERS @log.debug('setting custom headers : ' << ch.inspect) if @@config.CUSTOM_HEADERS @@config.CUSTOM_HEADERS.each do |pair| ch = pair.split(':') @headers[ch[0].strip ] = ch[1].strip @headers.compact! end end end |