class Mariko::Listener

Public Class Methods

new(path: '.') click to toggle source
# File lib/mariko/listener.rb, line 10
def initialize(path: '.')
  @path = path
end

Public Instance Methods

everything(files_paths) click to toggle source
# File lib/mariko/listener.rb, line 28
def everything(files_paths) # TODO: break it
  files_paths.each do |file_path|
    Zip::File.open(file_path) do |file_zip|

      Dir.mktmpdir do |tmp_dir_path|

        images_paths = []
        file_zip.each do |entry|
          image_path = "#{tmp_dir_path}/#{entry.name}"
          entry.extract image_path
          images_paths << image_path
        end

        pdf_path = "#{@path}/#{file_zip.name}.pdf"

        Prawn::Document.generate(pdf_path, skip_page_creation: true) do
          images_paths.each do |image_path|

            image_size = FastImage.size(image_path)

            start_new_page size: image_size, margin: 0

            image image_path, at: [0, image_size[1]]
          end
        end

        options = {
          address: ENV['MARIKO_SMTP_ADDRESS'],
          port: ENV['MARIKO_SMTP_PORT'],
          domain: ENV['MARIKO_SMTP_DOMAIN'],
          user_name: ENV['MARIKO_SMTP_USER_NAME'],
          password: ENV['MARIKO_SMTP_PASSWORD'],
          authentication: ENV['MARIKO_SMTP_AUTHENTICATION'],
          enable_starttls_auto: true
        }

        Mail.deliver do
          delivery_method :smtp, options

          to ENV['MARIKO_SEND_TO']
          from ENV['MARIKO_SEND_FROM']
          subject file_zip.name
          add_file pdf_path
        end

        File.delete pdf_path
      end
    end

    File.delete file_path
  end
end
listen() click to toggle source
# File lib/mariko/listener.rb, line 14
def listen
  puts 'Ctrl-C を押しプロセスを停止します'

  loop do
    files_paths = Dir["#{@path}/*.zip"]

    everything files_paths unless files_paths.empty?

    sleep 1
  end
rescue Interrupt
  puts 'またね~(@^0^)/~'
end