Class: Tsquery

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

Direct Known Subclasses

LazyTsquery, RetryingTsquery

Defined Under Namespace

Classes: Error, UnknownCommand

Instance Method Summary collapse

Constructor Details

#initialize(logger: Logger.new(STDOUT)) ⇒ Tsquery

Returns a new instance of Tsquery.



6
7
8
# File 'lib/tsquery.rb', line 6

def initialize(logger: Logger.new(STDOUT))
  @logger = logger
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(command, *args) ⇒ Object



59
60
61
# File 'lib/tsquery.rb', line 59

def method_missing(command, *args)
  execute(command.to_s, *args)
end

Instance Method Details

#closeObject



75
76
77
# File 'lib/tsquery.rb', line 75

def close
  @telnet.close
end

#connect(server: '127.0.0.1', port: '10011', telnet_class: Net::Telnet) ⇒ Object



69
70
71
72
# File 'lib/tsquery.rb', line 69

def connect(server: '127.0.0.1', port: '10011', telnet_class: Net::Telnet)
  @telnet = telnet_class.new('Host' => server, 'Port' => port, 'Waittime' => 0.1)
  @telnet.waitfor 'Match' => /^TS3\n/
end

#execute(command, *args) ⇒ Object



11
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/tsquery.rb', line 11

def execute(command, *args)
  args = args.each_with_object([]) do |arg, array|
    case arg
    when Integer
      array << arg.to_s
    when String
      array << arg
    when Hash
      arg.each do |key, value|
        array << "#{key}=#{value.to_s.gsub(ESCAPE_PATTERNS_REGEXP, ESCAPE_PATTERNS)}"
      end
    end
  end

  full_command = ([command] + args).join(' ')
  @logger.info "=> #{full_command}"

  case command
  when /list$/
    parse_list(@telnet.cmd(
      'String'  => full_command,
      'Timeout' => 3,
      'Match'   => /error id=\d+/
    ))
  when /info$/, 'whoami', 'version', 'clientgetdbidfromuid'
    parse_info(@telnet.cmd(
      'String'  => full_command,
      'Timeout' => 3,
      'Match'   => /error id=\d+/
    ))
  else
    parse(@telnet.cmd(
      'String'  => full_command,
      'Timeout' => 3,
      'Match'   => /^error id=\d+/
    ))
  end
end

#login(username: 'serveradmin', password:) ⇒ Object



51
52
53
54
55
56
# File 'lib/tsquery.rb', line 51

def (username: 'serveradmin', password:)
  execute 'login', username, password
  true
rescue Error
  false
end

#respond_to_missing?(command) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/tsquery.rb', line 64

def respond_to_missing?(command, *)
  !!(command =~ /[[:alnum:]]$/)
end