Class: Rumble::CLI
- Inherits:
-
Object
- Object
- Rumble::CLI
- Defined in:
- lib/rumble/cli.rb
Overview
Rumble main script.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2018-2024 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#initialize(opts) ⇒ CLI
constructor
Make an instance.
-
#send ⇒ Object
Send a letter, reading options from the opts.
Constructor Details
#initialize(opts) ⇒ CLI
Make an instance.
40 41 42 |
# File 'lib/rumble/cli.rb', line 40 def initialize(opts) @opts = opts end |
Instance Method Details
#send ⇒ Object
Send a letter, reading options from the opts.
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/rumble/cli.rb', line 45 def send letter = Liquid::Template.parse( File.read(File.(@opts[:letter])) ) skip = @opts[:skip] ? File.readlines(@opts[:skip]).map(&:strip) : [] if @opts[:test] rcpt = [] rcpt[@opts['col-first'].to_i] = 'John' rcpt[@opts['col-last'].to_i] = 'Doe' rcpt[@opts['col-email'].to_i] = @opts[:test] emails = [rcpt] else raise '--csv is required' unless @opts[:csv] emails = CSV.read(@opts[:csv]) end total = 0 sent = [] ignore = !@opts[:resume].nil? && !@opts[:test] from = @opts[:from].strip puts "Sending #{emails.length} email(s) as #{from}" domain = from.strip.gsub(/^.+@|>$/) emails.each do |array| email = array[@opts['col-email'].to_i] unless email puts "Email is #{Rainbow('absent').red} \ at the column ##{@opts['col-email'].to_i}: #{array}" next end email = email.strip.downcase if sent.include?(email) puts "#{Rainbow('Duplicate').red} at: #{array}" next end sent.push(email) first = (array[@opts['col-first'].to_i] || '').strip last = (array[@opts['col-last'].to_i] || '').strip first, last = first.split(' ', 2) if last.empty? && first.include?(' ') name = "#{first.strip} #{last.strip}".strip address = email address = "#{name} <#{email}>" unless name.empty? print "Sending to #{address}... " markdown = letter.render( 'email' => email, 'first' => first, 'last' => last ) html = Redcarpet::Markdown.new(Redcarpet::Render::HTML) .render(markdown) text = Redcarpet::Markdown.new(Redcarpet::Render::StripDown) .render(markdown) if ignore if @opts[:resume].downcase != email puts "#{Rainbow('ignored').orange}, waiting for #{@opts[:resume]}" next end ignore = false end if skip.include?(email) puts Rainbow('skipped').red next end subject = @opts[:subject] mail = Mail.new do from from to address subject subject "<#{UUIDTools::UUID.random_create}@#{domain}>" text_part do content_type 'text/plain; charset=UTF-8' body text end html_part do content_type 'text/html; charset=UTF-8' body html end end if @opts[:attach] Dir.mktmpdir do |dir| `#{@opts[:attach]} "#{email}" "#{name}" "#{dir}"` raise 'Failed to exec' unless $CHILD_STATUS.success? Dir[File.join(dir, '*')].each do |f| mail.add_file(filename: File.basename(f), content: File.read(f)) end end end mail.deliver! unless @opts[:dry] total += 1 puts "#{Rainbow('done').green} ##{total}" end puts "Processed #{sent.size} emails" end |