#!/usr/bin/env ruby # -*- encoding: utf-8 -*- # Copyright muflax , 2016 # License: GNU GPLv3 (or later) require "json" require "socket" require "tempfile" module MPV class Socket attr_reader :socket def initialize path @path = path @socket = UNIXSocket.new(path) end def get_response JSON.parse(socket.readline) end 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 def wait_for event loop do response = get_response case response["event"] when event, "idle" break end end end def close @socket.close File.delete(@path) if File.exists? @path end end end