lib/rumble.rb in rumble-0.5.1 vs lib/rumble.rb in rumble-0.5.2

- old
+ new

@@ -18,107 +18,12 @@ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -require 'mail' -require 'uuidtools' -require 'liquid' -require 'csv' -require 'redcarpet' -require 'redcarpet/render_strip' -require 'rainbow' -require_relative 'rumble/version' - -# Rumble main script. +# Rumble module. # Author:: Yegor Bugayenko (yegor256@gmail.com) # Copyright:: Copyright (c) 2018-2019 Yegor Bugayenko # License:: MIT module Rumble - # Command line interface. - class CLI - def initialize(opts) - @opts = opts - end - - def send - letter = Liquid::Template.parse( - File.read(File.expand_path(@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 - message_id "<#{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 - mail.deliver! unless @opts[:dry] - total += 1 - puts "#{Rainbow('done').green} ##{total}" - end - puts "Processed #{sent.size} emails" - end - end + # Nothing end