class Hermeneutics::Dictionary

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"

Attributes

hash[R]
to_h[R]
to_hash[R]

Public Class Methods

new(hash = nil) click to toggle source

Create a Dictionary object from a value and a hash.

ds = Dictionary.new v: 1, a: "rsa-sha256",
                            c: "relaxed/relaxed", ...
# 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
parse(line) click to toggle source

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"
# File lib/hermeneutics/contents.rb, line 46
def parse line
  rest = line.strip
  hash = parse_hash rest
  new hash
end
urltext() click to toggle source
# File lib/hermeneutics/contents.rb, line 52
def urltext
  @urltext ||= URLText.new mask_space: true
end

Private Class Methods

parse_hash(rest) click to toggle source
# File lib/hermeneutics/contents.rb, line 58
def parse_hash rest
  hash = Hash.new { |h,k| h[ k] = [] }
  asts = {}
  while rest.notempty? do
    key, rest = if rest =~ REA then
      ast = $1
      ord = $2.to_i if $2
      [ $`, $']
    else
      [ rest.dup, ""]
    end
    key.downcase!
    key = key.to_sym
    asts[ key] = ast
    val, rest = if not ast and rest =~ /\A"(.*?)"(?:#{SEP}\s*|\z)/ then
      [ $1, $']
    else
      rest.split RES, 2
    end
    if ord then
      hash[ key][ ord] = val
    else
      hash[ key] = val
    end
  end
  r = URLText::Dict.new
  hash.keys.each { |k|
    v = hash[ k]
    Array === v and v = v.join
    if asts[ k] then
      enc, lang, val = v.split "'"
      val.force_encoding enc
      v = URLText.decode val
    end
    r[ k] = v
  }
  r
end

Public Instance Methods

[]( key) → str or nil click to toggle source

Find value of key.

# File lib/hermeneutics/contents.rb, line 123
def [] key ; @hash[ key.to_sym] ; end
Also aliased as: field
Alias for: []
keys() → ary click to toggle source

Returns a list of all contained keys

c = Contents.new "text/html; boundary=0123456"
c.keys                #=> [ :boundary]
# File lib/hermeneutics/contents.rb, line 146
def keys ; @hash.keys ; end

Private Instance Methods

method_missing(sym, *args) click to toggle source
Calls superclass method
# File lib/hermeneutics/contents.rb, line 128
def method_missing sym, *args
  if sym =~ /[^a-z_]/ or args.any? then
    super
  else
    field sym
  end
end