Class: Mariko::Listener

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

Instance Method Summary collapse

Constructor Details

#initialize(path: '.') ⇒ Listener

Returns a new instance of Listener.



10
11
12
# File 'lib/mariko/listener.rb', line 10

def initialize(path: '.')
  @path = path
end

Instance Method Details

#everything(files_paths) ⇒ Object

TODO: break it



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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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

#listenObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# 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