module AsanaExceptionNotifier # module that is used for formatting numbers using metrics module ApplicationHelper # function that makes the methods incapsulated as utility functions module_function 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: default_template_path } end def multi_request_manager @multi_manager ||= EventMachine::MultiRequest.new end def extract_body(env) return if env.blank? || !env.is_a?(Hash) io = env['rack.input'] io.rewind if io.respond_to?(:rewind) io.read end def show_hash_content(hash) hash.map do |key, value| value.is_a?(Hash) ? show_hash_content(value) : ["#{key}:", value] end.join("\n ") end def tempfile_details(tempfile) file_details = get_extension_and_name_from_file(tempfile) { file: tempfile, path: tempfile.path }.merge(file_details) end # Returns utf8 encoding of the msg # @param [String] msg # @return [String] ReturnsReturns utf8 encoding of the msg def force_utf8_encoding(msg) msg.respond_to?(:force_encoding) && msg.encoding.name != 'UTF-8' ? msg.force_encoding('UTF-8') : msg end # returns the logger used to log messages and errors # # @return [Logger] # # @api public def logger @logger ||= defined?(Rails) ? Rails.logger : ExceptionNotifier.logger end def ensure_eventmachine_running(&block) Thread.abort_on_exception = true register_em_error_handler run_em_reactor(&block) end def register_em_error_handler EM.error_handler do |error| logger.debug '[AsanaExceptionNotifier]: Error during event loop :' logger.debug "[AsanaExceptionNotifier]: #{log_exception(error)}" end end def log_exception(exception) logger.debug exception.inspect log_bactrace(exception) if exception.respond_to?(:backtrace) end def log_bactrace(exception) logger.debug exception.backtrace.join("\n") end def execute_with_rescue(options = {}) yield if block_given? rescue Interrupt rescue_interrupt rescue => error log_exception(error) options.fetch(:value, '') end def rescue_interrupt `stty icanon echo` puts "\n Command was cancelled due to an Interrupt error." end def run_em_reactor Thread.new do EM.run do EM.defer proc { yield if block_given? } end end.join end def template_dir File.expand_path(File.join(root, 'note_templates')) end def default_template_path File.join(template_dir, 'asana_exception_notifier.html.erb') end def template_path_exist(path) return path if File.exist?(path) fail ArgumentError, "file #{path} doesn't exist" end def max_length(rows, index) value = rows.max_by { |array| array[index].to_s.size } value.is_a?(Array) ? value[index] : value end def get_hash_rows(hash, rows = [], prefix = '') hash.each do |key, value| if value.is_a?(Hash) get_hash_rows(value, rows, key) else rows.push(["#{prefix}#{key}".inspect, escape(value.inspect)]) end end rows end def link_helper(link) <<-LINK #{link.camelize} LINK end def escape(text) text.gsub('&', '&').gsub('<', '<').gsub('>', '>') end def set_fieldset_key(links, prefix) links[prefix] ||= {} prefix end def parse_fieldset_value(options) value = options[:value] value.is_a?(Hash) ? value.reject! { |_new_key, new_value| new_value.is_a?(Hash) } : value end # Gets a bidimensional array and create a table. # The first array is used as label. # def mount_table(array, options = {}) return '' if array.blank? header = array.shift header = header.map { |name| escape(name.to_s.humanize) } rows = array.map { |name| "
#{header.join(' | ')} |
---|