Class: AttachTag

Inherits:
LiquidumTag
  • Object
show all
Defined in:
lib/nuntius/liquid/tags/attach_tag.rb

Overview

Attaches files (from URL)

Basic usage:

{%attach 'https://www.boxture.com/assets/images/logo.png'%}

Instance Method Summary collapse

Instance Method Details

#render(context) ⇒ Object



9
10
11
12
13
14
15
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
# File 'lib/nuntius/liquid/tags/attach_tag.rb', line 9

def render(context)
  super

  return unless argv1

  message = context.registers["message"]

  if argv1.is_a? String
    message.add_attachment({url: argv1})
  elsif argv1.instance_of?(ActiveStorageAttachedOneDrop) || argv1.instance_of?(ActiveStorage::AttachmentDrop)

    io = StringIO.new(argv1.download)
    io.rewind
    content_type = argv1.content_type
    filename = argv1.filename

    if arg(:convert) == "pdf" && content_type != "application/pdf"
      content_type = "application/pdf"
      pdf = Labelary::Label.render(zpl: io.read,
        content_type: content_type,
        dpmm: 8,
        width: arg(:width).blank? ? 4 : arg(:width),
        height: arg(:height).blank? ? 6 : arg(:height))

      io = StringIO.new(pdf)
      filename = "#{filename}.pdf"
    end

    message.add_attachment({content: io, filename: filename, content_type: content_type})
  end

  ""
end