Class: TS3Query::TS3Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ts3query/ts3_connection.rb

Direct Known Subclasses

ClientConnection, ServerConnection

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ TS3Connection

Returns a new instance of TS3Connection.



7
8
9
# File 'lib/ts3query/ts3_connection.rb', line 7

def initialize(params)
  connect(params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ts3query/ts3_connection.rb', line 15

def method_missing(meth, *args, &block)
  result  = []
  options = ""
  params  = ""

  if block
    query_options = QueryOptions.new
    yield query_options

    query_options.options.each do |opt|
      options += " -#{opt}"
    end
  end

  if args.first
    args.first.each do |key, value|
      params += " #{key}=#{value}"
    end
  end

  @connection.cmd("String"  => "#{meth}#{params}#{options}\r",
                  "Match"   => /error id=0 msg=ok\n/,
                  "Timeout" => 3) { |data|
    data.force_encoding 'UTF-8'
    data.split("|").each do |current|
      current_data = {}
      current.split(" ").each do |entity|
        key, value = entity.split "="
        current_data[key] = value
      end
      current_data.delete("error")
      current_data.delete("id")
      current_data.delete("msg")

      result << current_data
    end
  }
  result << {"id" => "0", "msg" => "ok"}
  result.delete({})
  result
end

Instance Method Details

#disconnectObject



11
12
13
# File 'lib/ts3query/ts3_connection.rb', line 11

def disconnect
  @connection.close
end