Class: Hermeneutics::Contents

Inherits:
Dictionary show all
Defined in:
lib/hermeneutics/contents.rb

Overview

A parser for header fields in Content-Type style

Example

content_disposition = Contents.new "form-data", name: "mycontrol"

content_type = Contents.parse "text/html; boundary=0123456"
content_type.caption       #=>  "text/html"
content_type[ :boundary]   #=> "0123456"
  # (Subclass ContentType even splits the caption into type/subtype.)

Direct Known Subclasses

ContentType

Constant Summary

Constants inherited from Dictionary

Dictionary::REA, Dictionary::RES, Dictionary::SEA, Dictionary::SEP, Dictionary::TSPECIAL

Instance Attribute Summary collapse

Attributes inherited from Dictionary

#hash

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Dictionary

#[], #encode, #keys, #to_s, urltext

Constructor Details

#initialize(caption, hash = nil) ⇒ Contents

Create a Contents object from a value and a hash.

c = Contents.new "text/html", boundary: "0123456"


254
255
256
257
258
259
260
# File 'lib/hermeneutics/contents.rb', line 254

def initialize caption, hash = nil
  if caption =~ RES or caption =~ REA then
    raise "Invalid content caption '#{caption}'."
  end
  @caption = caption.new_string
  super hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hermeneutics::Dictionary

Instance Attribute Details

#captionObject (readonly)

Returns the value of attribute caption.



248
249
250
# File 'lib/hermeneutics/contents.rb', line 248

def caption
  @caption
end

Class Method Details

.parse(line) ⇒ Object

Create a Contents object out of a string from a mail header field.

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


239
240
241
242
243
244
# File 'lib/hermeneutics/contents.rb', line 239

def parse line
  rest = line.strip
  caption, rest = rest.split Dictionary::RES, 2
  hash = parse_hash rest
  new caption, hash
end

Instance Method Details

#=~(re) ⇒ Object



265
266
267
# File 'lib/hermeneutics/contents.rb', line 265

def =~ re
  @caption =~ re
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?    ; @caption.empty? and super           ; end

#encoded_partsObject



276
277
278
279
280
# File 'lib/hermeneutics/contents.rb', line 276

def encoded_parts
  r = [ "#@caption"]
  r.concat super
  r
end

#notempty?Boolean

Returns:

  • (Boolean)


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

def notempty? ; self if @caption.notempty? or super ; end

#quoted_partsObject



270
271
272
273
274
# File 'lib/hermeneutics/contents.rb', line 270

def quoted_parts
  r = [ "#@caption"]
  r.concat super
  r
end