Class: PdfMage::Workers::Base
- Inherits:
-
Object
- Object
- PdfMage::Workers::Base
- Includes:
- Sidekiq::Worker
- Defined in:
- lib/pdf_mage/workers/base.rb
Overview
Base worker class that configures Sidekiq options and all workers extend.
Direct Known Subclasses
Constant Summary collapse
- STRIP_STRING_OPTIONS =
Options for the strip string method, for use with the String#encode method.
{ invalid: :replace, undef: :replace, replace: '', universal_newline: true }.freeze
Instance Method Summary collapse
-
#ensure_directory_exists_for_pdf(filename) ⇒ NilClass
Creates directories in the filesystem for the given filename, so that writing a file to that location succeeds.
-
#pdf_filename(pdf_id) ⇒ String
Generates a filename for a unique PDF identifier using the pdf directory specified in the config and the given pdf id.
-
#secretize_url(url) ⇒ String
Adds the API secret to a URL.
-
#string_exists?(string) ⇒ TrueClass, FalseClass
Checks if the given string is not nil and is not empty, like ActiveSupport’s String#present?.
-
#strip_string(string) ⇒ String, NilClass
Removes all non-ASCII characters from a string.
Instance Method Details
#ensure_directory_exists_for_pdf(filename) ⇒ NilClass
Creates directories in the filesystem for the given filename, so that writing a file to that location succeeds.
30 31 32 33 34 35 36 37 38 |
# File 'lib/pdf_mage/workers/base.rb', line 30 def ensure_directory_exists_for_pdf(filename) unless string_exists?(filename) raise ArgumentError, 'filename must be a string that includes at least 1 ASCII character.' end directory_path = filename.split('/').slice(0..-2).join('/') FileUtils.mkdir_p(directory_path) if string_exists?(directory_path) nil end |
#pdf_filename(pdf_id) ⇒ String
Generates a filename for a unique PDF identifier using the pdf directory specified in the config and the given pdf id.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pdf_mage/workers/base.rb', line 48 def pdf_filename(pdf_id) return @filename if defined?(@filename) unless string_exists?(pdf_id) raise ArgumentError, 'pdf_id must be a string that includes at least 1 ASCII character.' end unless string_exists?(CONFIG.pdf_directory) raise ArgumentError, ' The pdf_directory in your config.yml must be a string that includes at least 1 ASCII character. ' end filename = "#{CONFIG.pdf_directory}/#{pdf_id}" filename += '.pdf' unless pdf_id.end_with?('.pdf') @filename = filename end |
#secretize_url(url) ⇒ String
Adds the API secret to a URL.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pdf_mage/workers/base.rb', line 73 def secretize_url(url) unless string_exists?(url) && (uri = URI(url)) && uri.scheme&.match(/^https?$/) raise ArgumentError, 'url must be a valid url using the http/s protocol.' end if CONFIG.api_secret new_query_params = URI.decode_www_form(uri.query.to_s) << ['secret', CONFIG.api_secret] uri.query = URI.encode_www_form(new_query_params) uri.to_s else url end end |
#string_exists?(string) ⇒ TrueClass, FalseClass
Checks if the given string is not nil and is not empty, like ActiveSupport’s String#present?
91 92 93 |
# File 'lib/pdf_mage/workers/base.rb', line 91 def string_exists?(string) !string.nil? && !string.empty? end |
#strip_string(string) ⇒ String, NilClass
Removes all non-ASCII characters from a string.
99 100 101 |
# File 'lib/pdf_mage/workers/base.rb', line 99 def strip_string(string) string&.encode(Encoding.find('ASCII'), STRIP_STRING_OPTIONS) end |