class AMXClient

Attributes

remember_token[R]

PROPERTIES

Public Class Methods

new(app_id, hostname, username, password = '', keepalive = false, secure = true) click to toggle source
# File lib/clientlib.rb, line 14
def initialize (app_id, hostname, username, password = '', keepalive = false, secure = true)
        @app_id = app_id
        @hostname = hostname
        @secure = secure
        if !password.empty? then
                @remember_token = auth username, password
        else
                @remember_token = username
        end
        @keepalive = keepalive
        exit_real unless @keepalive or password.empty?
end

Public Instance Methods

exit() click to toggle source
# File lib/clientlib.rb, line 48
def exit ()
        return unless @keepalive
        exit_real
end
fetch(message_id) click to toggle source
# File lib/clientlib.rb, line 41
def fetch (message_id)
        auth @remember_token unless @keepalive
        value = JSON.parse(send_message "FETCH #{message_id}")
        exit_real unless @keepalive
        return value
end
list(limit = '*', namespace = '/') click to toggle source
# File lib/clientlib.rb, line 27
def list (limit = '*', namespace = '/')
        auth @remember_token unless @keepalive
        value =  JSON.parse(send_message "LIST #{limit} #{namespace}")
        exit_real unless @keepalive
        return value
end
send(message) click to toggle source
# File lib/clientlib.rb, line 34
def send (message)
        auth @remember_token unless @keepalive
        value = send_message 'SEND', message.to_json
        exit_real unless @keepalive
        return value
end
sock() click to toggle source
# File lib/clientlib.rb, line 6
def sock
        @sock ||= new_sock
end
sock=(value) click to toggle source
# File lib/clientlib.rb, line 10
def sock= (value)
        @sock = value
end

Private Instance Methods

auth(username, password = '') click to toggle source
# File lib/clientlib.rb, line 58
def auth (username, password = '')
        send_message "APP #{@app_id}"
        token = send_message "AUTH #{username} #{password}"
        return token.chop
end
exit_real() click to toggle source
# File lib/clientlib.rb, line 64
def exit_real
        send_message 'EXIT'
        sock.close
        self.sock = nil
end
new_sock() click to toggle source
# File lib/clientlib.rb, line 83
def new_sock
        s = TCPSocket.new(@hostname, 2026)
        if @secure then
                ctx = OpenSSL::SSL::SSLContext.new
                ctx.ssl_version = :SSLv23_client
                ssl = OpenSSL::SSL::SSLSocket.new(s, ctx)
                ssl.sync_close = true
                ssl.connect
                return ssl
        else
                return s
        end
end
send_message(message, body = nil) click to toggle source
# File lib/clientlib.rb, line 70
def send_message (message, body = nil)
        sock.puts message
        if (body) then
                sock.puts (body)
                sock.puts '.'
        end
        response = ''
    while line = sock.gets and line.chop != '.' do   # Read lines from the socket
       response += line
        end
        return response
end