breezy_pdf_lite 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+ require "breezy_pdf_lite"
5
+ require "fileutils"
6
+
7
+ html = File.read(File.expand_path("ex.html", __dir__))
8
+
9
+ BreezyPDFLite.setup do |config|
10
+ config.secret_api_key = ENV["BREEZYPDF_SECRET_API_KEY"]
11
+ config.base_url = ENV.fetch("BREEZYPDF_BASE_URL", "http://localhost:5001")
12
+ config.middleware_path_matchers = [/as-pdf.pdf/]
13
+ end
14
+
15
+ response = BreezyPDFLite::RenderRequest.new(html).submit
16
+
17
+ if response.code.to_i == 201
18
+ path = File.expand_path("example.pdf", __dir__)
19
+
20
+ FileUtils.rm(File.expand_path("example.pdf", __dir__)) if File.exist?(path)
21
+ File.new(path, "w").tap do |file|
22
+ file.write(response.body)
23
+
24
+ file.flush
25
+ file.close
26
+ end
27
+
28
+ puts "Downloaded to #{path}"
29
+ else
30
+ puts "Unable to render to PDF, server responded with #{response.code}"
31
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+ require "net/http"
5
+
6
+ require "breezy_pdf_lite/version"
7
+ require "breezy_pdf_lite/util"
8
+
9
+ # :nodoc
10
+ module BreezyPDFLite
11
+ extend BreezyPDFLite::Util
12
+
13
+ autoload :Client, "breezy_pdf_lite/client"
14
+ autoload :Intercept, "breezy_pdf_lite/intercept"
15
+ autoload :Interceptor, "breezy_pdf_lite/interceptor"
16
+ autoload :Middleware, "breezy_pdf_lite/middleware"
17
+ autoload :RenderRequest, "breezy_pdf_lite/render_request"
18
+
19
+ BreezyPDFLiteError = Class.new(StandardError)
20
+
21
+ mattr_accessor :secret_api_key
22
+ @@secret_api_key = nil
23
+
24
+ mattr_accessor :base_url
25
+ @@base_url = "https://localhost:5001/"
26
+
27
+ mattr_accessor :middleware_path_matchers
28
+ @@middleware_path_matchers = [/\.pdf/]
29
+
30
+ def self.setup
31
+ yield self
32
+ end
33
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ # HTTP Client for BreezyPDFLite API
5
+ class Client
6
+ def post(path, body)
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" }
9
+ request = Net::HTTP::Post.new(uri.request_uri, headers)
10
+
11
+ request.body = body
12
+
13
+ http.request(request)
14
+ end
15
+
16
+ private
17
+
18
+ def headers
19
+ {
20
+ "Authorization": "Bearer #{BreezyPDFLite.secret_api_key}"
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ # :nodoc
5
+ module Intercept
6
+ UnRenderable = Class.new(BreezyPDFLiteError)
7
+ autoload :Base, "breezy_pdf_lite/intercept/base"
8
+ autoload :HTML, "breezy_pdf_lite/intercept/html"
9
+ end
10
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite::Intercept
4
+ # :nodoc
5
+ class Base
6
+ attr_reader :app, :env
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']}"
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite::Intercept
4
+ # :nodoc
5
+ class HTML < Base
6
+ def call
7
+ raise BreezyPDFLite::Intercept::UnRenderable unless (200..299).cover?(status)
8
+
9
+ render_request = BreezyPDFLite::RenderRequest.new(body).submit
10
+
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
22
+ end
23
+
24
+ private
25
+
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
36
+ end
37
+
38
+ def response
39
+ @response ||= app.call(doctored_env)
40
+ end
41
+
42
+ def doctored_env
43
+ env.dup.tap do |hash|
44
+ hash["HTTP_ACCEPT"] = "text/html"
45
+ hash["PATH_INFO"] = path
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ # Intercept a Rack request
5
+ class Interceptor
6
+ attr_reader :app, :env
7
+
8
+ def initialize(app, env)
9
+ @app = app
10
+ @env = env
11
+ end
12
+
13
+ def intercept!
14
+ if intercept?
15
+ intercept.new(@app, @env).call
16
+ else
17
+ app.call(env)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def intercept?
24
+ get? && matching_uri?
25
+ end
26
+
27
+ def matching_uri?
28
+ matchers.any? { |regex| env["REQUEST_URI"].match?(regex) }
29
+ end
30
+
31
+ def get?
32
+ env["REQUEST_METHOD"].match?(/get/i)
33
+ end
34
+
35
+ def matchers
36
+ @matchers ||= BreezyPDFLite.middleware_path_matchers
37
+ end
38
+
39
+ def intercept
40
+ BreezyPDFLite::Intercept::HTML
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ # Rack Middleware for BreezyPDFLite
5
+ class Middleware
6
+ def initialize(app, _options = {})
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ Interceptor.new(@app, env).intercept!
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ # Request conversion of a HTML string to PDF
5
+ class RenderRequest
6
+ def initialize(body)
7
+ @body = body
8
+ end
9
+
10
+ def submit
11
+ client.post("/render/html", @body)
12
+ end
13
+
14
+ private
15
+
16
+ def client
17
+ @client ||= Client.new
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ # Utility methods
5
+ module Util
6
+ def mattr_reader(*syms)
7
+ syms.each do |sym|
8
+ raise NameError, "invalid attribute name: #{sym}" unless /\A[_A-Za-z]\w*\z/.match?(sym)
9
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
10
+ @@#{sym} = nil unless defined? @@#{sym}
11
+ def self.#{sym}
12
+ @@#{sym}
13
+ end
14
+ EOS
15
+
16
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
17
+ def #{sym}
18
+ @@#{sym}
19
+ end
20
+ EOS
21
+ class_variable_set("@@#{sym}", yield) if block_given?
22
+ end
23
+ end
24
+
25
+ def mattr_writer(*syms)
26
+ syms.each do |sym|
27
+ raise NameError, "invalid attribute name: #{sym}" unless /\A[_A-Za-z]\w*\z/.match?(sym)
28
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
29
+ @@#{sym} = nil unless defined? @@#{sym}
30
+ def self.#{sym}=(obj)
31
+ @@#{sym} = obj
32
+ end
33
+ EOS
34
+
35
+ class_eval(<<-EOS, __FILE__, __LINE__ + 1)
36
+ def #{sym}=(obj)
37
+ @@#{sym} = obj
38
+ end
39
+ EOS
40
+ send("#{sym}=", yield) if block_given?
41
+ end
42
+ end
43
+
44
+ def mattr_accessor(*syms, &blk)
45
+ mattr_reader(*syms, &blk)
46
+ mattr_writer(*syms)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BreezyPDFLite
4
+ VERSION = "0.0.1"
5
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: breezy_pdf_lite
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Westendorf
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest-stub-const
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: '0.54'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: '0.54'
83
+ description:
84
+ email:
85
+ - daniel@prowestech.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".hound.yml"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - breezy_pdf_lite.gemspec
102
+ - example/config.ru
103
+ - example/ex.html
104
+ - example/pragmatic.rb
105
+ - lib/breezy_pdf_lite.rb
106
+ - lib/breezy_pdf_lite/client.rb
107
+ - lib/breezy_pdf_lite/intercept.rb
108
+ - lib/breezy_pdf_lite/intercept/base.rb
109
+ - lib/breezy_pdf_lite/intercept/html.rb
110
+ - lib/breezy_pdf_lite/interceptor.rb
111
+ - lib/breezy_pdf_lite/middleware.rb
112
+ - lib/breezy_pdf_lite/render_request.rb
113
+ - lib/breezy_pdf_lite/util.rb
114
+ - lib/breezy_pdf_lite/version.rb
115
+ homepage:
116
+ licenses:
117
+ - GPLv3
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.6.13
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Ruby/rack middleware for BreezyPDF Lite. HTML to PDF.
139
+ test_files: []