Module: Muzak::Utils

Included in:
Index, Instance, Player, 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

Instance Method Summary collapse

Class Method Details

.album_art?(filename) ⇒ Boolean

Tests whether the given filename is likely to be album art.

Parameters:

  • filename (String)

    the filename to test

Returns:

  • (Boolean)

    whether or not the file is an art file



32
33
34
# File 'lib/muzak/utils.rb', line 32

def self.album_art?(filename)
  File.basename(filename) =~ /(cover)|(folder).(jpg)|(png)/i
end

.music?(filename) ⇒ Boolean

Tests whether the given filename is likely to be music.

Parameters:

  • filename (String)

    the filename to test

Returns:

  • (Boolean)

    whether or not the file is a music file



25
26
27
# File 'lib/muzak/utils.rb', line 25

def self.music?(filename)
  [".mp3", ".flac", ".m4a", ".wav", ".ogg", ".oga", ".opus"].include?(File.extname(filename.downcase))
end

.resolve_command(cmd) ⇒ String

Convert the given command into a method (kebab to camel case).

Examples:

resolve_command "do-something" # => "do_something"

Parameters:

  • cmd (String)

    the command to convert

Returns:

  • (String)

    the method corresponding to the command



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).

Examples:

resolve_method "do_something" # => "do-something"

Parameters:

  • meth (String, Symbol)

    the method to convert

Returns:

  • (String)

    the command corresponding to the method



18
19
20
# File 'lib/muzak/utils.rb', line 18

def self.resolve_method(meth)
  meth.to_s.tr "_", "-"
end

.which?(util) ⇒ Boolean

Tests whether the given utility is available in the system path.

Parameters:

  • util (String)

    the utility to test

Returns:

  • (Boolean)

    whether or not the utility is available



39
40
41
42
43
# File 'lib/muzak/utils.rb', line 39

def self.which?(util)
  ENV["PATH"].split(File::PATH_SEPARATOR).any? do |path|
    File.executable?(File.join(path, util))
  end
end

Instance Method Details

#build_response(error: nil, data: nil) ⇒ Object

Returns a response hash containing the given data and error.

Parameters:

  • error (String) (defaults to: nil)

    the error string, if needed

  • data (String, Hash) (defaults to: nil)

    the data, if needed



124
125
126
127
128
129
130
131
# File 'lib/muzak/utils.rb', line 124

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.

Parameters:

  • args (Array<String>)

    the message(s)



106
107
108
109
110
# File 'lib/muzak/utils.rb', line 106

def debug(*args)
  return unless debug?
  context = self.is_a?(Module) ? name : self.class.name
  output pretty(:yellow, "debug"), "[#{context}]", args
end

#debug?Boolean

Returns whether or not muzak is running in debug mode.

Returns:

  • (Boolean)

    whether or not muzak is running in debug mode



46
47
48
# File 'lib/muzak/utils.rb', line 46

def debug?
  Config.debug
end

#error(*args) ⇒ void

This method returns an undefined value.

Outputs a boxed error message.

Parameters:

  • args (Array<String>)

    the message(s)



90
91
92
93
# File 'lib/muzak/utils.rb', line 90

def error(*args)
  context = self.is_a?(Module) ? name : self.class.name
  output pretty(:red, "error"), "[#{context}]", args
end

#error!(*args) ⇒ void

This method returns an undefined value.

Outputs a boxed error message and then exits.

Parameters:

  • args (Array<String>)

    the message(s)



98
99
100
101
# File 'lib/muzak/utils.rb', line 98

def error!(*args)
  error *args
  exit 1
end

#output(box, *args) ⇒ void

This method returns an undefined value.

Outputs a boxed message and arguments.

Parameters:

  • box (String)

    the string to box

  • args (Array<String>)

    the trailing strings to print



75
76
77
78
# File 'lib/muzak/utils.rb', line 75

def output(box, *args)
  msg = args.join(" ")
  puts "[#{box}] #{msg}"
end

#pretty(color = :none, str) ⇒ String

Formats a string with ANSI colors.

Parameters:

  • color (Symbol) (defaults to: :none)

    the color to use on the string

  • str (String)

    the string to format

Returns:

  • (String)

    the color-formatted string



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/muzak/utils.rb', line 59

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.

Parameters:

  • args (Array<String>)

    the message(s)



115
116
117
118
119
# File 'lib/muzak/utils.rb', line 115

def verbose(*args)
  return unless verbose?

  output pretty(:blue, "verbose"), args
end

#verbose?Boolean

Returns whether or not muzak is running in verbose mode.

Returns:

  • (Boolean)

    whether or not muzak is running in verbose mode



51
52
53
# File 'lib/muzak/utils.rb', line 51

def verbose?
  Config.verbose
end

#warn(*args) ⇒ void

This method returns an undefined value.

Outputs a boxed warning message.

Parameters:

  • args (Array<String>)

    the message(s)



83
84
85
# File 'lib/muzak/utils.rb', line 83

def warn(*args)
  output pretty(:yellow, "warn"), args
end