Module: AsanaExceptionNotifier::ApplicationHelper

Includes:
HeredocHelper
Included in:
ErrorPage, UnsafeFilter, ExceptionNotifier::AsanaNotifier
Defined in:
lib/asana_exception_notifier/helpers/application_helper.rb

Overview

module that is used for formatting numbers using metrics

Class Method Summary collapse

Methods included from HeredocHelper

link_helper, mount_table

Class Method Details

.add_files_to_zip(zipfile, files) ⇒ void

This method returns an undefined value.

This method receives multiple files, that will be added to a archive

Parameters:

  • zipfile (::Zip::File)

    The archive that will be used to add files to it

  • files (Array<File>)

    The Array of files that will be added to the archive



353
354
355
356
357
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 353

def add_files_to_zip(zipfile, files)
  files.each do |file|
    zipfile.add(file.sub(File.dirname(file) + '/', ''), file)
  end
end

.archive_files(directory, name, files) ⇒ Zip::File

This method receives multiple files, that will be added to a archive and will return the resulting archive

Parameters:

  • directory (String)

    The directory where the archive will be created

  • name (String)

    The name of the archive ( without the .zip extension )

  • files (Array<File>)

    The Array of files that will be added to the archive

Returns:

  • (Zip::File)

    returns the archive that was created after each of the files were added to the archive and compressed

See Also:

  • #prepare_archive_creation
  • #add_files_to_zip
  • Zip::File::open


340
341
342
343
344
345
346
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 340

def archive_files(directory, name, files)
  archive = prepare_archive_creation(directory, name)
  ::Zip::File.open(archive, Zip::File::CREATE) do |zipfile|
    add_files_to_zip(zipfile, files)
  end
  archive
end

.ensure_thread_running(&block) ⇒ Thread

returns the newly created thread

Parameters:

  • &block (Proc)

    the block that the new thread will execute

Returns:

  • (Thread)

    returns the newly created thread

See Also:

  • #run_new_thread
  • Thread#abort_on_exception


106
107
108
109
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 106

def ensure_thread_running(&block)
  Thread.abort_on_exception = true
  run_new_thread(&block)
end

.escape(text) ⇒ String

Method used to escape a text by escaping some characters like ‘&’, ‘<’ and ‘>’ , which could affect HTML format

Parameters:

  • text (#to_s)

    The text that will be escaped

Returns:

  • (String)

    Returns the text HTML escaped



216
217
218
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 216

def escape(text)
  text.to_s.gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;')
end

.execute_with_rescue(options = {}) ⇒ String, ...

method used to rescue exceptions

Parameters:

  • options (Hash) (defaults to: {})

    Additional options used for returning values when a exception occurs, or empty string

Returns:

  • (String, nil, Object)

    Returns nil if the exception is a interrupt or a String empty if no value was provided in the options hash or the value from the options

See Also:

  • #rescue_interrupt
  • #log_exception


136
137
138
139
140
141
142
143
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 136

def execute_with_rescue(options = {})
  yield if block_given?
rescue Interrupt
  rescue_interrupt
rescue => error
  log_exception(error)
  options.fetch(:value, '')
end

.expanded_path(path) ⇒ String

returns the expanded path of a file path

Parameters:

  • path (String)

    The file path that will be expanded

Returns:

  • (String)

    returns the expanded path of a file path



40
41
42
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 40

def expanded_path(path)
  File.expand_path(path)
end

.extract_body(io) ⇒ String

method used to extract the body of a IO object

Parameters:

  • io (IO)

    The IO object that will be used

Returns:

  • (String)

    returns the body of the IO object by rewinding it and reading the content, or executes inspect if a exception happens



57
58
59
60
61
62
63
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 57

def extract_body(io)
  return unless io.respond_to?(:rewind)
  io.rewind
  io.read
rescue
  io.inspect
end

.force_utf8_encoding(msg) ⇒ String

Returns utf8 encoding of the msg

Parameters:

  • msg (String)

Returns:

  • (String)

    Returns utf8 encoding of the msg



81
82
83
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 81

def force_utf8_encoding(msg)
  msg.respond_to?(:force_encoding) && msg.encoding.name != 'UTF-8' ? msg.force_encoding('UTF-8') : msg
end

.get_extension_and_name_from_file(tempfile) ⇒ Hash

returns the extension of the file, the filename and the file path of the Tempfile file received as argument

Parameters:

  • tempfile (Tempfile)

    the Tempfile that will be used

Returns:

  • (Hash)

    returns the extension of the file, the filename and the file path of the Tempfile file received as argument



304
305
306
307
308
309
310
311
312
313
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 304

def get_extension_and_name_from_file(tempfile)
  path = tempfile.respond_to?(:path) ? tempfile.path : tempfile
  pathname = Pathname.new(path)
  extension = pathname.extname
  {
    extension: extension,
    filename: File.basename(pathname, extension),
    file_path: path
  }
end

.get_hash_rows(hash, rows = []) ⇒ Array<Array<String>>

Method used to construct table rows from a Hash, by constructing an array of arrays with two elements ( First is the key and the value ) This is useful for constructing the table, the number of elements in a array means the number of columns of the table

This is a recursive function if the Hash contains other Hash values.

Parameters:

  • hash (Hash)

    the Hash that wil be used to construct the array of arrays with two columns

  • rows (Array<Array<String>>) (defaults to: [])

    This is the array that will contain the result ( Default: empty array).

Returns:

  • (Array<Array<String>>)

    Returns an array of arrays (with two elements), useful for printing tables from a Hash

See Also:

  • #inspect_value


188
189
190
191
192
193
194
195
196
197
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 188

def get_hash_rows(hash, rows = [])
  hash.each do |key, value|
    if value.is_a?(Hash)
      get_hash_rows(value, rows)
    else
      rows.push([inspect_value(key), inspect_value(value)])
    end
  end
  rows
end

.get_table_headers(header) ⇒ String

This method is used to construct the Th header elements that can be used on HTML table from a array, by humanizing and escaping the values

Parameters:

  • header (#map)

    the Header array that will be used to construct the Th header elements that can be used on HTML table

Returns:

  • (String)

    Returns the HTML th elements constructed from the array , that can be used on a HTML table

See Also:

  • #escape


279
280
281
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 279

def get_table_headers(header)
  header.map { |name| escape(name.to_s.humanize) }.join('</th><th>')
end

.get_table_rows(array) ⇒ String

This method is to construct a HTML row for each value that exists in the array, each value from the array is a array itself. The row is constructed by joining the values from each array with td element, so the result will be a valid HTML row element The final result is a concatenation of multiple row elements that can be displayed inside a tbody element from a HTML table

Parameters:

  • array (#map)

    The Array that will be used to construct the inner rows of a HTML table

Returns:

  • (String)

    Returns a concatenation of multiple HTML tr and td elements that are in fact the inner rows of HTML table



289
290
291
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 289

def get_table_rows(array)
  array.map { |name| "<tr><td>#{name.join('</td><td>')}</td></tr>" }.join
end

.hash_to_html_attributes(hash) ⇒ String

This method receives a options list which will be used to construct a string which will be used to set HTML attributes on a HTML element

Parameters:

  • hash (Hash)

    The Hash that will be used to construct the string of HTML attributes

Returns:

  • (String)

    Returns the string of HTML attributes which can be used on any HTML element



257
258
259
260
261
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 257

def hash_to_html_attributes(hash)
  hash.map do |key, value|
    "#{key}=\"#{value.gsub('"', '\"')}\" "
  end.join(' ')
end

.inspect_value(value) ⇒ String

Method used to inspect a value, by checking if is a IO object, and in that case extract the body from the IO object, otherwise will just use the “inpspect” method. The final result will be escaped so that it can be printed in HTML

Parameters:

  • value (#inspect, #to_s)

    The value that will be inspected and escaped

Returns:

  • (String)

    Returns the value inspected and escaped

See Also:

  • #extract_body
  • #escape


207
208
209
210
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 207

def inspect_value(value)
  inspected_value = value.is_a?(IO) ? extract_body(value) : value.inspect
  escape(inspected_value)
end

.log_bactrace(exception) ⇒ void

This method returns an undefined value.

method used to log exception backtrace

Parameters:

  • exception (Exception)

    the exception that will be used



125
126
127
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 125

def log_bactrace(exception)
  logger.debug exception.backtrace.join("\n")
end

.log_exception(exception) ⇒ void

This method returns an undefined value.

method used to log exceptions

Parameters:

  • exception (Exception)

    the exception that will be used

See Also:

  • #log_bactrace


116
117
118
119
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 116

def log_exception(exception)
  logger.debug exception.inspect
  log_bactrace(exception) if exception.respond_to?(:backtrace)
end

.loggerLogger

returns the logger used to log messages and errors

Returns:

  • (Logger)


88
89
90
91
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 88

def logger
  @logger ||= (defined?(Rails) && rails_logger.present? ? rails_logger : ExceptionNotifier.logger)
  @logger = @logger.present? ? @logger : Logger.new(STDOUT)
end

.mount_table_for_hash(hash, options = {}) ⇒ String

This method is used to mount a table from a hash. After the table is mounted, since the generated table has two columns ( Array of array with two elements), We’re going to prepend to this generated table a array with two elements (Name and Value) , which will be the columns headers on the generated table . We also will add a HTML class attribute to the generated table (‘name_values’)

Parameters:

  • hash (Hash)

    The Hash that will be used to mount a table from the keys and values

  • options (Hash) (defaults to: {})

    Additional options that will be used to set HTML attributes on the generated table

Returns:

  • (String)

    Returns the HTML table generated as a string, which can be printed anywhere

See Also:

  • #get_hash_rows
  • HeredocHelper#mount_table


246
247
248
249
250
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 246

def mount_table_for_hash(hash, options = {})
  return if hash.blank?
  rows = get_hash_rows(hash, options.fetch('rows', []))
  mount_table(rows.unshift(%w(Name Value)), { class: 'name_values' }.merge(options))
end

.path_is_a_template?(path) ⇒ Boolean

checks to see if a path is valid

Parameters:

  • path (String)

    The file path that will be used

Returns:

  • (Boolean)

    returns true if the path is valid otherwise false

See Also:

  • #template_path_exist


49
50
51
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 49

def path_is_a_template?(path)
  path.present? && template_path_exist(path)
end

.permitted_optionsHash

returns the Hash containing as keys the permitted options and as values their default values

Returns:

  • (Hash)

    Returns the Hash containing as keys the permitted options and as values their default values



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 14

def permitted_options
  {
    asana_api_key:  nil,
    workspace: nil,
    assignee:  nil,
    assignee_status: nil,
    due_at: nil,
    due_on: nil,
    hearted: false,
    hearts: [],
    projects: [],
    followers: [],
    memberships: [],
    tags: [],
    notes: '',
    name: '',
    template_path: nil,
    unsafe_options: []
  }
end

.prepare_archive_creation(directory, name) ⇒ String

This method prepares the creation of a archive, by making sure that the directory is created and if the archive already exists, will be removed, and the path to where this archive needs to be created will be returned

Parameters:

  • directory (String)

    The directory where the archive should be created

  • name (String)

    The name of the archive ( without the .zip extension )

Returns:

  • (String)

    returns the path to where this archive needs to be created



365
366
367
368
369
370
371
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 365

def prepare_archive_creation(directory, name)
  archive = File.join(directory, name + '.zip')
  archive_dir = File.dirname(archive)
  FileUtils.mkdir_p(archive_dir) unless File.directory?(archive_dir)
  FileUtils.rm archive, force: true if File.exist?(archive)
  archive
end

.rails_loggerRails::Logger

returns the rails logger

Returns:

  • (Rails::Logger)


96
97
98
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 96

def rails_logger
  Rails.logger
end

.remove_blank(args) ⇒ Hash, Array

This method can receive either a Hash or an Array, which will be filtered of blank values

Parameters:

  • args (Hash, Array)

    The Hash or the array which will be used for filtering blank values

Returns:

  • (Hash, Array)

    Returns the Hash or the array received , filtered of blank values



268
269
270
271
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 268

def remove_blank(args)
  args.delete_if { |_key, value| value.blank? } if args.is_a?(Hash)
  args.reject!(&:blank?) if args.is_a?(Array)
end

.rescue_interruptvoid

This method returns an undefined value.

method used to rescue from interrupt and show a message



148
149
150
151
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 148

def rescue_interrupt
  `stty icanon echo`
  puts "\n Command was cancelled due to an Interrupt error."
end

.rootString

returns the root path of the gem ( the lib directory )

Returns:

  • (String)

    Returns the root path of the gem ( the lib directory )



296
297
298
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 296

def root
  File.expand_path(File.dirname(__dir__))
end

.run_new_threadThread

method used to create a thread and execute a block

Returns:

  • (Thread)

    returns the newly created thread

See Also:

  • Thread#new


156
157
158
159
160
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 156

def run_new_thread
  Thread.new do
    yield if block_given?
  end.join
end

.set_fieldset_key(links, prefix, default) ⇒ String

Method used to set the prefix name on the links Hash, this is needed when building a table from a hash, because when going through the first level of a Hash, we don’t have a title of what this level is about , but deeper levels can have a title, by using the key to which the value is associated

Because of this this method expects a default name, in case the prefix is blank

Parameters:

  • links (Hash)

    The links Hash object that will be used to print the fieldsets links in HTML template

  • prefix (String)

    The prefix that will be set as key on the links Hash and associated with a empty Hash , if the links hash does not have this key yet

  • default (String)

    The default prefix that will be set as key on the links Hash and associated with a empty Hash , if the links hash does not have this key yet and the prefix is blank

Returns:

  • (String)

    Returns the prefix that was used to set the key on the links Hash, either the ‘prefix’ variable or the ‘default’ variable



230
231
232
233
234
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 230

def set_fieldset_key(links, prefix, default)
  prefix_name = prefix.present? ? prefix : default
  links[prefix_name] ||= {}
  prefix_name
end

.split_archive(archive, partial_name, segment_size) ⇒ Array<String>

Splits a archive into multiple archives if the size of the archive is greater than the segment_size received as argument and returns a array that contains the paths to each of the archives that were resulted after splitting

Parameters:

  • archive (::Zip::File)

    the archive that will try to be splitted

  • partial_name (String)

    the partial name that will be used when splitting the archives

  • segment_size (Integer)

    the size that will be used for splitting the archive

Returns:

  • (Array<String>)

    returns a array that contains the paths to each of the archives that were resulted after splitting



322
323
324
325
326
327
328
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 322

def split_archive(archive, partial_name, segment_size)
  indexes = Zip::File.split(archive, segment_size, true, partial_name)
  archives = Array.new(indexes) do |index|
    File.join(File.dirname(archive), "#{partial_name}.zip.#{format('%03d', index + 1)}")
  end if indexes.present?
  archives.blank? ? [archive] : archives
end

.tempfile_details(tempfile) ⇒ Hash

method used to return the file and the path of a tempfile , along with the extension and the name of the file

Parameters:

  • tempfile (Tempfile)

    the temporary file that will be used

Returns:

  • (Hash)

    returns the the file and the path of a tempfile , along with the extension and the name of the file

See Also:

  • #get_extension_and_name_from_file


70
71
72
73
74
75
76
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 70

def tempfile_details(tempfile)
  file_details = get_extension_and_name_from_file(tempfile)
  {
    file: tempfile,
    path:  tempfile.path
  }.merge(file_details)
end

.template_dirString

returns the templates directory

Returns:

  • (String)

    returns the path to the templates directory

See Also:

  • #root


165
166
167
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 165

def template_dir
  File.expand_path(File.join(root, 'templates'))
end

.template_path_exist(path) ⇒ String

returns true if file exists or false otherwise

Returns:

  • (String)

    returns the path to the templates directory

See Also:

  • File#exist?


174
175
176
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 174

def template_path_exist(path)
  File.exist?(expanded_path(path))
end