Completed
Push — master ( 1a678e...5c13ba )
by John
01:04
created

Text   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 37
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 19 6
A get_attachment_content_disposition() 0 5 2
A set_attachment_content_disposition() 0 6 2
1
require 'mime'
2
3
module MIMEBuilder
4
  class Text
5
    attr_accessor :mime
6
    attr_accessor :text
7
8
    def initialize(text, opts = {})
9
      @text = text
10
11
      if opts.key?(:content_type) && opts[:content_type].to_s.length>0
12
        content_type = opts[:content_type]
13
        if content_type =~ /^text\/([^\/]+)$/i
14
          @mime = MIME::Text.new(text, $1.downcase)
15
        else
16
          raise "Unknown Content Type: " + opts[:content_type].to_s
17
        end
18
      else
19
        @mime = MIME::Text.new(text, 'plain')
20
      end
21
22
      @mime.headers.delete('Content-Id') \
23
        if opts.key?(:content_id_disable) && opts[:content_id_disable]
24
25
      set_attachment_content_disposition(opts[:filename], opts[:is_attachment])
26
    end
27
28
    def set_attachment_content_disposition(filename, is_attachment)
29
      @mime.headers.set(
30
        'Content-Disposition',
31
        get_attachment_content_disposition(filename)
32
      ) if is_attachment
33
    end
34
35
    def get_attachment_content_disposition(filename = nil)
36
      cd = filename.to_s.length > 0              \
37
        ? "attachment; filename=\"#{filename}\"" \
38
        : 'attachment'
39
    end
40
  end
41
end
42