alego_pdf 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: af1c4d29195fc9f0cc0648b710923a8e63bf63c6e7d48feaf9373bf76ae7c536
4
- data.tar.gz: f67c101c414c719d10c695504ca4454d1e3c41924304c3c4b30d6dd96d184bde
2
+ SHA1:
3
+ metadata.gz: c18e67ee692d9a16b56d8a3d9cfde913dd5c344c
4
+ data.tar.gz: a769b0fd00f864c4f924c4242752ab28a62a02e9
5
5
  SHA512:
6
- metadata.gz: 998a826b0368b40fa1a1d43964f241dc2229d8423096a73f25d930b9ae49cfaa4ab15369dcd28da21dc2d4f7599500c84b0572d1654549f3d752121c288af071
7
- data.tar.gz: 40f26d1de7c0cbd1b0b07df2b7136a4d9defa0405017960de99d007b35a2f39b9d300f38d62e0b75577a5bf192024c24256f8f43e375dd47f2222b60af95adc2
6
+ metadata.gz: 1911376a281719e8830569f0d890ebc2e6c21bb1cae6019d4cdaac44feabe02af644adbed26d3b61633c8ab3a7592d2bd81665e3c7c069b05c3cb096539c2e30
7
+ data.tar.gz: 0c9007c161e74d0ab7d362fd8b8dbc46a1a52578f92e8fc5ad188e7d47f71f0c29e8e4fdd5bd755c491bf0ebded17ed17bbc708b9bd58a5569f8c1614f03af45
data/README.md CHANGED
@@ -26,49 +26,79 @@ Ou instale isto você mesmo usando o comando:
26
26
  ## Uso
27
27
  A gem 'alego_pdf' depende da gem 'hexapdf' para fazer a juntada de arquivos.
28
28
  Veja mais sobre a gem 'hexapdf' no Github: https://github.com/gettalong/hexapdf
29
- A gem 'alego_pdf' faz a leitura dos arquivos via URI e Tempfile e a gem 'hexapdf' junta-os. Veja o exemplo na gem alego_pdf:
30
-
29
+ A gem 'alego_pdf' busca os objeto faz a leitura dos arquivos via URI e Tempfile e a gem 'hexapdf' junta-os. Veja o exemplo na gem alego_pdf:
31
30
  ```
32
- def call
33
- target = HexaPDF::Document.new
34
-
35
- # filepaths é um array com as url's dos arquivos
36
- filepaths.each do |filepath|
37
- uri = URI.parse(filepath).open(&:read) # leitura do arquivo
38
- tempfile = Tempfile.new
39
- tempfile.write(uri.force_encoding('UTF-8')) # escrita no arquivo Tempfile
40
- tempfile.close # fecha o arquivo
41
-
42
- pdf = HexaPDF::Document.open(tempfile)
43
- pdf.pages.each { |page| target.pages << target.import(page) } # junta os arquivos em um novo HexaPDF
31
+ require 'hexapdf'
32
+ require 'open-uri'
33
+
34
+ module AlegoPdf
35
+ class Merging
36
+ attr_reader :ids, :model, :attribute, :filename
37
+
38
+ def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf')
39
+ @ids = ids
40
+ @model = model
41
+ @attribute = attribute
42
+ @filename = filename
44
43
  end
45
44
 
46
- target.write(filename, optimize: true)
47
- end
48
- ```
45
+ def call
46
+ array = model.is_a?(String) ? model.camelize.constantize.where(id: ids) : model.where(id: ids)
47
+ target = HexaPDF::Document.new
48
+
49
+ array.each do |objeto|
50
+ file = objeto[attribute]
51
+ filepath = file.try(:url) || objeto.try(:arquivo_url)
49
52
 
50
- Após instalar a gem 'alego_pdf', deve buscar a URL dos arquivos e passar estas URL's como parâmetro. Veja o exemplo a seguir:
53
+ next if filepath.blank?
54
+
55
+ uri = URI.parse(filepath).open(&:read)
56
+ tempfile = Tempfile.new
57
+ tempfile.write(uri.force_encoding('UTF-8'))
58
+ tempfile.close
59
+
60
+ pdf = HexaPDF::Document.open(tempfile)
61
+ pdf.pages.each { |page| target.pages << target.import(page) }
62
+ end
51
63
 
64
+ target.write(filename, optimize: true)
65
+ end
66
+ end
67
+ end
52
68
  ```
69
+ Obs.: Os parâmetros 'ids', 'model' e 'attribute' são obrigatórios. O parâmetro 'filename' é por default 'tmp/merge_files.pdf', porém pode-se atribuir um caminho pré-definido.
70
+ Após instalar a gem 'alego_pdf', deve criar uma rota '/rota_merge_arquivos' e deve buscar os ids dos arquivos e passá-los como parâmetro junto com o model e o atributo que onde é guardado os metadados dos arquivos. A gem procura também por um atributo chamado 'arquivo_url', caso não encontre a 'url' (ex.: attribute.try(:url)) Veja o exemplo a seguir:
71
+ ```
72
+ # organogramas_controller.rb
53
73
  def merge_arquivos
54
- # O atributo "arquivo_url" deve ser a url onde o arquivo está armazenado.
55
- # Exemplo: https://spiderminio-dev.al.go.leg.br:9000/portal-rails/uploads/transparencia/organograma/arquivo/1/Manual_do_stimuus_-_6_Instalando_o_esti%CC%81mulo_no_seu_aplicativo.pdf?X-Amz-Expires=600&X-Amz-Date=20220909T165125Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=BUjuSkJYyf0wzRmG%2F20220909%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=791bbc7db8aac7bd0c61dc7a60d04ec3d39f6d63d60f9d971383c97b55e9815a
56
-
57
- filepaths = Model.order('id desc').map(&:arquivo_url)
74
+ organograma_ids = ::Transparencia::Organograma.order('data desc').map(&:id)
58
75
 
59
76
  respond_to do |format|
60
77
  format.html
61
78
  format.pdf do
62
- filename = 'tmp/arquivos.pdf'
63
- AlegoPdf::Merging.new(filepaths, filename).call
79
+ filename = 'tmp/organogramas.pdf' # nome e local onde será salvo o arquivo temporário
80
+ AlegoPdf::Merging.new(organograma_ids, ::Transparencia::Organograma, :arquivo, filename).call
64
81
 
65
82
  pdf_filename = File.join(Rails.root, filename)
66
- send_file(pdf_filename, filename: 'merge-arquivos.pdf', type: 'application/pdf')
83
+ send_file(pdf_filename, filename: 'organogramas.pdf', type: 'application/pdf')
67
84
  end
68
85
  end
69
86
  end
70
87
  ```
71
-
88
+ PS: caso não exista nenhum arquivo, é feito download de uma página em branco, portanto certifique-se que contenha ao menos 1 objeto com upload PDF antes fazer a chamada AlegoPdf::Merging ou apresentar o botão na view que chama a rota e faz o download dos arquivos. Veja o exemplo:
89
+ ```
90
+ // index.html.erb
91
+ <% @organograma_pdf_any = ::Transparencia::Organograma.any?(&:arquivo_url) # verifica se existe ao menos 1 url do arquivo %>
92
+
93
+ <% if @organograma_pdf_any %>
94
+ <a href="/organogramas/merge_arquivos.pdf" target="_blank" title="Juntar arquivos" class="bt bt--secundario">
95
+ Todos organogramas
96
+ <svg class="ico">
97
+ <use xlink:href="<%= asset_path('ico.svg#download') %>" />
98
+ </svg>
99
+ </a>
100
+ <% end %>
101
+ ```
72
102
  ## Contribuição
73
103
 
74
104
  Relatórios de bugs e pull requests são bem-vindos no GitLab em https://gitlab.al.go.leg.br/sistemas/gem/alego_pdf.
Binary file
data/alego_pdf.gemspec CHANGED
@@ -5,9 +5,9 @@ Gem::Specification.new do |spec|
5
5
  spec.version = AlegoPdf::VERSION
6
6
  spec.authors = ['Rodrigo de Sá']
7
7
  spec.email = ['rodrigo.sa@al.go.leg.br']
8
- spec.homepage = 'https://gitlab.al.go.leg.br/gem/alego_pdf'
9
- spec.summary = 'Summary of AlegoPdf.'
10
- spec.description = 'Description of AlegoPdf.'
8
+ spec.homepage = 'https://gitlab.al.go.leg.br/sistemas/gem/alego_pdf'
9
+ spec.summary = "Gem responsible for joining 1 or more PDF in the same file."
10
+ spec.description = "Gem responsible for joining 1 or more PDF in the same file."
11
11
  spec.license = 'MIT'
12
12
  spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
13
13
 
@@ -3,17 +3,25 @@ require 'open-uri'
3
3
 
4
4
  module AlegoPdf
5
5
  class Merging
6
- attr_reader :filepaths, :filename
6
+ attr_reader :ids, :model, :attribute, :filename
7
7
 
8
- def initialize(filepaths, filename)
9
- @filepaths = filepaths
8
+ def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf')
9
+ @ids = ids
10
+ @model = model
11
+ @attribute = attribute
10
12
  @filename = filename
11
13
  end
12
14
 
13
15
  def call
16
+ array = model.is_a?(String) ? model.camelize.constantize.where(id: ids) : model.where(id: ids)
14
17
  target = HexaPDF::Document.new
15
18
 
16
- filepaths.each do |filepath|
19
+ array.each do |object|
20
+ file = object[attribute]
21
+ filepath = file.try(:url) || object.try(:arquivo_url)
22
+
23
+ next if filepath.blank?
24
+
17
25
  uri = URI.parse(filepath).open(&:read)
18
26
  tempfile = Tempfile.new
19
27
  tempfile.write(uri.force_encoding('UTF-8'))
@@ -1,3 +1,3 @@
1
1
  module AlegoPdf
2
- VERSION = '1.0.0'
2
+ VERSION = '2.0.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alego_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo de Sá
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-09 00:00:00.000000000 Z
11
+ date: 2022-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,34 +94,32 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: Description of AlegoPdf.
97
+ description: Gem responsible for joining 1 or more PDF in the same file.
98
98
  email:
99
99
  - rodrigo.sa@al.go.leg.br
100
100
  executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - ".gitignore"
105
- - ".gitlab-ci.yml"
106
- - ".ruby-version"
107
104
  - CHANGELOG
108
105
  - Gemfile
109
106
  - Gemfile.lock
110
107
  - MIT-LICENSE
111
108
  - README.md
112
109
  - Rakefile
110
+ - alego_pdf-1.0.0.gem
113
111
  - alego_pdf.gemspec
114
112
  - bin/console
115
113
  - bin/setup
116
114
  - lib/alego_pdf.rb
117
115
  - lib/alego_pdf/merging.rb
118
116
  - lib/alego_pdf/version.rb
119
- homepage: https://gitlab.al.go.leg.br/gem/alego_pdf
117
+ homepage: https://gitlab.al.go.leg.br/sistemas/gem/alego_pdf
120
118
  licenses:
121
119
  - MIT
122
120
  metadata:
123
- homepage_uri: https://gitlab.al.go.leg.br/gem/alego_pdf
124
- post_install_message:
121
+ homepage_uri: https://gitlab.al.go.leg.br/sistemas/gem/alego_pdf
122
+ post_install_message:
125
123
  rdoc_options: []
126
124
  require_paths:
127
125
  - lib
@@ -136,8 +134,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
134
  - !ruby/object:Gem::Version
137
135
  version: '0'
138
136
  requirements: []
139
- rubygems_version: 3.1.6
140
- signing_key:
137
+ rubyforge_project:
138
+ rubygems_version: 2.6.14.4
139
+ signing_key:
141
140
  specification_version: 4
142
- summary: Summary of AlegoPdf.
141
+ summary: Gem responsible for joining 1 or more PDF in the same file.
143
142
  test_files: []
data/.gitignore DELETED
@@ -1,18 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
- /.idea
10
- /log/*.log
11
- /test/dummy/db/*.sqlite3
12
- /test/dummy/db/*.sqlite3-journal
13
- /test/dummy/log/*.log
14
- /test/dummy/node_modules/
15
- /test/dummy/yarn-error.log
16
- /test/dummy/storage/
17
- /test/dummy/tmp/
18
- /vendor/*
data/.gitlab-ci.yml DELETED
@@ -1,28 +0,0 @@
1
- stages:
2
- - build
3
-
4
- #testes:
5
- # stage: test
6
- # script:
7
- # - echo "Executanto testes automatizados"
8
- # - export RAILS_ENV="test"
9
- # - rbenv install -s
10
- # - if ! rbenv which bundle; then gem install bundler --no-ri --no-rdoc; rbenv rehash; fi
11
- # - bundle install
12
- # - rspec spec
13
- # tags:
14
- # - tester
15
-
16
- build:
17
- stage: build
18
- script:
19
- - if ! rbenv shell 2.7.3; then rbenv install -s 2.7.3; rbenv rehash; fi
20
- - if ! rbenv which bundle; then gem install bundler --no-ri --no-rdoc; rbenv rehash; fi
21
- - bundle install
22
- - rake build
23
- - rake release
24
- # - rake force_release
25
- only:
26
- - /^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$/
27
- tags:
28
- - deployer
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.7.3