Text   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 38
Duplicated Lines 13.16 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 19 6
A set_attachment_content_disposition() 0 6 2
A get_attachment_content_disposition() 5 6 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
    def get_attachment_content_disposition(filename = nil)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
36
      if filename.to_s.length > 0
37
        return "attachment; filename=\"#{filename}\""
38
      end
39
      'attachment'
40
    end
41
  end
42
end
43