breezy_pdf_lite 0.0.3 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ae2792c57054cfa446d22768ad68811dece14a76f1039d591bf7639fd839b26
4
- data.tar.gz: 17c7077620ab69408359978aa4ce0dcd95a4e889bd5adcdac3bb449781fbeed6
3
+ metadata.gz: 8fbaacf2d5f7019593c6e8bc787f9b4b86e00094c8488a530043404ec10879e3
4
+ data.tar.gz: 9d3705028fb7094808165c1019413eea73cc82b091515a8868e7fb0962e87290
5
5
  SHA512:
6
- metadata.gz: 2c4d30a30a18af0ae9ad1dedc45d00fd1eaedd186133811f486ce0d03a47c4f98c8ca39366c401998cdfa5b9210edf96898ad118bfdc8d9682676be81a55e182
7
- data.tar.gz: 377735a9dfe4755bf5957059426ff3dd8c20be2c130a13cad85d57d2ecd6bf47a154b8159d6876882b96018f44591d508d532e448aa0233a14f75f3960cbb9bd
6
+ metadata.gz: 83b673d531dfe0c3f610a64917f6f9ac4ca78e0566b24e89d02a59106ce06722b641ebf6f3c483e4455d8d35a31772ac3cbb8ef6b64559504833b49c5d77762a
7
+ data.tar.gz: 27afdc98b8847fcee7b880ce81494507da83e7c43eebe4d950a6925f44c57ed7cdb43eedb0917025d14c3bdda10c0c950480b65999f2959c72c14fd31f1b5078
@@ -0,0 +1,44 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ master ]
13
+ pull_request:
14
+ branches: [ master ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+
21
+ steps:
22
+ - uses: actions/checkout@v2
23
+ - name: Set up Ruby
24
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
25
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
26
+ # uses: ruby/setup-ruby@v1
27
+ uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
28
+ with:
29
+ ruby-version: 2.6
30
+ - name: Install dependencies
31
+ run: bundle install
32
+ - name: Run tests
33
+ run: bundle exec rake
34
+
35
+ lint:
36
+
37
+ runs-on: ubuntu-latest
38
+
39
+ steps:
40
+ - name: Rubocop checks
41
+ uses: gimenete/rubocop-action@1.0
42
+ env:
43
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
+
@@ -1,5 +1,9 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.4.2
4
+ - 2.7
5
+ - 2.6
6
+ - 2.5
7
+ - jruby
5
8
  before_install: gem install bundler
9
+ install: bundle install --jobs=3 --retry=3
@@ -1,29 +1,35 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- breezy_pdf_lite (0.0.2)
4
+ breezy_pdf_lite (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.0)
10
- jaro_winkler (1.5.4)
11
- minitest (5.14.0)
12
- minitest-stub-const (0.6)
13
- parallel (1.19.1)
14
- parser (2.7.0.2)
15
- ast (~> 2.4.0)
9
+ ast (2.4.1)
10
+ minitest (5.14.1)
11
+ mocha (1.11.2)
12
+ parallel (1.19.2)
13
+ parser (2.7.1.4)
14
+ ast (~> 2.4.1)
15
+ rack (2.2.3)
16
16
  rainbow (3.0.0)
17
17
  rake (13.0.1)
18
- rubocop (0.79.0)
19
- jaro_winkler (~> 1.5.1)
18
+ regexp_parser (1.7.1)
19
+ rexml (3.2.4)
20
+ rubocop (0.89.1)
20
21
  parallel (~> 1.10)
21
- parser (>= 2.7.0.1)
22
+ parser (>= 2.7.1.1)
22
23
  rainbow (>= 2.2.2, < 4.0)
24
+ regexp_parser (>= 1.7)
25
+ rexml
26
+ rubocop-ast (>= 0.3.0, < 1.0)
23
27
  ruby-progressbar (~> 1.7)
24
- unicode-display_width (>= 1.4.0, < 1.7)
28
+ unicode-display_width (>= 1.4.0, < 2.0)
29
+ rubocop-ast (0.3.0)
30
+ parser (>= 2.7.1.4)
25
31
  ruby-progressbar (1.10.1)
26
- unicode-display_width (1.6.0)
32
+ unicode-display_width (1.7.0)
27
33
 
28
34
  PLATFORMS
29
35
  ruby
@@ -32,9 +38,10 @@ DEPENDENCIES
32
38
  breezy_pdf_lite!
33
39
  bundler (~> 2.0)
34
40
  minitest (~> 5.0)
35
- minitest-stub-const (~> 0.6)
41
+ mocha (= 1.11.2)
42
+ rack (~> 2.0)
36
43
  rake (~> 13.0)
37
- rubocop (= 0.79.0)
44
+ rubocop (= 0.89.1)
38
45
 
39
46
  BUNDLED WITH
40
47
  2.1.2
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ["daniel@prowestech.com"]
12
12
 
13
13
  spec.summary = "Ruby/rack middleware for BreezyPDF Lite. HTML to PDF."
14
- spec.license = "GPLv3"
14
+ spec.license = "GPL-3.0"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
17
  f.match(%r{^(test|spec|features)/})
@@ -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.2"
26
27
  spec.add_development_dependency "rake", "~> 13.0"
27
- spec.add_development_dependency "rubocop", "0.79.0"
28
+ spec.add_development_dependency "rubocop", "0.89.1"
28
29
  end
@@ -17,7 +17,8 @@ module BreezyPDFLite
17
17
  autoload :Middleware, "breezy_pdf_lite/middleware"
18
18
  autoload :RenderRequest, "breezy_pdf_lite/render_request"
19
19
 
20
- BreezyPDFLiteError = Class.new(StandardError)
20
+ Error = Class.new(StandardError)
21
+ RenderError = Class.new(Error)
21
22
 
22
23
  mattr_accessor :secret_api_key
23
24
  @@secret_api_key = nil
@@ -28,6 +29,12 @@ module BreezyPDFLite
28
29
  mattr_accessor :middleware_path_matchers
29
30
  @@middleware_path_matchers = [/\.pdf/]
30
31
 
32
+ mattr_accessor :open_timeout
33
+ @@open_timeout = 30
34
+
35
+ mattr_accessor :read_timeout
36
+ @@read_timeout = 30
37
+
31
38
  def self.setup
32
39
  yield self
33
40
  end
@@ -5,7 +5,12 @@ module BreezyPDFLite
5
5
  class Client
6
6
  def post(path, body)
7
7
  uri = URI.parse(BreezyPDFLite.base_url + path)
8
- http = Net::HTTP.new(uri.host, uri.port).tap { |h| h.use_ssl = uri.scheme == "https" }
8
+
9
+ http = Net::HTTP.new(uri.host, uri.port)
10
+ http.use_ssl = uri.scheme == "https"
11
+ http.open_timeout = BreezyPDFLite.open_timeout
12
+ http.read_timeout = BreezyPDFLite.read_timeout
13
+
9
14
  request = Net::HTTP::Post.new(uri.request_uri, headers)
10
15
 
11
16
  request.body = body
@@ -3,7 +3,7 @@
3
3
  module BreezyPDFLite
4
4
  # :nodoc
5
5
  module Intercept
6
- UnRenderable = Class.new(BreezyPDFLiteError)
6
+ UnRenderable = Class.new(BreezyPDFLite::Error)
7
7
  autoload :Base, "breezy_pdf_lite/intercept/base"
8
8
  autoload :HTML, "breezy_pdf_lite/intercept/html"
9
9
  end
@@ -3,42 +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
- BreezyPDFLite.middleware_path_matchers.reduce(env["PATH_INFO"]) do |path, matcher|
34
- path.gsub(matcher, "")
35
- end
36
- end
37
-
38
- def query_string
39
- return "" if env["QUERY_STRING"].nil?
40
-
41
- env["QUERY_STRING"] == "" ? "" : "?#{env['QUERY_STRING']}"
8
+ def initialize(body)
9
+ @body = body
42
10
  end
43
11
  end
44
12
  end
@@ -1,49 +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
- [
12
- 201,
13
- {
14
- "Content-Type" => "application/pdf",
15
- "Content-Length" => render_request.header["Content-Length"],
16
- "Content-Disposition" => render_request.header["Content-Disposition"]
17
- },
18
- [render_request.body]
19
- ]
20
- rescue BreezyPDFLite::Intercept::UnRenderable
21
- response
14
+ Rack::File.new(path, response_headers).serving(request, path)
22
15
  end
23
16
 
24
17
  private
25
18
 
26
- def status
27
- @status ||= response[0].to_i
28
- end
29
-
30
- def headers
31
- @headers ||= response[1]
32
- end
33
-
34
- def body
35
- @body ||= response[2].respond_to?(:body) ? response[2].body : response[2].join
19
+ def render_request
20
+ @render_request ||= BreezyPDFLite::RenderRequest.new(body)
36
21
  end
37
22
 
38
- def response
39
- @response ||= app.call(doctored_env)
23
+ def render_request_file
24
+ @render_request_file ||= render_request.to_file
40
25
  end
41
26
 
42
- def doctored_env
43
- env.dup.tap do |hash|
44
- hash["HTTP_ACCEPT"] = "text/html"
45
- hash["PATH_INFO"] = path
46
- end
27
+ def response_headers
28
+ @response_headers ||= {
29
+ "Content-Type" => "application/pdf",
30
+ "Content-Disposition" => render_request.response.header["Content-Disposition"]
31
+ }
47
32
  end
48
33
  end
49
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 BreezyPDFLiteError, "#{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
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ # RenderRequest -> Net::HTTPResponse wrapper that is streaming compatable
5
+ class Response
6
+ def initialize(response)
7
+ @response = response
8
+ end
9
+
10
+ def status
11
+ @status ||= @response.code.to_i
12
+ end
13
+
14
+ def headers
15
+ {
16
+ "Content-Type" => "application/pdf",
17
+ "Content-Length" => @response.header["Content-Length"],
18
+ "Content-Disposition" => @response.header["Content-Disposition"]
19
+ }
20
+ end
21
+
22
+ def each(&blk)
23
+ @response.read_body(&blk)
24
+ end
25
+
26
+ def body
27
+ io = StringIO.new
28
+ io.binmode
29
+
30
+ each do |chunk|
31
+ io.write chunk
32
+ end
33
+
34
+ io.flush
35
+ io.rewind
36
+
37
+ io
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BreezyPDFLite
4
- VERSION = "0.0.3"
4
+ VERSION = "0.1.1"
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.3
4
+ version: 0.1.1
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-13 00:00:00.000000000 Z
11
+ date: 2020-08-19 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.2
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.2
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +86,14 @@ dependencies:
72
86
  requirements:
73
87
  - - '='
74
88
  - !ruby/object:Gem::Version
75
- version: 0.79.0
89
+ version: 0.89.1
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - '='
81
95
  - !ruby/object:Gem::Version
82
- version: 0.79.0
96
+ version: 0.89.1
83
97
  description:
84
98
  email:
85
99
  - daniel@prowestech.com
@@ -88,6 +102,7 @@ extensions: []
88
102
  extra_rdoc_files: []
89
103
  files:
90
104
  - ".gitattributes"
105
+ - ".github/workflows/ruby.yml"
91
106
  - ".gitignore"
92
107
  - ".hound.yml"
93
108
  - ".rubocop.yml"
@@ -111,11 +126,12 @@ files:
111
126
  - lib/breezy_pdf_lite/interceptor.rb
112
127
  - lib/breezy_pdf_lite/middleware.rb
113
128
  - lib/breezy_pdf_lite/render_request.rb
129
+ - lib/breezy_pdf_lite/response.rb
114
130
  - lib/breezy_pdf_lite/util.rb
115
131
  - lib/breezy_pdf_lite/version.rb
116
132
  homepage:
117
133
  licenses:
118
- - GPLv3
134
+ - GPL-3.0
119
135
  metadata: {}
120
136
  post_install_message:
121
137
  rdoc_options: []