Class: Hermeneutics::Dictionary
- Inherits:
-
Object
- Object
- Hermeneutics::Dictionary
- Defined in:
- lib/hermeneutics/contents.rb
Overview
A parser for header fields like DKIM-Signature
Example
ds = Dictionary.new v: 1, a: "rsa-sha256", c: "relaxed/relaxed", ...
ds = Dictionary.parse "v=1; a=rsa-sha256; c=relaxed/relaxed; ..."
ds[ "a"] #=> "0123456"
Direct Known Subclasses
Constant Summary collapse
- SEP =
:stopdoc:
";"
- SEA =
"="
- RES =
/#{SEP}\s*/
- REA =
/((?:\*(\d+))?\*)?#{SEA}/
- TSPECIAL =
:stopdoc:
%r_[()<>@,;:\\"\[\]/?=]_
Instance Attribute Summary collapse
-
#hash ⇒ Object
(also: #to_hash)
readonly
Returns the value of attribute hash.
Class Method Summary collapse
-
.parse(line) ⇒ Object
Create a
Dictionary
object out of a string from a mail header field. - .urltext ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #field)
:call-seq: []( key) -> str or nil.
- #empty? ⇒ Boolean
-
#encode ⇒ Object
Encode it for a mail header field.
-
#initialize(hash = nil) ⇒ Dictionary
constructor
Create a
Dictionary
object from a value and a hash. -
#keys ⇒ Object
:call-seq: keys() -> ary.
- #notempty? ⇒ Boolean
-
#to_s ⇒ Object
(also: #quote)
Show the line as readable text.
Constructor Details
#initialize(hash = nil) ⇒ Dictionary
Create a Dictionary
object from a value and a hash.
ds = Dictionary.new v: 1, a: "rsa-sha256",
c: "relaxed/relaxed", ...
108 109 110 111 112 113 114 115 116 |
# File 'lib/hermeneutics/contents.rb', line 108 def initialize hash = nil case hash when URLText::Dict then @hash = hash else @hash = URLText::Dict.new @hash.merge! hash if hash end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object (private)
131 132 133 134 135 136 137 |
# File 'lib/hermeneutics/contents.rb', line 131 def method_missing sym, *args if sym =~ /[^a-z_]/ or args.any? then super else field sym end end |
Instance Attribute Details
#hash ⇒ Object (readonly) Also known as: to_hash
Returns the value of attribute hash.
99 100 101 |
# File 'lib/hermeneutics/contents.rb', line 99 def hash @hash end |
Class Method Details
.parse(line) ⇒ Object
Create a Dictionary
object out of a string from a mail header field.
ds = Dictionary.parse "v=1; a=rsa-sha256; c=relaxed/relaxed; ..."
ds[ "a"] #=> "0123456"
46 47 48 49 50 |
# File 'lib/hermeneutics/contents.rb', line 46 def parse line rest = line.strip hash = parse_hash rest new hash end |
Instance Method Details
#[](key) ⇒ Object Also known as: field
:call-seq:
[]( key) -> str or nil
Find value of key
.
126 |
# File 'lib/hermeneutics/contents.rb', line 126 def [] key ; @hash[ key.to_sym] ; end |
#empty? ⇒ Boolean
118 |
# File 'lib/hermeneutics/contents.rb', line 118 def empty? ; @hash.empty? ; end |
#encode ⇒ Object
Encode it for a mail header field.
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/hermeneutics/contents.rb', line 178 def encode f, *rest = encoded_parts if f then r = [ f] rest.each { |e| r.last << SEP r.push e } end r end |
#keys ⇒ Object
149 |
# File 'lib/hermeneutics/contents.rb', line 149 def keys ; @hash.keys ; end |
#notempty? ⇒ Boolean
119 |
# File 'lib/hermeneutics/contents.rb', line 119 def notempty? ; self if @hash.notempty? ; end |
#to_s ⇒ Object Also known as: quote
Show the line as readable text.
158 159 160 |
# File 'lib/hermeneutics/contents.rb', line 158 def to_s quoted_parts.join "#{SEP} " end |