Sha256: 52641d8263c149bc29a7fedf2583216dcf56cf63b3e8a8b7ac83445a345482d7

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright muflax <mail@muflax.com>, 2016
# License: GNU GPLv3 (or later) <http://www.gnu.org/copyleft/gpl.html>

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mpv-slave-0.2 lib/mpv-slave/socket.rb
mpv-slave-0.1 lib/mpv-slave/socket.rb