Class: Hermeneutics::Message

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

Direct Known Subclasses

Mail

Defined Under Namespace

Classes: Header, 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.



519
520
521
522
# File 'lib/hermeneutics/message.rb', line 519

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



530
531
532
533
534
535
536
537
# File 'lib/hermeneutics/message.rb', line 530

def method_missing sym, *args, &block
  case sym
    when /\Ah_(.*)/, /\Aheader_(.*)/ then
      header $1.to_sym, *args
    else
      super
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



517
518
519
# File 'lib/hermeneutics/message.rb', line 517

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



517
518
519
# File 'lib/hermeneutics/message.rb', line 517

def headers
  @headers
end

Class Method Details

.createObject



478
479
480
# File 'lib/hermeneutics/message.rb', line 478

def create
  new nil, nil
end

.parse(input) ⇒ Object



472
473
474
475
476
# File 'lib/hermeneutics/message.rb', line 472

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

Instance Method Details

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



546
547
548
# File 'lib/hermeneutics/message.rb', line 546

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

#body_binary=(body) ⇒ Object



604
605
606
607
# File 'lib/hermeneutics/message.rb', line 604

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

#body_decodedObject



583
584
585
586
587
588
589
590
591
592
593
594
595
# File 'lib/hermeneutics/message.rb', line 583

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



597
598
599
600
601
602
# File 'lib/hermeneutics/message.rb', line 597

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

#has?(name) ⇒ Boolean Also known as: has_header?

Returns:

  • (Boolean)


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

def has? name
  @headers.has? name
end

#header(sym, type = nil) ⇒ Object



524
525
526
# File 'lib/hermeneutics/message.rb', line 524

def header sym, type = nil
  @headers.field sym, type
end

#inspectObject



555
556
557
558
559
560
561
562
# File 'lib/hermeneutics/message.rb', line 555

def inspect
  r = ""
  r << "#<#{self.class}:"
  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)


550
551
552
# File 'lib/hermeneutics/message.rb', line 550

def is_multipart?
  Multipart === @body
end

#to_sObject



564
565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/hermeneutics/message.rb', line 564

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 << "\n" << @body.to_s
  r.ends_with? "\n" or r << "\n"
  r
end

#transfer_encodingObject



578
579
580
581
# File 'lib/hermeneutics/message.rb', line 578

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