require_relative './heredoc_helper' module AsanaExceptionNotifier # module that is used for formatting numbers using metrics module ApplicationHelper include AsanaExceptionNotifier::HeredocHelper # 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: nil, unsafe_options: [] } end def expanded_path(path) File.expand_path(path) end def path_is_a_template?(path) path.present? && template_path_exist(expanded_path(path)) end def multi_request_manager @multi_manager ||= EventMachine::MultiRequest.new end def extract_body(io) return unless io.respond_to?(:rewind) io.rewind io.read 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, 'templates')) end def template_path_exist(path) File.exist?(path) 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 inspect_value(value) value.is_a?(IO) ? extract_body(value) : value end def escape(text) text.gsub('&', '&').gsub('<', '<').gsub('>', '>') end def set_fieldset_key(links, prefix, default) prefix_name = prefix.present? ? prefix : default links[prefix_name] ||= {} prefix_name 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 def coerce_object_to_hash(object) hash = {} object.instance_variables.each do |name| hash[name.to_s[1..-1]] = object.instance_variable_get(name) end hash end # Mount table for hash, using name and value and adding a name_value class # to the generated table. # 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 def hash_to_html_attributes(hash) hash.map do |key, value| "#{key}=\"#{value.gsub('"', '\"')}\" " end.join(' ') end 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 def get_table_headers(header) header.map { |name| escape(name.to_s.humanize) }.join('