Class: Spty::Command::VolumeCommand

Inherits:
BaseCommand show all
Defined in:
lib/spty/commands/volume_command.rb

Constant Summary collapse

ASCRIPT_PLAYER_VOLUME_STATUS =
<<-EOL
  tell application "Spotify" to return sound volume
EOL
ASCRIPT_PLAYER_VOLUME_UP =
<<-EOL
  tell application "Spotify"
    set currentvol to get sound volume
    if currentvol > 90 then
      set sound volume to 100
    else
      set sound volume to currentvol + 10
    end if
  end tell
EOL
ASCRIPT_PLAYER_VOLUME_DOWN =
<<-EOL
  tell application "Spotify"
    set currentvol to get sound volume
    if currentvol < 10 then
      set sound volume to 0
    else
      set sound volume to currentvol - 10
    end if
  end tell
EOL
ASCRIPT_PLAYER_VOLUME_SET =
<<-EOL
  tell application "Spotify" to set sound volume to %<level>s
EOL

Constants inherited from BaseCommand

BaseCommand::ASCRIPT_PLAYER_DETECT

Class Method Summary collapse

Methods inherited from BaseCommand

running?

Class Method Details

.call(options, _) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/spty/commands/volume_command.rb', line 3

def self.call(options, _)
  return unless running?

  action = options.shift

  # call status if no sub command was called.
  action = 'status' if action.nil?

  # if sub command is number, trigger volume set with the number.
  if action =~ /\A\d+\z/
    options = action.to_i
    action = 'level'
  end

  begin
    send(action.to_sym, options)
  rescue NameError => _e
    puts "unknown volume command #{action}"
  end
end

.down(_options) ⇒ Object



57
58
59
60
# File 'lib/spty/commands/volume_command.rb', line 57

def self.down(_options)
  Spty::AppleScriptRunner.call(ASCRIPT_PLAYER_VOLUME_DOWN)
  status
end

.level(options) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/spty/commands/volume_command.rb', line 65

def self.level(options)
  if options < 0 || options > 100
    puts 'volume level should be between 0 to 100'
    return
  end

  script = format(ASCRIPT_PLAYER_VOLUME_SET, level: options)
  Spty::AppleScriptRunner.call(script)
  status
end

.status(_options = nil) ⇒ Object



27
28
29
30
# File 'lib/spty/commands/volume_command.rb', line 27

def self.status(_options = nil)
  current_vol = Spty::AppleScriptRunner.call(ASCRIPT_PLAYER_VOLUME_STATUS)
  puts "volume is at #{current_vol}"
end

.up(_options) ⇒ Object



42
43
44
45
# File 'lib/spty/commands/volume_command.rb', line 42

def self.up(_options)
  Spty::AppleScriptRunner.call(ASCRIPT_PLAYER_VOLUME_UP)
  status
end