Class: Headers
Overview
an object of this class represents the headers of a news-article
Constant Summary
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.
Class Method Summary collapse
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
Add headers, if need be.
Methods included from BasicLogging
#clear_log, is_muted?, #level, #log, #log_name, mute, #set_level, #set_target, #target
Constructor Details
#initialize(article_text) ⇒ Headers
read the headers from the article
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/headers.rb', line 89 def initialize(article_text) @config = Configuration.instance line = nil debug 'setting the headers' @headers = Headers::headers(article_text) @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.
164 165 166 167 168 169 170 171 172 |
# File 'lib/headers.rb', line 164 def method_missing(method, args = nil) if(@headers && !@headers.empty?) return @headers[method] if @headers[method] error("unknown symbol '#{method}'") else error('no article headers set') end return nil end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
182 183 184 |
# File 'lib/headers.rb', line 182 def lines @lines end |
#newsgroups ⇒ Object (readonly)
Returns the value of attribute newsgroups.
182 183 184 |
# File 'lib/headers.rb', line 182 def newsgroups @newsgroups end |
Class Method Details
.headers(article_text) ⇒ Object
26 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 81 82 83 |
# File 'lib/headers.rb', line 26 def self.headers(article_text) # transform the article to an array. line_array = article_text.split($LN) # Ensure that all three headers are present. missing_header = ['From:', 'Newsgroups:', 'Message-ID:'].detect{|h| ! line_array.any?{|l| l.match(h) } } if(missing_header) msg = "Input does not look like a news-article, no #{missing_header.delete(':')}; aborting." STDERR.puts msg exit false end # find the first empty line end_index = line_array.index {|ele| ele.strip == ''} # keep the preceding lines. begin lines = line_array.slice!(0, end_index) rescue Exception => ex msg = 'ERROR: cannot split the input into lines: ' << self.class.name << ': ' << ex. # console STDERR.puts msg # log exit false end # 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?(/\s+/) # header is all before the first colon begin cur_header = l.match(/^(.*?):/)[1].to_sym rescue Exception => ex exit false; end # value is all after the first colon val = l.match(/:(.*)/)[1].lstrip else # start_with?(' ') # a wrapped value is not devided val = l 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 end return headers end |
.supersedes?(article_text) ⇒ Boolean
84 85 86 |
# File 'lib/headers.rb', line 84 def self::supersedes?(article_text) headers(article_text).keys.include?(:Supersedes) end |
Instance Method Details
#header(name) ⇒ Object
returns the value of header 'name'
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/headers.rb', line 101 def header(name) # name must be a symbol. if(@headers && !@headers.empty?) if name.respond_to?(:to_sym) return @headers[name] else error(name.to_s << ' is not a symbol!') end else error('no headers set') end nil end |
#join ⇒ Object
return the headers as a String.
175 176 177 178 179 180 |
# File 'lib/headers.rb', line 175 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
158 159 160 |
# File 'lib/headers.rb', line 158 def remove(name) @headers.delete(name) if @headers[name] end |
#update ⇒ Object
Add headers, if need be. Set values where necessary.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/headers.rb', line 116 def update() no_archive = @newsgroups.no_archive debug('no_archive should be set now : ' << no_archive.to_s) if no_archive @headers["X-No-Archive".to_sym] = no_archive @headers["Archive".to_sym] = 'no' end # -------> # check for altered Subject and impose the newer value alterSubject() # <---- if @config.CUSTOM_HEADERS ch = @config.CUSTOM_HEADERS debug('setting custom headers : ' << ch.inspect) @config.CUSTOM_HEADERS.each do |pair| # limit to 2 elements to include ':' in values. ch = pair.split(':', 2) # ^ here # name hn = ch[0].strip # value hv = ch[1].strip debug 'setting new header ' << hn << ': ' << hv # Ensure header is ascii only if hv.ascii_only? && hn.ascii_only? # <---------- special treatment Post-Processor ----------> hv << ' ' << PROGVERSION.to_s if hn == 'X-Post-Processor' && hv == 'flnews_post_proc' # >----------< @headers[hn.to_sym] = hv else warn "Custom header [#{hn}:#{hv}] should be ASCII only! But I try to encode it..." hv = hv.split.collect{|w| (w.ascii_only? ? w : Rfc2047.encode(w, encoding: :Q)) }.join(' ') debug ' custom header encoded' end @headers[hn.to_sym] = hv end @headers.compact! end debug('updated headers are ' << @headers.inspect) end |