Sha256: 96c0f8239bac44c9bf8fea6d72ac93f6b016a96a9c6100d598b1efb63512db23

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module MailExtract
  class Line
    attr_reader :body, :type, :subtype
    
    PATTERNS = {
      /^[>]+\s?/                     => :quote,
      /^--/                          => :signature,
      /^-- /                         => :signature,
      /^[_]{2,}\n?/                  => :signature,
      /^[-]{2,}\n?/                  => :signature,
      /^sent from my (iphone|ipad)/i => :signature 
    }
    
    def initialize(str)
      @body    = str
      @subtype = :none
      
      detect_type(str)
    end
    
    # Returns true if line was detected as text
    #
    def text?
      type == :text
    end
    
    # Returns true if line was detected as quote
    #
    def quote?
      type == :quote
    end
    
    # Returns true if line was detected as signature
    #
    def signature?
      type == :signature
    end
    
    private
    
    def detect_type(line)
      # Detects the start line of quote text
      if line.strip =~ /^On\s/i && line =~ /at [\d:]+/ || line.strip =~ />? wrote:\z/
        @type    = :quote
        @subtype = :start
        return
      end
      
      @type = :text
      
      PATTERNS.each_pair do |p,t|
        if line =~ p
          @type = t
          break
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mail_extract-0.1.4 lib/mail_extract/line.rb
mail_extract-0.1.3 lib/mail_extract/line.rb