module Muzak::Utils
A collection of convenience utilities for use throughout muzak.
Public Class Methods
Tests whether the given filename is likely to be album art. @param filename [String] the filename to test @return [Boolean] whether or not the file is an art file
# File lib/muzak/utils.rb, line 16 def self.album_art?(filename) File.basename(filename) =~ Config::ALBUM_ART_REGEX end
Tests whether the given filename is likely to be music. @param filename [String] the filename to test @return [Boolean] whether or not the file is a music file
# File lib/muzak/utils.rb, line 9 def self.music?(filename) Config::MUSIC_SUFFIXES.include?(File.extname(filename.downcase)) end
Tests whether the given utility is available in the system path. @param util [String] the utility to test @return [Boolean] whether or not the utility is available
# File lib/muzak/utils.rb, line 23 def self.which?(util) ENV["PATH"].split(File::PATH_SEPARATOR).any? do |path| File.executable?(File.join(path, util)) end end
Public Instance Methods
Returns a response hash containing the given data and error. @param error [String] the error string, if needed @param data [String, Hash] the data, if needed
# File lib/muzak/utils.rb, line 108 def build_response(error: nil, data: nil) { response: { error: error, data: data, method: caller_locations.first.label, }, } end
Outputs a boxed warning message. @param args [Array<String>] the message(s) @return [void]
# File lib/muzak/utils.rb, line 67 def danger(*args) output pretty("warn", :yellow), args end
Outputs a boxed debugging message. @param args [Array<String>] the message(s) @return [void]
# File lib/muzak/utils.rb, line 90 def debug(*args) return unless debug? context = is_a?(Module) ? name : self.class.name output pretty("debug", :yellow), "[#{context}]", args end
@return [Boolean] whether or not muzak is running in debug mode
# File lib/muzak/utils.rb, line 30 def debug? Config.debug end
Outputs a boxed error message. @param args [Array<String>] the message(s) @return [void]
# File lib/muzak/utils.rb, line 74 def error(*args) context = is_a?(Module) ? name : self.class.name output pretty("error", :red), "[#{context}]", args end
Outputs a boxed error message and then exits. @param args [Array<String>] the message(s) @return [void]
# File lib/muzak/utils.rb, line 82 def error!(*args) error(*args) exit 1 end
Outputs a boxed message and arguments. @param box [String] the string to box @param args [Array<String>] the trailing strings to print @return [void]
# File lib/muzak/utils.rb, line 59 def output(box, *args) msg = args.join(" ") puts "[#{box}] #{msg}" end
Formats a string with ANSI colors. @param color [Symbol] the color to use on the string @param str [String] the string to format @return [String] the color-formatted string
# File lib/muzak/utils.rb, line 43 def pretty(str, color = :none) colors = { none: 0, red: 31, green: 32, yellow: 33, blue: 34, } "\e[#{colors[color]}m#{str}\e[0m" end
Outputs a boxed verbose message. @param args [Array<String>] the message(s) @return [void]
# File lib/muzak/utils.rb, line 99 def verbose(*args) return unless verbose? output pretty("verbose", :blue), args end
@return [Boolean] whether or not muzak is running in verbose mode
# File lib/muzak/utils.rb, line 35 def verbose? Config.verbose end