Class: SpotifyCli::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/spotify_cli/api.rb

Constant Summary collapse

PLAY =
""
STOP =
""
SPOTIFY_SEARCH_API =
"https://api.spotify.com/v1/search"

Class Method Summary collapse

Class Method Details

.help(mappings) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/spotify_cli/api.rb', line 146

def help(mappings)
  Dex::UI.frame('Spotify CLI', timing: false) do
    puts "CLI interface for Spotify"
  end

  mappings.group_by { |_,v| v }.each do |k, v|
    v.reject! { |mapping| mapping.first == k.to_s }
    doc = get_doc(self.class, k.to_s).strip_heredoc

    Dex::UI.frame(k, timing: false) do
      puts puts Dex::UI.resolve_text(doc)
      next if v.empty?
      puts Dex::UI.resolve_text("{{bold:Aliases:}}")
      v.each { |mapping| puts Dex::UI.resolve_text(" - {{info:#{mapping.first}}}") }
    end
  end
end

.nextObject



18
19
20
21
# File 'lib/spotify_cli/api.rb', line 18

def next
  puts "Playing next song"
  SpotifyCli::App.next!
end

.pauseObject



102
103
104
105
# File 'lib/spotify_cli/api.rb', line 102

def pause
  SpotifyCli::App.pause!
  status
end

.play_pauseObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/spotify_cli/api.rb', line 66

def play_pause
  args = ARGV[1..-1]

  if args.empty?
    # no specifying paremeter, this is a standard play/pause
    SpotifyCli::App.play_pause!
    status
    return
  end

  arg = args.shift
  type = arg == 'song' ? 'track' : arg

  Dex::UI.frame("Searching for #{type}", timing: false) do
    play_uri = case type
    when 'album', 'artist', 'track'
      results = search_and_play(type: type, query: args.join(' '))
      results.first
    when 'uri'
      args.first
    end
    puts "Results found, playing"
    SpotifyCli::App.play_uri!(play_uri)
    sleep 0.05 # Give time for the app to switch otherwise status may be stale
  end

  status
end

.previousObject



29
30
31
32
# File 'lib/spotify_cli/api.rb', line 29

def previous
  puts "Playing previous song"
  SpotifyCli::App.prev!
end

.replayObject



51
52
53
54
# File 'lib/spotify_cli/api.rb', line 51

def replay
  puts "Restarting song"
  SpotifyCli::App.replay!
end

.set_posObject



40
41
42
43
# File 'lib/spotify_cli/api.rb', line 40

def set_pos
  puts "Setting position to #{ARGV[1]}"
  SpotifyCli::App.set_pos!(ARGV[1])
end

.statusObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/spotify_cli/api.rb', line 113

def status
  stat = SpotifyCli::App.status

  time = "#{stat[:position]} / #{stat[:duration]}"
  state_sym = case stat[:state]
  when 'playing'
    PLAY
  else
    STOP
  end
  # 3 for padding around time, and symbol, and space for the symbol, 2 for frame
  width = Dex::UI::Terminal.width - time.size - 5

  Dex::UI.frame(stat[:track], timing: false) do
    puts Dex::UI.resolve_text([
      "{{bold:Artist:}} #{stat[:artist]}",
      "{{bold:Album:}} #{stat[:album]}",
    ].join("\n"))
    puts [
      Dex::UI::Progress.progress(stat[:percent_done], width),
      state_sym,
      time
    ].join(' ')
  end
end