Class: Facsimile
Defined Under Namespace
Classes: BadContent, Error, NoContentFound, NoPhoneNumberFound
Constant Summary collapse
- Width =
1728
- Height =
2156
Instance Attribute Summary collapse
-
#frames ⇒ Object
readonly
Returns the value of attribute frames.
-
#mail ⇒ Object
readonly
Returns the value of attribute mail.
Instance Method Summary collapse
- #callfile_name ⇒ Object
- #filename ⇒ Object
- #id ⇒ Object
-
#initialize(source) ⇒ Facsimile
constructor
A new instance of Facsimile.
- #number ⇒ Object
-
#render ⇒ Object
memoize :number.
- #write(path) ⇒ Object
Methods included from Logging
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
#frames ⇒ Object (readonly)
Returns the value of attribute frames.
14 15 16 |
# File 'lib/facsimile.rb', line 14 def frames @frames end |
#mail ⇒ Object (readonly)
Returns the value of attribute mail.
15 16 17 |
# File 'lib/facsimile.rb', line 15 def mail @mail end |
Instance Method Details
#callfile_name ⇒ Object
88 89 90 |
# File 'lib/facsimile.rb', line 88 def callfile_name "#{id}.call" end |
#filename ⇒ Object
84 85 86 |
# File 'lib/facsimile.rb', line 84 def filename "#{id}.tiff" end |
#id ⇒ Object
92 93 94 95 96 97 |
# File 'lib/facsimile.rb', line 92 def id unless mail. mail.("#{Time.now.iso8601}-#{Process.pid}") end mail. end |
#number ⇒ Object
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 |
#render ⇒ Object
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
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. 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 |