Class: Html2Email

Inherits:
Object
  • Object
show all
Defined in:
lib/html2email.rb

Overview

Command line wrapper for creating and writing HtmlEmail objects

Constant Summary collapse

VERSION =
'0.1.4'

Instance Method Summary collapse

Constructor Details

#initialize(args = []) ⇒ Html2Email

Returns a new instance of Html2Email.



12
13
14
# File 'lib/html2email.rb', line 12

def initialize(args = [])
  @args, @options = args, { :default_type => 'str', :test_recipients => [] }
end

Instance Method Details

#optionsObject



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
# File 'lib/html2email.rb', line 16

def options
  OptionParser.new do |opt|
    opt.banner = %Q{\
      Convert an html file to an email-compatible form.

      Usage: #{File.basename $0} [options] [infile[:outfile], ...]

      Options:
    }.gsub(/^ +/,'')

    opt.on('-l', '--layout FILE', 'Use FILE as a layout template') do |arg|
      @options[:layout] = File.expand_path arg
    end

    types = Tilt.mappings.keys
    opt.on('-t', '--default-type FORMAT',
           'Fall back to FORMAT when template type cannot be inferred from',
           "a file's extension, e.g. input from STDIN. " +
           "Defaults to `#{@options[:default_type]}'") do |arg|
      if types.include? arg
        @options[:default_type] = arg
      else
        raise ArgumentError, "Default type must be one of: #{types.join ', '}"
      end
    end

    opt.on('-e', '--email [ADDR,ADDR]', Array,
           'Send rendered html to recipients; list can also be defined',
           'within the template') do |arg|
      @options[:email_test] = true
      @options[:test_recipients] = arg || []
    end

    opt.on('--stdout',
           'Write to STDOUT when an outfile is not explicitly specified') do
      @options[:stdout] = true
    end

    opt.separator "\nSupported FORMATs:\n#{types.join ', '}"
  end
end

#runObject

Command line interface



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/html2email.rb', line 59

def run
  options.parse! @args
  messages = []

  process(@args).each do |infile, outfile|
    htmlemail = HtmlEmail.new infile, @options[:layout], @options
    write (html = htmlemail.render), outfile
    messages << html
    @options[:test_recipients] += htmlemail.test_recipients
  end

  if @options[:email_test]
    HtmlMailer.new(messages, @options[:test_recipients].uniq).html_send
  end
ensure
  FileUtils.rm_f(@tempfile) if @tempfile
end