Class: DoubanFM::Player

Inherits:
Object
  • Object
show all
Defined in:
lib/doubanfm/player.rb

Instance Method Summary collapse

Constructor Details

#initializePlayer

Returns a new instance of Player.



5
6
7
8
# File 'lib/doubanfm/player.rb', line 5

def initialize
  @client = Client.new
  @semaphore = Mutex.new
end

Instance Method Details

#loginObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/doubanfm/player.rb', line 59

def 
  return if @client.user_logged?

  email = ask("邮箱: ")
  password = ask("密码: ") do |q|
    q.echo = false
  end

  @client.(email, password)
end

#playObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/doubanfm/player.rb', line 39

def play
  Thread.new do
    player_proc = proc do |waiting|
      if waiting
        @client.next_song

        play_current_song do |wait|
          player_proc.call(wait)
        end
      end
    end
    player_proc.call(true)
  end
end

#skipObject



54
55
56
57
# File 'lib/doubanfm/player.rb', line 54

def skip
  @client.next_song
  kill_player_thread
end

#startObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/doubanfm/player.rb', line 10

def start
  play

  loop do
    cmd = ask("DoubanFM> ", %w{login skip rate unrate quit}) do |q|
      q.overwrite = true
      q.readline = true
    end

    case cmd
    when 'login'
      
    when 'skip'
      skip
    when 'rate'
      say('请先登录') unless @client.user_logged?
      @client.rate
    when 'unrate'
      say('请先登录') unless @client.user_logged?
      @client.unrate
    when 'quit'
      kill_player_thread
      break
    else
      say('Nothing to do')
    end
  end
end