Class: MPV::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/mpv-slave/socket.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Socket

Returns a new instance of Socket.



14
15
16
17
# File 'lib/mpv-slave/socket.rb', line 14

def initialize path
  @path   = path
  @socket = UNIXSocket.new(path)
end

Instance Attribute Details

#socketObject (readonly)

Returns the value of attribute socket.



12
13
14
# File 'lib/mpv-slave/socket.rb', line 12

def socket
  @socket
end

Instance Method Details

#closeObject



51
52
53
54
# File 'lib/mpv-slave/socket.rb', line 51

def close
  @socket.close
  File.delete(@path) if File.exists? @path
end

#command(*args) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mpv-slave/socket.rb', line 23

def command *args
  msg = {
    "command" => args,
  }
  socket.puts(JSON.dump(msg))

  loop do
    response = get_response
    next if not response["event"].nil?

    if response["error"] != "success"
      warn "error: #{response} for #{msg}"
    end

    return response["data"]
  end
end

#get_responseObject



19
20
21
# File 'lib/mpv-slave/socket.rb', line 19

def get_response
  JSON.parse(socket.readline)
end

#wait_for(event) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/mpv-slave/socket.rb', line 41

def wait_for event
  loop do
    response = get_response
    case response["event"]
    when event, "idle"
      break
    end
  end
end