Class: Facsimile

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/facsimile.rb

Defined Under Namespace

Classes: BadContent, Error, NoContentFound, NoPhoneNumberFound

Constant Summary collapse

Width =
1728
Height =
2156

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(source) ⇒ Facsimile

Returns a new instance of Facsimile.



17
18
19
20
21
22
23
24
25
# File 'lib/facsimile.rb', line 17

def initialize(source)
  if source.is_a?(IO)
    @mail = Mail.new(source.read)
  else
    @mail = Mail.read(source)
  end
  @frames = Magick::ImageList.new
  @tempfiles = []
end

Instance Attribute Details

#framesObject (readonly)

Returns the value of attribute frames.



14
15
16
# File 'lib/facsimile.rb', line 14

def frames
  @frames
end

#mailObject (readonly)

Returns the value of attribute mail.



15
16
17
# File 'lib/facsimile.rb', line 15

def mail
  @mail
end

Instance Method Details

#callfile_nameObject



88
89
90
# File 'lib/facsimile.rb', line 88

def callfile_name
  "#{id}.call"
end

#filenameObject



84
85
86
# File 'lib/facsimile.rb', line 84

def filename
  "#{id}.tiff"
end

#idObject



92
93
94
95
96
97
# File 'lib/facsimile.rb', line 92

def id
  unless mail.has_message_id?
    mail.add_message_id("#{Time.now.iso8601}-#{Process.pid}")
  end
  mail.message_id
end

#numberObject



27
28
29
30
31
32
33
34
# File 'lib/facsimile.rb', line 27

def number
  n = mail.header['X-Original-To'].andand.to_s || mail.to.andand.first
  if n.present? && n.to_s =~ /^(\d{5,})/
    return $1
  else
    raise NoPhoneNumberFound, "could not find any phone number"
  end
end

#renderObject

memoize :number



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/facsimile.rb', line 38

def render

  if mail.multipart?

    mail.parts.each do |part|
      case part.content_type
      when %r'text/plain'
        add_frames text2tiff( part.decoded )
      when %r'opendocument\.text'
        add_frames oo2tiff( part.decoded )
      when %r'openxmlformats-officedocument' # docx, xlsx
        add_frames oo2tiff( part.decoded, part.filename )
      when %r'application/pdf'
        add_frames pdfdata2tiff( part.decoded )
      when %r'text/html'
        # FIXME ignore html, let's hope a plain text contained everything important
      when %r'multipart/alternative'
        # ignore this, stupid outlook
      else
        log "unsupported content type: #{part.content_type}"
      end
    end

  else

    unless mail.body.decoded.blank?
      text2tiff( mail.body.decoded ).each do |tiff|
        frames << tiff
      end
    end

  end

end

#write(path) ⇒ Object

Raises:



73
74
75
76
77
78
79
80
81
82
# File 'lib/facsimile.rb', line 73

def write(path)
  render
  raise NoContentFound if frames.empty?
  path = File.expand_path path
  log("writing facsimile to #{path}")
  path += '.tiff' unless path.ends_with?('.tiff')
  dir = File.dirname path
  FileUtils.mkdir_p(dir) unless File.directory?(dir)
  frames.write(path)
end