class Hermes::Message

Constants

MIME

Attributes

body[R]
headers[R]

Public Class Methods

new(headers, body) click to toggle source
# File lib/hermes/message.rb, line 542
def initialize headers, body
  @headers, @body = headers, body
  @headers ||= Headers.create
end

Public Instance Methods

[](name, type = nil) click to toggle source
# File lib/hermes/message.rb, line 556
def [] name, type = nil
  @headers[ name, type]
end
body=(body) click to toggle source
# File lib/hermes/message.rb, line 619
def body= body
  @body = body
end
body_binary=(body) click to toggle source
# File lib/hermes/message.rb, line 614
def body_binary= body
  @headers.replace :content_transfer_encoding, "base64"
  @body = [ body].pack "m*"
end
body_decoded() click to toggle source
# File lib/hermes/message.rb, line 592
def body_decoded
  r = case transfer_encoding
    when "quoted-printable" then
      (@body.unpack "M").join
    when "base64" then
      (@body.unpack "m").join
    else
      @body.new_string
  end
  if (c = @headers.content_type) and (s = c[ :charset]) then
    r.force_encoding s
  end
  r
end
body_text=(body) click to toggle source
# File lib/hermes/message.rb, line 607
def body_text= body
  body = body.to_s
  @headers.replace :content_type, "text/plain", charset: body.encoding
  @headers.replace :content_transfer_encoding, "quoted-printable"
  @body = [ body].pack "M*"
end
create() click to toggle source
# File lib/hermes/message.rb, line 502
def create
  new nil, nil
end
inspect() click to toggle source
# File lib/hermes/message.rb, line 565
def inspect
  r = ""
  r << "#<#{cls}:"
  r << "0x%x" % (object_id<<1)
  r << " headers:#{@headers.length}"
  r << " multipart" if is_multipart?
  r << ">"
end
is_multipart?() click to toggle source
# File lib/hermes/message.rb, line 560
def is_multipart?
  Multipart === @body
end
Also aliased as: mp?
method_missing(sym, *args, &block) click to toggle source
Calls superclass method
# File lib/hermes/message.rb, line 547
def method_missing sym, *args, &block
  case sym
    when /h_(.*)/, /header_(.*)/ then
      @headers.field $1.to_sym, *args
    else
      @headers.field sym, *args or super
  end
end
mp?()
Alias for: is_multipart?
parse(input, parameters = nil) click to toggle source
# File lib/hermes/message.rb, line 496
def parse input, parameters = nil
  parse_hb input do |h,b|
    new h, b
  end
end
to_s() click to toggle source
# File lib/hermes/message.rb, line 574
def to_s
  r = ""
  if is_multipart? then
    c = @headers.field :content_type
    u = @body.boundary
    if c[ :boundary] != u then
      @headers.replace :content_type, c.fulltype, :boundary => u
    end
  end
  r << @headers.to_s << $/ << @body.to_s
  r
end
transfer_encoding() click to toggle source
# File lib/hermes/message.rb, line 587
def transfer_encoding
  c = @headers[ :content_transfer_encoding]
  c.caption if c
end

Private Instance Methods

parse_hb(input) { |h, b| ... } click to toggle source
# File lib/hermes/message.rb, line 508
def parse_hb input
  h = parse_headers input
  c = h.content_type
  b = c.parse_mime input if c
  unless b then
    b = ""
    input.eat_lines { |l| b << l }
    b
  end
  yield h, b
end
parse_headers(input) click to toggle source
# File lib/hermes/message.rb, line 520
def parse_headers input
  h = []
  input.eat_lines { |l|
    l.chomp!
    case l
      when /^$/ then
        break
      when /^\s+/ then
        h.last or
          raise ParseError, "First line may not be a continuation."
        h.last << $/ << l
      else
        h.push l
    end
  }
  Headers.parse h
end