Class: AlegoPdf::Merging

Inherits:
Object
  • Object
show all
Defined in:
lib/alego_pdf/merging.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf', urls = [], orderize = 'url') ⇒ Merging

Returns a new instance of Merging.



8
9
10
11
12
13
14
15
16
# File 'lib/alego_pdf/merging.rb', line 8

def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf', urls = [], orderize = 'url')
  @ids = ids
  @model = model
  @attribute = attribute
  @filename = filename
  @urls = urls
  @orderize = orderize
  @target = HexaPDF::Document.new
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



6
7
8
# File 'lib/alego_pdf/merging.rb', line 6

def attribute
  @attribute
end

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/alego_pdf/merging.rb', line 6

def filename
  @filename
end

#idsObject (readonly)

Returns the value of attribute ids.



6
7
8
# File 'lib/alego_pdf/merging.rb', line 6

def ids
  @ids
end

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/alego_pdf/merging.rb', line 6

def model
  @model
end

#orderizeObject (readonly)

Returns the value of attribute orderize.



6
7
8
# File 'lib/alego_pdf/merging.rb', line 6

def orderize
  @orderize
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'lib/alego_pdf/merging.rb', line 6

def target
  @target
end

#urlsObject (readonly)

Returns the value of attribute urls.



6
7
8
# File 'lib/alego_pdf/merging.rb', line 6

def urls
  @urls
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/alego_pdf/merging.rb', line 18

def call
  if orderize == 'url'
    join_urls
    join_array
  else
    join_array
    join_urls
  end

  target.write(filename, optimize: true)
end

#join_arrayObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/alego_pdf/merging.rb', line 44

def join_array
  array = model.is_a?(String) ? model.camelize.constantize.where(id: ids) : model.where(id: ids)

  array.each do |object|
    file = object[attribute]
    filepath = file.try(:url) || object.try(:arquivo_url)

    next if filepath.blank?

    uri = URI.parse(filepath).open(&:read)
    tempfile = Tempfile.new
    tempfile.write(uri.force_encoding('UTF-8'))
    tempfile.close

    pdf = HexaPDF::Document.open(tempfile)
    pdf.pages.each { |page| target.pages << target.import(page) }
  end
end

#join_urlsObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/alego_pdf/merging.rb', line 30

def join_urls
  urls.each do |filepath|
    next if filepath.blank?

    uri = URI.parse(filepath).open(&:read)
    tempfile = Tempfile.new
    tempfile.write(uri.force_encoding('UTF-8'))
    tempfile.close

    pdf = HexaPDF::Document.open(tempfile)
    pdf.pages.each { |page| target.pages << target.import(page) }
  end 
end