Class: Hermeneutics::Dictionary

Inherits:
Object
  • Object
show all
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

Contents

Constant Summary collapse

SEP =

:stopdoc:

";"
SEA =
"="
RES =
/#{SEP}\s*/
REA =
/((?:\*(\d+))?\*)?#{SEA}/
TSPECIAL =

:stopdoc:

%r_[()<>@,;:\\"\[\]/?=]_

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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



126
127
128
129
130
131
132
# File 'lib/hermeneutics/contents.rb', line 126

def method_missing sym, *args
  if sym =~ /[^a-z_]/ or args.any? then
    super
  else
    field sym
  end
end

Instance Attribute Details

#hashObject (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

.urltextObject



52
53
54
# File 'lib/hermeneutics/contents.rb', line 52

def urltext
  @urltext ||= URLText.new mask_space: true
end

Instance Method Details

#[](key) ⇒ Object Also known as: field

:call-seq:

[]( key)      -> str or nil

Find value of key.



123
# File 'lib/hermeneutics/contents.rb', line 123

def [] key ; @hash[ key.to_sym] ; end

#encodeObject

Encode it for a mail header field.



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/hermeneutics/contents.rb', line 171

def encode
  f, *rest = encoded_parts
  if f then
    r = [ f]
    rest.each { |e|
      r.last << SEP
      r.push e
    }
  end
  r
end

#keysObject

:call-seq:

keys()       -> ary

Returns a list of all contained keys

c = Contents.new "text/html; boundary=0123456"
c.keys                #=> [ :boundary]


142
# File 'lib/hermeneutics/contents.rb', line 142

def keys ; @hash.keys ; end

#to_sObject Also known as: quote

Show the line as readable text.



151
152
153
# File 'lib/hermeneutics/contents.rb', line 151

def to_s
  quoted_parts.join "#{SEP} "
end