Class: Hermeneutics::Message

Inherits:
Mime
  • Object
show all
Defined in:
lib/hermeneutics/message.rb

Direct Known Subclasses

Mail

Defined Under Namespace

Classes: Headers, ParseError

Constant Summary collapse

MIME =
"message/rfc822"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Mime

find, inherited

Constructor Details

#initialize(headers, body) ⇒ Message

Returns a new instance of Message.



542
543
544
545
# File 'lib/hermeneutics/message.rb', line 542

def initialize headers, body
  @headers, @body = headers, body
  @headers ||= Headers.create
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



547
548
549
550
551
552
553
554
# File 'lib/hermeneutics/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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



540
541
542
# File 'lib/hermeneutics/message.rb', line 540

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



540
541
542
# File 'lib/hermeneutics/message.rb', line 540

def headers
  @headers
end

Class Method Details

.createObject



502
503
504
# File 'lib/hermeneutics/message.rb', line 502

def create
  new nil, nil
end

.parse(input) ⇒ Object



496
497
498
499
500
# File 'lib/hermeneutics/message.rb', line 496

def parse input
  parse_hb input do |h,b|
    new h, b
  end
end

Instance Method Details

#[](name, type = nil) ⇒ Object



556
557
558
# File 'lib/hermeneutics/message.rb', line 556

def [] name, type = nil
  @headers[ name, type]
end

#body_binary=(body) ⇒ Object



613
614
615
616
# File 'lib/hermeneutics/message.rb', line 613

def body_binary= body
  @headers.replace :content_transfer_encoding, "base64"
  @body = [ body].pack "m*"
end

#body_decodedObject



592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/hermeneutics/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
  end
  c = @headers.content_type
  r.force_encoding c&&c[ :charset] || Encoding::ASCII_8BIT
  r
end

#body_text=(body) ⇒ Object



606
607
608
609
610
611
# File 'lib/hermeneutics/message.rb', line 606

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

#inspectObject



565
566
567
568
569
570
571
572
# File 'lib/hermeneutics/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?Boolean Also known as: mp?

Returns:

  • (Boolean)


560
561
562
# File 'lib/hermeneutics/message.rb', line 560

def is_multipart?
  Multipart === @body
end

#to_sObject



574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/hermeneutics/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_encodingObject



587
588
589
590
# File 'lib/hermeneutics/message.rb', line 587

def transfer_encoding
  c = @headers[ :content_transfer_encoding]
  c.caption if c
end