File: transfer_encoding.rb

package info (click to toggle)
ruby-mail 2.6.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,092 kB
  • ctags: 1,281
  • sloc: ruby: 43,919; makefile: 2
file content (58 lines) | stat: -rw-r--r-- 1,348 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# encoding: utf-8
module Mail
  module Encodings
    class TransferEncoding
      NAME = ''

      PRIORITY = -1

      def self.can_transport?(enc)
        enc = Encodings.get_name(enc)
        if Encodings.defined? enc
          Encodings.get_encoding(enc).new.is_a? self
        else
          false
        end
      end

      def self.can_encode?(enc)
        can_transport? enc 
      end

      def self.cost(str)
        raise "Unimplemented"
      end

      def self.to_s
        self::NAME
      end

      def self.get_best_compatible(source_encoding, str)
        if self.can_transport? source_encoding then
            source_encoding 
        else
            choices = []
            Encodings.get_all.each do |enc|
                choices << enc if self.can_transport? enc and enc.can_encode? source_encoding
            end
            best = nil 
            best_cost = 100
            choices.each do |enc|
                this_cost = enc.cost str
                if this_cost < best_cost then
                    best_cost = this_cost
                    best = enc
                elsif this_cost == best_cost then 
                    best = enc if enc::PRIORITY < best::PRIORITY
                end
            end
            best
        end
      end

      def to_s
        self.class.to_s
      end
    end
  end
end