Class: TS3Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ TS3Connection

Returns a new instance of TS3Connection.



4
5
6
# File 'lib/ts3query/ts3_connection.rb', line 4

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



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
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ts3query/ts3_connection.rb', line 12

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

  args.each do |param|
    params += " #{param.keys[0]}=#{param[param.keys[0]]}"
  end
  
  @connection.cmd("String"  => "#{meth}#{params}#{options}\r",
                  "Match"   => /error id=0 msg=ok\n/,
                  "Timeout" => 3) { |data|
    data.split("|").each do |current|
      current_data = {}
      current.split(" ").each do |entity|
        current_data[entity.split("=")[0]] = entity.split("=")[1]
      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



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

def disconnect
  @connection.close
end