class Tastevin::Agent

Attributes

host[RW]
port[RW]

Public Class Methods

get(config, key) click to toggle source
# File lib/tastevin/agent.rb, line 16
def self.get(config, key)
  contact(config) { |agent| agent.get(key) }
end
new(host, port, session) click to toggle source
# File lib/tastevin/agent.rb, line 40
def initialize(host, port, session)
  @host = host
  @port = port

  @session = session
end
set(config, key, value) click to toggle source
# File lib/tastevin/agent.rb, line 20
def self.set(config, key, value)
  contact(config) { |agent| agent.set(key, value) }
end
status(config) click to toggle source
# File lib/tastevin/agent.rb, line 6
def self.status(config)
  begin
    contact(config).release

    :online
  rescue Error
    :offline
  end
end

Private Class Methods

connect(host, port) click to toggle source
# File lib/tastevin/agent.rb, line 71
def self.connect(host, port)
  begin
    Wine::Session.connect(host, port)
  rescue Wine::ConnectionRefused
    raise ConnectionError
  end
end
contact(config) { |agent| ... } click to toggle source
# File lib/tastevin/agent.rb, line 47
def self.contact(config)
  host     = config['host']
  port     = config['port']
  username = config['username']
  password = config['password']

  session = connect(host, port)
  login(session, username, password)

  agent = new(host, port, session)

  if block_given?
    begin
      yield agent
    ensure
      agent.release
    end
  else
    agent
  end
end
login(session, username, password) click to toggle source
# File lib/tastevin/agent.rb, line 81
def self.login(session, username, password)
  begin
    unless session.login(username, password)
      session.close
      raise LoginError
    end
  rescue Wine::ProtocolError
    raise Error, "Protocol error"
  rescue Wine::ResponseTimeout
    raise Error, "Response timeout"
  end
end

Public Instance Methods

get(key) click to toggle source
# File lib/tastevin/agent.rb, line 26
def get(key)
  @session.get(key)
end
release() click to toggle source
# File lib/tastevin/agent.rb, line 34
def release
  @session.close
end
set(key, value) click to toggle source
# File lib/tastevin/agent.rb, line 30
def set(key, value)
  @session.set(key, value)
end