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
Constants included from BasicLogging
BasicLogging::DEBUG, BasicLogging::ERROR, BasicLogging::FATAL, BasicLogging::INFO, BasicLogging::Levels, BasicLogging::UNKNOWN, BasicLogging::WARN
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#newsgroups ⇒ Object
readonly
Returns the value of attribute newsgroups.
Attributes included from BasicLogging
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 ⇒ Object
Modify headers, if need be.
Methods included from BasicLogging
is_muted?, #log, mute, #set_level, #set_target
Constructor Details
#initialize(article_text) ⇒ Headers
read the headers from the article
27 28 29 30 31 32 33 34 35 36 37 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/headers.rb', line 27 def initialize(article_text) line = nil # transform the article to an array. debug('before split, article_text is : ' << article_text) line_array = article_text.split($LN) debug('after split, line_array is : ' << line_array.inspect) # find the first empty line end_index = line_array.index {|ele| ele.strip == ''} # keep the preceding lines. @lines = line_array.slice!(0, end_index) debug('headers: ' << @lines.to_s) # headername: headervalue @headers = {} # fill the headers Hash from the header-lines. # headers may have been line-wrapped. cur_header = nil @lines.each do |l| # has the header been wrapped? if !l.start_with?(' ') # header is all before the first colon begin cur_header = l.match(/^(.*?):/)[1].to_sym rescue Exception => ex error ("Cannot match a header in line " << l << "(" << ex. << ")") exit false; end # value is all after the first colon val = l.match(/:(.*)/)[1].strip else # a wrapped value is not devided val = l.strip end # add value to the existing if cur_header && @headers[cur_header] @headers[cur_header] += val else # or add a new value @headers[cur_header] = val end #@headers[l.match(/^(.*?):/)[1].to_sym] = l.match(/:(.*)/)[1].strip # h = l.split(':') # @headers[h[0].strip.to_sym] = h[1...h.size].join(':').strip end debug('headers are ' << @headers.to_s) @newsgroups = Newsgroups.new(header(:Newsgroups)) 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.
119 120 121 122 |
# File 'lib/headers.rb', line 119 def method_missing(method, args = nil) return @headers[method] if @headers[method] error("unknown symbol '#{method}'") end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
132 133 134 |
# File 'lib/headers.rb', line 132 def lines @lines end |
#newsgroups ⇒ Object (readonly)
Returns the value of attribute newsgroups.
132 133 134 |
# File 'lib/headers.rb', line 132 def newsgroups @newsgroups end |
Instance Method Details
#header(name) ⇒ Object
returns the value of header ‘name’
83 84 85 86 87 88 89 90 91 |
# File 'lib/headers.rb', line 83 def header(name) # name must be a symbol. if name.respond_to?(:to_sym) @headers[name] else error(name.to_s << ' is not a symbol!') nil end end |
#join ⇒ Object
return the headers as a String.
125 126 127 128 129 130 |
# File 'lib/headers.rb', line 125 def join htext = '' @headers.each_pair {|h, t| htext << h.to_s << ': ' << t << $LN } debug('joined headers: ' << htext) htext end |
#remove(name) ⇒ Object
remove a header
113 114 115 |
# File 'lib/headers.rb', line 113 def remove(name) @headers.delete(name) if @headers[name] end |
#update ⇒ Object
Modify headers, if need be.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/headers.rb', line 94 def update() xnay = @newsgroups.xnay debug('xnay should be set now : ' << xnay.to_s) if xnay @headers['X-No-Archive'] = xnay end ch = @@config.CUSTOM_HEADERS 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 debug('updated headers are ' << @headers.inspect) end |