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
-
.add_files_to_zip(zipfile, files) ⇒ void
This method receives multiple files, that will be added to a archive.
-
.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.
-
.ensure_thread_running(&block) ⇒ Thread
returns the newly created thread.
-
.escape(text) ⇒ String
Method used to escape a text by escaping some characters like ‘&’, ‘<’ and ‘>’ , which could affect HTML format.
-
.execute_with_rescue(options = {}) ⇒ String, ...
method used to rescue exceptions.
-
.expanded_path(path) ⇒ String
returns the expanded path of a file path.
-
.extract_body(io) ⇒ String
method used to extract the body of a IO object.
-
.force_utf8_encoding(msg) ⇒ String
Returns utf8 encoding of the msg.
-
.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.
-
.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.
-
.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.
-
.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.
-
.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.
-
.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.
-
.log_bactrace(exception) ⇒ void
method used to log exception backtrace.
-
.log_exception(exception) ⇒ void
method used to log exceptions.
-
.logger ⇒ Logger
returns the logger used to log messages and errors.
-
.mount_table_for_hash(hash, options = {}) ⇒ String
This method is used to mount a table from a hash.
-
.path_is_a_template?(path) ⇒ Boolean
checks to see if a path is valid.
-
.permitted_options ⇒ Hash
returns the Hash containing as keys the permitted options and as values their default values.
-
.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.
-
.rails_logger ⇒ Rails::Logger
returns the rails logger.
-
.remove_blank(args) ⇒ Hash, Array
This method can receive either a Hash or an Array, which will be filtered of blank values.
-
.rescue_interrupt ⇒ void
method used to rescue from interrupt and show a message.
-
.root ⇒ String
returns the root path of the gem ( the lib directory ).
-
.run_new_thread ⇒ Thread
method used to create a thread and execute a block.
-
.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.
-
.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.
-
.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.
-
.template_dir ⇒ String
returns the templates directory.
-
.template_path_exist(path) ⇒ String
returns true if file exists or false otherwise.
Methods included from HeredocHelper
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
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
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
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
216 217 218 |
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 216 def escape(text) text.to_s.gsub('&', '&').gsub('<', '<').gsub('>', '>') end |
.execute_with_rescue(options = {}) ⇒ String, ...
method used to rescue exceptions
136 137 138 139 140 141 142 143 |
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 136 def execute_with_rescue( = {}) yield if block_given? rescue Interrupt rescue_interrupt rescue => error log_exception(error) .fetch(:value, '') end |
.expanded_path(path) ⇒ String
returns the expanded path of a file path
40 41 42 |
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 40 def (path) File.(path) end |
.extract_body(io) ⇒ String
method used to extract the body of a IO object
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
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
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.
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
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
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
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
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
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
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 |
.logger ⇒ Logger
returns the logger used to log messages and errors
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’)
246 247 248 249 250 |
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 246 def mount_table_for_hash(hash, = {}) return if hash.blank? rows = get_hash_rows(hash, .fetch('rows', [])) mount_table(rows.unshift(%w(Name Value)), { class: 'name_values' }.merge()) end |
.path_is_a_template?(path) ⇒ Boolean
checks to see if a path is valid
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_options ⇒ 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 { 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
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_logger ⇒ Rails::Logger
returns the 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
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_interrupt ⇒ void
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 |
.root ⇒ 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.(File.dirname(__dir__)) end |
.run_new_thread ⇒ Thread
method used to create a thread and execute a block
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
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
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
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_dir ⇒ String
returns the templates directory
165 166 167 |
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 165 def template_dir File.(File.join(root, 'templates')) end |
.template_path_exist(path) ⇒ String
returns true if file exists or false otherwise
174 175 176 |
# File 'lib/asana_exception_notifier/helpers/application_helper.rb', line 174 def template_path_exist(path) File.exist?((path)) end |