Class: PdfMage::Workers::Base

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker
Defined in:
lib/pdf_mage/workers/base.rb

Overview

Base worker class that configures Sidekiq options and all workers extend.

Since:

  • 0.1.0

Direct Known Subclasses

RenderPdf, SendWebhook, UploadFile

Constant Summary collapse

STRIP_STRING_OPTIONS =

Options for the strip string method, for use with the String#encode method.

Since:

  • 0.1.0

{
  invalid: :replace,
  undef: :replace,
  replace: '',
  universal_newline: true
}.freeze

Instance Method Summary collapse

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.

Parameters:

  • filename (String)
    • string that represents the path the PDF will be created at

Returns:

  • (NilClass)

Raises:

  • (ArgumentError)

    if filename is nil or an empty string

Since:

  • 0.1.0



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.

Parameters:

  • pdf_id (String)
    • PDF identifier to make filename from

Returns:

  • (String)

    filename to store PDF at

Raises:

  • (ArgumentError)

    if pdf_id is nil or an empty string

  • (ArgumentError)

    if CONFIG.pdf_directory is nil or an empty string

Since:

  • 0.1.0



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.

Parameters:

  • url (String)
    • URL to add the API secret from the config to

Returns:

  • (String)

    url with secret

Raises:

  • (ArgumentError)

    if url is nil or an empty string

Since:

  • 0.1.0



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?

Parameters:

  • string (String)
    • string to strip non-ASCII characters from

Returns:

  • (TrueClass, FalseClass)

    boolean of if the string is present or not

Since:

  • 0.1.0



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.

Parameters:

  • string (String)
    • string to strip non-ASCII characters from

Returns:

  • (String, NilClass)

    string with non-ASCII characters removed or nil if given nil

Since:

  • 0.1.0



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