Module: Muzak::Utils
- Included in:
- Index, Instance, Player::StubPlayer, Plugin::StubPlugin, Song
- Defined in:
- lib/muzak/utils.rb
Overview
A collection of convenience utilities for use throughout muzak.
Class Method Summary collapse
-
.resolve_command(cmd) ⇒ String
Convert the given command into a method (kebab to camel case).
-
.resolve_method(meth) ⇒ String
Convert the given method into a command (camel to kebab case).
Instance Method Summary collapse
-
#album_art?(filename) ⇒ Boolean
Tests whether the given filename is likely to be album art.
-
#build_response(error: nil, data: nil) ⇒ Object
Returns a response hash containing the given data and error.
-
#debug(*args) ⇒ void
Outputs a boxed debugging message.
-
#debug? ⇒ Boolean
Whether or not muzak is running in debug mode.
-
#error(*args) ⇒ void
Outputs a boxed error message.
-
#error!(*args) ⇒ void
Outputs a boxed error message and then exits.
-
#info(*args) ⇒ void
Outputs a boxed informational message.
-
#music?(filename) ⇒ Boolean
Tests whether the given filename is likely to be music.
-
#output(box, *args) ⇒ void
Outputs a boxed message and arguments.
-
#pretty(color = :none, str) ⇒ String
Formats a string with ANSI colors.
-
#verbose(*args) ⇒ void
Outputs a boxed verbose message.
-
#verbose? ⇒ Boolean
Whether or not muzak is running in verbose mode.
-
#warn(*args) ⇒ void
Outputs a boxed warning message.
Class Method Details
.resolve_command(cmd) ⇒ String
Convert the given command into a method (kebab to camel case).
9 10 11 |
# File 'lib/muzak/utils.rb', line 9 def self.resolve_command(cmd) cmd.tr "-", "_" end |
.resolve_method(meth) ⇒ String
Convert the given method into a command (camel to kebab case).
18 19 20 |
# File 'lib/muzak/utils.rb', line 18 def self.resolve_method(meth) meth.to_s.tr "_", "-" end |
Instance Method Details
#album_art?(filename) ⇒ Boolean
Tests whether the given filename is likely to be album art.
32 33 34 |
# File 'lib/muzak/utils.rb', line 32 def album_art?(filename) File.basename(filename) =~ /(cover)|(folder).(jpg)|(png)/i end |
#build_response(error: nil, data: nil) ⇒ Object
Returns a response hash containing the given data and error.
121 122 123 124 125 126 127 128 |
# File 'lib/muzak/utils.rb', line 121 def build_response(error: nil, data: nil) { response: { error: error, data: data, method: caller_locations.first.label } } end |
#debug(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed debugging message.
103 104 105 106 107 |
# File 'lib/muzak/utils.rb', line 103 def debug(*args) return unless debug? output pretty(:yellow, "debug"), "[#{self.class.name}]", args end |
#debug? ⇒ Boolean
Returns whether or not muzak is running in debug mode.
37 38 39 |
# File 'lib/muzak/utils.rb', line 37 def debug? Config.debug end |
#error(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed error message.
88 89 90 |
# File 'lib/muzak/utils.rb', line 88 def error(*args) output pretty(:red, "error"), "[#{self.class.name}]", args end |
#error!(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed error message and then exits.
95 96 97 98 |
# File 'lib/muzak/utils.rb', line 95 def error!(*args) error *args exit 1 end |
#info(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed informational message.
74 75 76 |
# File 'lib/muzak/utils.rb', line 74 def info(*args) output pretty(:green, "info"), args end |
#music?(filename) ⇒ Boolean
Tests whether the given filename is likely to be music.
25 26 27 |
# File 'lib/muzak/utils.rb', line 25 def music?(filename) [".mp3", ".flac", ".m4a", ".wav", ".ogg", ".oga", ".opus"].include?(File.extname(filename.downcase)) end |
#output(box, *args) ⇒ void
This method returns an undefined value.
Outputs a boxed message and arguments.
66 67 68 69 |
# File 'lib/muzak/utils.rb', line 66 def output(box, *args) msg = args.join(" ") puts "[#{box}] #{msg}" end |
#pretty(color = :none, str) ⇒ String
Formats a string with ANSI colors.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/muzak/utils.rb', line 50 def pretty(color = :none, str) colors = { none: 0, red: 31, green: 32, yellow: 33, blue: 34 } "\e[#{colors[color]}m#{str}\e[0m" end |
#verbose(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed verbose message.
112 113 114 115 116 |
# File 'lib/muzak/utils.rb', line 112 def verbose(*args) return unless verbose? output pretty(:blue, "verbose"), args end |
#verbose? ⇒ Boolean
Returns whether or not muzak is running in verbose mode.
42 43 44 |
# File 'lib/muzak/utils.rb', line 42 def verbose? Config.verbose end |
#warn(*args) ⇒ void
This method returns an undefined value.
Outputs a boxed warning message.
81 82 83 |
# File 'lib/muzak/utils.rb', line 81 def warn(*args) output pretty(:yellow, "warn"), args end |