prawn-attachment 0.2.0 → 0.3.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
2
  SHA256:
3
- metadata.gz: 5cedf55c7c97ed821fc7d9a61cd6f88538eb959d17cd2fe11f8a5aaae2d734c0
4
- data.tar.gz: 5ad7f5ce585f97f508fda3d6a257f4f7bc47197887914c1424aaa506f4747bad
3
+ metadata.gz: c82ce1fc502c03d7348f8c72873ae174a82387f42b6f1619ee0405eccd75165b
4
+ data.tar.gz: 61646a398075b6757fd9682f47754041e12e4bb444956489fbb60bb2757ec059
5
5
  SHA512:
6
- metadata.gz: c4c2452fa385e45480211f2744e6cfaa7d96131acff1472e8652ef7802500b5c895eeb16358a134520f77492a06db8533abba8d4b3707d9a899ecf8e5384ab36
7
- data.tar.gz: '09fdd61fd399f58a78898b15faaea1b4738f1fa7617a0aa8dd1edc9e9bdbbdf02d4fc15bd1208a9d936beefdecbf10f5d604eb09ce079361458a395742357e65'
6
+ metadata.gz: 76721c29d6d7b918b4ef580b7465b651fc97c9d5579107a842084f8528994854c68d4b6e2a47d8b52325ce4ed74e7934e93ae3680c7ae9ceab22471e8cf99656
7
+ data.tar.gz: 5e349c0d2f29986ec24c74efbd7b2155b4a7e8623a069c4791da1d0162b361d7db37fb01bf0a86784f6d58390d3aa2d6bef790a3b964ff2c03e2aa83fa98a2e5
data/.rubocop.yml CHANGED
@@ -1,5 +1,7 @@
1
1
  AllCops:
2
2
  TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - "spec/**/*"
3
5
 
4
6
  Style/StringLiterals:
5
7
  Enabled: true
data/README.md CHANGED
@@ -28,7 +28,7 @@ require "prawn/attachment"
28
28
 
29
29
  Prawn::Document.generate("hello.pdf") do
30
30
  text "Hello World!"
31
- attach "./data.json"
31
+ attach "data.json", File.open("./data.json")
32
32
  end
33
33
  ```
34
34
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Prawn
4
4
  module Attachment
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
@@ -19,6 +19,13 @@ module Prawn
19
19
  end
20
20
  end
21
21
 
22
+ # Invalid data provided
23
+ class InvalidDataError < StandardError
24
+ def message
25
+ "Source data must be an IO object or string"
26
+ end
27
+ end
28
+
22
29
  # Attach a file's data to the document. File IO Objects are expected.
23
30
  #
24
31
  # Arguments:
@@ -42,9 +49,7 @@ module Prawn
42
49
  # hidden, then nil is returned.
43
50
  #
44
51
  def attach(name, src, opts = {})
45
- data = src.is_a?(IO) ? src.read : src.b
46
- raise NoDataError if data.length.zero?
47
-
52
+ data = extract_data_from_source(src)
48
53
  opts = prepare_options(name, opts)
49
54
 
50
55
  # Prepare embeddable representation of the source data
@@ -58,6 +63,19 @@ module Prawn
58
63
 
59
64
  private
60
65
 
66
+ def extract_data_from_source(src)
67
+ if src.respond_to?(:read)
68
+ data = src.read
69
+ elsif src.respond_to?(:b)
70
+ data = src.b
71
+ else
72
+ raise InvalidDataError
73
+ end
74
+ raise NoDataError if data.length.zero?
75
+
76
+ data
77
+ end
78
+
61
79
  def prepare_options(name, opts)
62
80
  {
63
81
  name: name,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-attachment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Lilue
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-03-07 00:00:00.000000000 Z
12
+ date: 2022-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: prawn