class EmSpamc::Connection

Constants

DEFAULT_OPTIONS
PROTO_VERSION

Constants ============================================================

Attributes

error[R]

Properties ===========================================================

Public Class Methods

check(message, options = nil) click to toggle source

Class Methods ========================================================

# File lib/em_spamc/connection.rb, line 20
def self.check(message, options = nil)
  request('CHECK', message, options)
end
new(options) click to toggle source

Instance Methods =====================================================

# File lib/em_spamc/connection.rb, line 59
def initialize(options)
  @fiber = options[0]
  @command = options[1]
  @message = options[2] && (options[2].gsub(/\r?\n/, "\r\n") + "\r\n")
  @error = nil
  @data = nil
end
ping(options = nil) click to toggle source
# File lib/em_spamc/connection.rb, line 32
def self.ping(options = nil)
  request('PING', nil, options)
end
report(message, options = nil) click to toggle source
# File lib/em_spamc/connection.rb, line 28
def self.report(message, options = nil)
  request('REPORT', message, options)
end
request(command, message, options = nil) click to toggle source
# File lib/em_spamc/connection.rb, line 36
def self.request(command, message, options = nil)
  if (options)
    options = DEFAULT_OPTIONS.merge(options)
  else
    options = DEFAULT_OPTIONS
  end

  EventMachine.connect(
    options[:host],
    options[:port],
    self,
    [
      Fiber.current,
      command,
      message
    ]
  )

  Fiber.yield
end
symbols(message, options = nil) click to toggle source
# File lib/em_spamc/connection.rb, line 24
def self.symbols(message, options = nil)
  request('SYMBOLS', message, options)
end

Public Instance Methods

error?() click to toggle source
# File lib/em_spamc/connection.rb, line 104
def error?
  !!@error
end
post_init() click to toggle source

EventMachine hook that’s engaged after the client is initialized.

# File lib/em_spamc/connection.rb, line 68
def post_init
  if (error?)
    @fiber.resume
  else
    send_data("#{@command} #{PROTO_VERSION}\r\n")

    if (@message)
      send_data("Content-length: #{@message.bytesize}\r\n")
      send_data("\r\n")
      send_data(@message)
    else
      send_data("\r\n")
    end
  end
end
receive_data(data) click to toggle source
# File lib/em_spamc/connection.rb, line 84
def receive_data(data)
  @data ||= ''
  @data << data
end
unbind() click to toggle source
# File lib/em_spamc/connection.rb, line 89
def unbind
  result = EmSpamc::Result.new

  if (@data)
    result.headers = EmSpamc::HeaderParser.parse(@data)

    case (@command)
    when 'REPORT'
      result.report = EmSpamc::ReportParser.parse(@data)
    end
  end

  @fiber.resume(result)
end