html2pdf-rails 0.7.0 → 0.8.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 +4 -4
- data/README.md +16 -0
- data/lib/html2pdf/rails/api_key.rb +21 -0
- data/lib/html2pdf/rails/client.rb +9 -3
- data/lib/html2pdf/rails/errors.rb +13 -0
- data/lib/html2pdf/rails/version.rb +1 -1
- data/lib/html2pdf/rails.rb +11 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fdb4c6c41f2cf9c0b780aba5dbdecc3c47b658a79241247746b8be48a0b4c947
|
|
4
|
+
data.tar.gz: 3b2dc888c30b92815dbf2834bfb6d9ec48f035a10e97870251ee2b4614f54b2b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 2fd542442975af190846df080ea2a55a5c5d7cb883774f0a75fe5f1aa48804cb1468002a6acb9cbe51084e3a263eac3f3e1167fcc492ea0133bcbea0225542ae
|
|
7
|
+
data.tar.gz: 0db1aef20b4907f76d404fac2331c9588d2b6ab0e9b8c850b446684cf712607097e4b644b9955acefdf4f67972adb56dd812947a36db83e1e1df7bcf8ed7539b
|
data/README.md
CHANGED
|
@@ -166,6 +166,22 @@ Html2Pdf.configure do |config|
|
|
|
166
166
|
end
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
+
### API key
|
|
170
|
+
|
|
171
|
+
If your endpoint requires an API key, set `api_key`. It is sent as `Authorization: Bearer <api_key>`.
|
|
172
|
+
When `api_key` is not set, `ENV['HTML2PDF_API_KEY']` is used, so in most cases you only need to set the environment variable.
|
|
173
|
+
Surrounding whitespace (such as a trailing newline) is removed, and a blank `api_key` (`nil`, `''` or only whitespace) is treated as not set.
|
|
174
|
+
A key that contains whitespace raises `Html2Pdf::Rails::InvalidApiKeyError` before any request is sent; the key itself is not included in the message.
|
|
175
|
+
|
|
176
|
+
```ruby
|
|
177
|
+
Html2Pdf.configure do |config|
|
|
178
|
+
config.endpoint = 'YOUR_HTTP_TRIGGER_ENDPOINT'
|
|
179
|
+
config.api_key = Rails.application.credentials.html2pdf_api_key # optional; defaults to ENV['HTML2PDF_API_KEY']
|
|
180
|
+
end
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
If the endpoint rejects the key (HTTP 401), `Html2Pdf::Rails::UnauthorizedError` (a subclass of `Html2Pdf::Rails::RequestError`) is raised. It is not retried.
|
|
184
|
+
|
|
169
185
|
### `html2pdf_base_tag` in mailers / jobs
|
|
170
186
|
|
|
171
187
|
`html2pdf_base_tag` resolves the base URL in this order (matches Rails `url_for`):
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'html2pdf/rails/errors'
|
|
4
|
+
|
|
5
|
+
module Html2Pdf
|
|
6
|
+
module Rails
|
|
7
|
+
module ApiKey
|
|
8
|
+
# Returns the key with surrounding whitespace removed, or nil when it is blank
|
|
9
|
+
# (nil, '' or only whitespace), so that callers can treat blank as "not set".
|
|
10
|
+
def self.normalize(value)
|
|
11
|
+
key = value.to_s.strip
|
|
12
|
+
return nil if key.empty?
|
|
13
|
+
# NOTE: Never include the key in the message. Net::HTTP would otherwise raise its own
|
|
14
|
+
# ArgumentError for a CR/LF inside the header value, and that message contains the value.
|
|
15
|
+
raise InvalidApiKeyError, 'html2pdf api_key must not contain whitespace' if key.match?(/\s/)
|
|
16
|
+
|
|
17
|
+
key
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'html2pdf/rails/errors'
|
|
4
|
+
require 'html2pdf/rails/api_key'
|
|
4
5
|
|
|
5
6
|
module Html2Pdf
|
|
6
7
|
module Rails
|
|
7
8
|
class Client
|
|
8
9
|
def self.post(**options)
|
|
9
|
-
self.new(Html2Pdf.config.endpoint, Html2Pdf.config.app).post(**options)
|
|
10
|
+
self.new(Html2Pdf.config.endpoint, Html2Pdf.config.app, api_key: Html2Pdf.config.api_key).post(**options)
|
|
10
11
|
end
|
|
11
12
|
|
|
12
|
-
def initialize(endpoint, app)
|
|
13
|
+
def initialize(endpoint, app, api_key: nil)
|
|
13
14
|
@uri = URI.parse(endpoint)
|
|
14
15
|
@app = app
|
|
16
|
+
@api_key = Html2Pdf::Rails::ApiKey.normalize(api_key)
|
|
15
17
|
end
|
|
16
18
|
|
|
17
19
|
def post(html:, put_to_storage: false, file_name: nil, disposition: nil, pdf_options: {})
|
|
@@ -30,6 +32,8 @@ module Html2Pdf
|
|
|
30
32
|
case response.code
|
|
31
33
|
when '200'
|
|
32
34
|
response.body
|
|
35
|
+
when '401'
|
|
36
|
+
raise Html2Pdf::Rails::UnauthorizedError.new(response)
|
|
33
37
|
when '503'
|
|
34
38
|
raise Html2Pdf::Rails::ServiceUnavailableError.new(response)
|
|
35
39
|
else
|
|
@@ -42,7 +46,9 @@ module Html2Pdf
|
|
|
42
46
|
private
|
|
43
47
|
|
|
44
48
|
def headers
|
|
45
|
-
{ 'Content-Type' => 'application/json' }
|
|
49
|
+
headers = { 'Content-Type' => 'application/json' }
|
|
50
|
+
headers['Authorization'] = "Bearer #{@api_key}" if @api_key
|
|
51
|
+
headers
|
|
46
52
|
end
|
|
47
53
|
end
|
|
48
54
|
end
|
|
@@ -3,6 +3,10 @@ module Html2Pdf
|
|
|
3
3
|
class NetworkError < StandardError
|
|
4
4
|
end
|
|
5
5
|
|
|
6
|
+
# Raised when the configured API key cannot be sent as a header value.
|
|
7
|
+
class InvalidApiKeyError < ArgumentError
|
|
8
|
+
end
|
|
9
|
+
|
|
6
10
|
class RequestError < StandardError
|
|
7
11
|
attr_reader :response
|
|
8
12
|
|
|
@@ -15,5 +19,14 @@ module Html2Pdf
|
|
|
15
19
|
|
|
16
20
|
class ServiceUnavailableError < RequestError
|
|
17
21
|
end
|
|
22
|
+
|
|
23
|
+
# Raised on HTTP 401. Not retried, because sending the same API key again cannot succeed.
|
|
24
|
+
class UnauthorizedError < RequestError
|
|
25
|
+
def initialize(response, msg = nil)
|
|
26
|
+
msg ||= 'html2pdf request was rejected with HTTP status 401. ' \
|
|
27
|
+
'Check that Html2Pdf.config.api_key (or ENV["HTML2PDF_API_KEY"]) is set to a valid key.'
|
|
28
|
+
super(response, msg)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
18
31
|
end
|
|
19
32
|
end
|
data/lib/html2pdf/rails.rb
CHANGED
|
@@ -1,16 +1,27 @@
|
|
|
1
1
|
require 'retryable'
|
|
2
2
|
require 'html2pdf/rails/version'
|
|
3
3
|
require 'html2pdf/rails/errors'
|
|
4
|
+
require 'html2pdf/rails/api_key'
|
|
4
5
|
require 'html2pdf/rails/client'
|
|
5
6
|
require 'html2pdf/rails/railtie'
|
|
6
7
|
|
|
7
8
|
module Html2Pdf
|
|
8
9
|
class Config
|
|
9
10
|
attr_accessor :endpoint, :app
|
|
11
|
+
attr_writer :api_key
|
|
10
12
|
|
|
11
13
|
def initialize
|
|
12
14
|
@endpoint = nil
|
|
13
15
|
@app = nil
|
|
16
|
+
@api_key = nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Falls back to ENV at read time (not in #initialize) so that the key can be
|
|
20
|
+
# set after the config object is built, e.g. by dotenv loading later in boot.
|
|
21
|
+
# A blank value also falls back, because `credentials.xxx.to_s` and the like
|
|
22
|
+
# yield '' when the key is not set.
|
|
23
|
+
def api_key
|
|
24
|
+
Html2Pdf::Rails::ApiKey.normalize(@api_key) || Html2Pdf::Rails::ApiKey.normalize(ENV['HTML2PDF_API_KEY'])
|
|
14
25
|
end
|
|
15
26
|
end
|
|
16
27
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: html2pdf-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- aki77
|
|
@@ -99,6 +99,7 @@ files:
|
|
|
99
99
|
- bin/setup
|
|
100
100
|
- html2pdf-rails.gemspec
|
|
101
101
|
- lib/html2pdf/rails.rb
|
|
102
|
+
- lib/html2pdf/rails/api_key.rb
|
|
102
103
|
- lib/html2pdf/rails/client.rb
|
|
103
104
|
- lib/html2pdf/rails/errors.rb
|
|
104
105
|
- lib/html2pdf/rails/helper.rb
|