breezy_pdf_lite 0.0.7 → 0.1.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: 7d66b946175176e3984b66d1076c738ec5c73b04dea63a12860d004b02310482
4
- data.tar.gz: e20bbd337b677b0c884f36781468811668d5c47b91b16038627abcaea8d322fe
3
+ metadata.gz: c0d0a2c4ac70b3568da6aa15a499409ca7c4de9b602b6e9f7233fd640eca7d91
4
+ data.tar.gz: e2f46ce4a20952f4e5357a348e4689b2eddf427b6eb613cdb70002e00554342c
5
5
  SHA512:
6
- metadata.gz: 31c7402cf0d7f78de0a70d5a8600cb7fbff7b29c122ff02c1a648464057aaadab37d3d1da9feb284c1298894e6922dc5520d90cf00a2396665df5056522adb0f
7
- data.tar.gz: c741a246d806b735e3c96c070585acb01f1deb0de38b9d3bb6cf44ad2f143b15bb8edbf530fa61c8c73ddd94af696dc5250c9b736325bb2f7c16c026ac8bd403
6
+ metadata.gz: 26b62d5f98e2bb19b80aee67ca53fa4408a2616c9522a0320bd88743a1807f5419a49c9b331039198c034ca3a2ca3db1c6756e58f83239242415620165b196e1
7
+ data.tar.gz: c51834be66ab1e37422d3c8e9652b2a2c987d80608e743d6f9d271fa357665f50d27a2008d7f6ff27a3073dfaaba570a3454ffe145485ac473bc37700486ffe6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- breezy_pdf_lite (0.0.2)
4
+ breezy_pdf_lite (0.0.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -9,10 +9,11 @@ GEM
9
9
  ast (2.4.0)
10
10
  jaro_winkler (1.5.4)
11
11
  minitest (5.14.0)
12
- minitest-stub-const (0.6)
12
+ mocha (1.11.0)
13
13
  parallel (1.19.1)
14
14
  parser (2.7.0.2)
15
15
  ast (~> 2.4.0)
16
+ rack (2.1.1)
16
17
  rainbow (3.0.0)
17
18
  rake (13.0.1)
18
19
  rubocop (0.79.0)
@@ -32,7 +33,8 @@ DEPENDENCIES
32
33
  breezy_pdf_lite!
33
34
  bundler (~> 2.0)
34
35
  minitest (~> 5.0)
35
- minitest-stub-const (~> 0.6)
36
+ mocha (= 1.11)
37
+ rack (~> 2.0)
36
38
  rake (~> 13.0)
37
39
  rubocop (= 0.79.0)
38
40
 
@@ -20,9 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
21
  spec.require_paths = ["lib"]
22
22
 
23
+ spec.add_development_dependency "rack", "~> 2.0"
23
24
  spec.add_development_dependency "bundler", "~> 2.0"
24
25
  spec.add_development_dependency "minitest", "~> 5.0"
25
- spec.add_development_dependency "minitest-stub-const", "~> 0.6"
26
+ spec.add_development_dependency "mocha", "1.11"
26
27
  spec.add_development_dependency "rake", "~> 13.0"
27
28
  spec.add_development_dependency "rubocop", "0.79.0"
28
29
  end
@@ -3,40 +3,10 @@
3
3
  module BreezyPDFLite::Intercept
4
4
  # :nodoc
5
5
  class Base
6
- attr_reader :app, :env
6
+ attr_reader :body
7
7
 
8
- def initialize(app, env)
9
- @app = app
10
- @env = env
11
- end
12
-
13
- private
14
-
15
- def rendered_url
16
- "#{base_url}#{path}#{query_string}"
17
- end
18
-
19
- def requested_url
20
- "#{env['rack.url_scheme']}://#{env['SERVER_NAME']}#{port}" \
21
- "#{env['PATH_INFO']}#{query_string}"
22
- end
23
-
24
- def base_url
25
- "#{env['rack.url_scheme']}://#{env['SERVER_NAME']}#{port}"
26
- end
27
-
28
- def port
29
- ":#{env['SERVER_PORT']}" unless [80, 443].include?(env["SERVER_PORT"].to_i)
30
- end
31
-
32
- def path
33
- env["PATH_INFO"].gsub(/\.pdf/, "")
34
- end
35
-
36
- def query_string
37
- return "" if env["QUERY_STRING"].nil?
38
-
39
- env["QUERY_STRING"] == "" ? "" : "?#{env['QUERY_STRING']}"
8
+ def initialize(body)
9
+ @body = body
40
10
  end
41
11
  end
42
12
  end
@@ -1,62 +1,34 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "rack/request"
4
+ require "rack/file"
5
+
3
6
  module BreezyPDFLite::Intercept
4
- # :nodoc
7
+ # Takes the App's response body, and submits it to the breezypdf lite endpoint
8
+ # resulting in a file. File is then served with Rack::File
5
9
  class HTML < Base
6
10
  def call
7
- raise BreezyPDFLite::Intercept::UnRenderable unless (200..299).cover?(status)
8
-
9
- render_request = BreezyPDFLite::RenderRequest.new(body).submit
11
+ request = Rack::Request.new({})
12
+ path = render_request_file.path
10
13
 
11
- if render_request.code != "201"
12
- raise BreezyPDFLite::RenderError, "Status: #{render_request.status} body: #{render_request.body}"
13
- end
14
-
15
- [
16
- 201,
17
- {
18
- "Content-Type" => "application/pdf",
19
- "Content-Length" => render_request.header["Content-Length"],
20
- "Content-Disposition" => render_request.header["Content-Disposition"]
21
- },
22
- [render_request.body]
23
- ]
24
- rescue BreezyPDFLite::Intercept::UnRenderable
25
- response
14
+ Rack::File.new(path, response_headers).serving(request, path)
26
15
  end
27
16
 
28
17
  private
29
18
 
30
- def status
31
- @status ||= response[0].to_i
32
- end
33
-
34
- def headers
35
- @headers ||= response[1]
36
- end
37
-
38
- def body
39
- if response[2].respond_to?(:join)
40
- response[2].join
41
- elsif response[2].respond_to?(:each)
42
- content = []
43
- response[2].each { |part| content << part }
44
-
45
- content.join
46
- else
47
- response[2]
48
- end
19
+ def render_request
20
+ @render_request ||= BreezyPDFLite::RenderRequest.new(body)
49
21
  end
50
22
 
51
- def response
52
- @response ||= app.call(doctored_env)
23
+ def render_request_file
24
+ @render_request_file ||= render_request.to_file
53
25
  end
54
26
 
55
- def doctored_env
56
- env.dup.tap do |hash|
57
- hash["HTTP_ACCEPT"] = "text/html"
58
- hash["PATH_INFO"] = path
59
- end
27
+ def response_headers
28
+ @response_headers ||= {
29
+ "Content-Type" => "application/pdf",
30
+ "Content-Disposition" => render_request.response.header["Content-Disposition"]
31
+ }
60
32
  end
61
33
  end
62
34
  end
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BreezyPDFLite
4
- # Intercept a Rack request
4
+ # Intercept a Rack request, determining if the app's response should
5
+ # be intercepted or simply returned
5
6
  class Interceptor
6
7
  attr_reader :app, :env
7
8
 
@@ -10,34 +11,50 @@ module BreezyPDFLite
10
11
  @env = env
11
12
  end
12
13
 
13
- def intercept!
14
- if intercept?
15
- intercept.new(@app, @env).call
14
+ def call
15
+ if (200..299).cover?(app_response_status) # Did the app respond well?
16
+ Intercept::HTML.new(app_response_body).call # Try to return a PDF
16
17
  else
17
- app.call(env)
18
+ app_response # Bad app response, just send respond with that
18
19
  end
19
20
  end
20
21
 
21
22
  private
22
23
 
23
- def intercept?
24
- get? && matching_uri?
24
+ def app_response
25
+ @app_response ||= app.call(doctored_env)
25
26
  end
26
27
 
27
- def matching_uri?
28
- matchers.any? { |regex| env["REQUEST_URI"].match?(regex) }
28
+ def app_response_status
29
+ @app_response_status ||= app_response[0].to_i
29
30
  end
30
31
 
31
- def get?
32
- env["REQUEST_METHOD"].match?(/get/i)
32
+ def app_response_headers
33
+ @app_response_headers ||= app_response[1]
33
34
  end
34
35
 
35
- def matchers
36
- @matchers ||= BreezyPDFLite.middleware_path_matchers
36
+ def app_response_body
37
+ if app_response[2].respond_to?(:join)
38
+ app_response[2].join
39
+ elsif app_response[2].respond_to?(:each)
40
+ content = []
41
+ app_response[2].each { |part| content << part }
42
+
43
+ content.join
44
+ else
45
+ app_response[2]
46
+ end
47
+ end
48
+
49
+ def doctored_env
50
+ env.dup.tap do |hash|
51
+ hash["HTTP_ACCEPT"] = "text/html"
52
+ hash["PATH_INFO"] = path
53
+ end
37
54
  end
38
55
 
39
- def intercept
40
- BreezyPDFLite::Intercept::HTML
56
+ def path
57
+ env["PATH_INFO"].gsub(/\.pdf/, "")
41
58
  end
42
59
  end
43
60
  end
@@ -2,13 +2,31 @@
2
2
 
3
3
  module BreezyPDFLite
4
4
  # Rack Middleware for BreezyPDFLite
5
+ # Determines if the request should be intercepted or not
5
6
  class Middleware
6
7
  def initialize(app, _options = {})
7
8
  @app = app
8
9
  end
9
10
 
10
11
  def call(env)
11
- Interceptor.new(@app, env).intercept!
12
+ if intercept?(env)
13
+ Interceptor.new(@app, env).call
14
+ else
15
+ @app.call(env)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ # Is this request applicable?
22
+ def intercept?(env)
23
+ env["REQUEST_METHOD"].match?(/get/i) && matching_uri?(env)
24
+ end
25
+
26
+ def matching_uri?(env)
27
+ BreezyPDFLite.middleware_path_matchers.any? do |regex|
28
+ env["REQUEST_URI"].match?(regex)
29
+ end
12
30
  end
13
31
  end
14
32
  end
@@ -2,18 +2,19 @@
2
2
 
3
3
  module BreezyPDFLite
4
4
  # Request conversion of a HTML string to PDF
5
+ # If the response isn't a 201, raise an error
5
6
  class RenderRequest
6
7
  def initialize(body)
7
8
  @body = body
8
9
  end
9
10
 
10
- def submit
11
- client.post("/render/html", @body)
11
+ def response
12
+ @response ||= submit.tap do |resp|
13
+ raise RenderError, "#{resp.code}: #{resp.body}" if resp.code != "201"
14
+ end
12
15
  end
13
16
 
14
17
  def to_file
15
- raise RenderError, "#{response.code}: #{response.body}" if response.code != "201"
16
-
17
18
  @to_file ||= Tempfile.new(%w[response .pdf]).tap do |file|
18
19
  file.binmode
19
20
  file.write response.body
@@ -24,8 +25,8 @@ module BreezyPDFLite
24
25
 
25
26
  private
26
27
 
27
- def response
28
- @response ||= submit
28
+ def submit
29
+ client.post("/render/html", @body)
29
30
  end
30
31
 
31
32
  def client
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BreezyPDFLite
4
- VERSION = "0.0.7"
4
+ VERSION = "0.1.0"
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breezy_pdf_lite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Westendorf
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-22 00:00:00.000000000 Z
11
+ date: 2020-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -39,19 +53,19 @@ dependencies:
39
53
  - !ruby/object:Gem::Version
40
54
  version: '5.0'
41
55
  - !ruby/object:Gem::Dependency
42
- name: minitest-stub-const
56
+ name: mocha
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - "~>"
59
+ - - '='
46
60
  - !ruby/object:Gem::Version
47
- version: '0.6'
61
+ version: '1.11'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - "~>"
66
+ - - '='
53
67
  - !ruby/object:Gem::Version
54
- version: '0.6'
68
+ version: '1.11'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement