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

#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.



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?    ; @hash.empty?            ; end

#encodeObject

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

#keysObject

:call-seq:

keys()       -> ary

Returns a list of all contained keys

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


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

def keys ; @hash.keys ; end

#notempty?Boolean

Returns:

  • (Boolean)


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

def notempty? ; self if @hash.notempty? ; end

#to_sObject 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