class Backburner::Connection

Constants

PROMPT_REGEXP
STATUS_REGEXP
XML_COMMENT_REGEXP

Attributes

last_body[R]
last_message[R]
last_response[R]
last_size[R]
last_status[R]

Public Class Methods

new(host='127.0.0.1', port=3234) click to toggle source
# File lib/backburner/connection.rb, line 14
def initialize host='127.0.0.1', port=3234
  @session = Net::Telnet.new('Host' => host, 'Port' => port, 'Prompt' => PROMPT_REGEXP)
  @session.waitfor 'Match' => PROMPT_REGEXP
end

Public Instance Methods

close() click to toggle source
# File lib/backburner/connection.rb, line 23
def close
  @session.close
end
cmd(data) click to toggle source
# File lib/backburner/connection.rb, line 55
def cmd data
  @last_response = @session.cmd data
  parse_response
end
disable_prompt() click to toggle source
# File lib/backburner/connection.rb, line 27
def disable_prompt
  response = exec 'String' => 'set prompt off'
  return response if @last_status==201
  raise
end
empty?() click to toggle source
# File lib/backburner/connection.rb, line 92
def empty?
  @last_size == 0
end
error?() click to toggle source
# File lib/backburner/connection.rb, line 88
def error?
  (@last_status||0) > 399
end
exec(arg_hash) click to toggle source
# File lib/backburner/connection.rb, line 33
def exec arg_hash
  xml_string = ''
  first = true
  @session.cmd arg_hash do |response|
    if first
      response =~ /(\d+) (\d*)(.*)/
      @last_status = $1.to_i
      @last_size = $2.to_i
      @last_message = $3
      first = false
      next
    end
    xml_string += response
  end

  xml_string = xml_string.sub(/backburner>$/, '')
  DataObject.new xml_string
end
method_missing(method_name) click to toggle source
# File lib/backburner/connection.rb, line 19
def method_missing method_name
  RemoteCommand.new method_name, self
end
proceed?() click to toggle source
# File lib/backburner/connection.rb, line 80
def proceed?
  @last_status < 200 || @last_status > 299
end
ready?() click to toggle source
# File lib/backburner/connection.rb, line 76
def ready?
  (@last_status||0) == 251
end
send_data() click to toggle source
# File lib/backburner/connection.rb, line 52
def send_data
end
send_text(command_data, data=nil) click to toggle source
# File lib/backburner/connection.rb, line 60
def send_text command_data, data=nil
  request = "#{command_data}"
  request_hash = {}
  if data
    request_hash['Match'] = STATUS_REGEXP
    request += "  #{data.bytesize+14}"
  end
  request_hash['String'] = request
  @last_response = @session.cmd request_hash
  parse_status
  @last_response = @session.puts data
  @last_response = @session.waitfor('Match' => STATUS_REGEXP)
  @session.waitfor 'Match' => PROMPT_REGEXP
  parse_status
end

Private Instance Methods

parse_body() click to toggle source
# File lib/backburner/connection.rb, line 113
def parse_body
  @last_body = @last_response.gsub PROMPT_REGEXP, ''
  @last_body.gsub! STATUS_REGEXP, ''
  @last_body.gsub! XML_COMMENT_REGEXP, ''
end
parse_object() click to toggle source
# File lib/backburner/connection.rb, line 119
def parse_object
  parser = Nori.new :convert_tags_to => lambda { |tag| tag.snakecase.to_sym }
  DataObject.new parser.parse @last_body
end
parse_response() click to toggle source
# File lib/backburner/connection.rb, line 98
def parse_response
  parse_status
  parse_body
  parse_object
end
parse_status() click to toggle source
# File lib/backburner/connection.rb, line 104
def parse_status
  #TODO: add exception from wrong status codes
  @last_response.scan STATUS_REGEXP
  @last_status = $1.to_i
  @last_size = $2.to_i
  @last_message = $2
  raise(ConnectionError.new, @last_message) if error?
end