Class: EmSpamc::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/em_spamc/connection.rb

Constant Summary collapse

PROTO_VERSION =

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

'SPAMC/1.2'.freeze
DEFAULT_OPTIONS =
{
  :host => 'localhost',
  :port => 783
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

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



59
60
61
62
63
64
65
# 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

Instance Attribute Details

#errorObject (readonly)

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



16
17
18
# File 'lib/em_spamc/connection.rb', line 16

def error
  @error
end

Class Method Details

.check(message, options = nil) ⇒ Object

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



20
21
22
# File 'lib/em_spamc/connection.rb', line 20

def self.check(message, options = nil)
  request('CHECK', message, options)
end

.ping(options = nil) ⇒ Object



32
33
34
# File 'lib/em_spamc/connection.rb', line 32

def self.ping(options = nil)
  request('PING', nil, options)
end

.report(message, options = nil) ⇒ Object



28
29
30
# File 'lib/em_spamc/connection.rb', line 28

def self.report(message, options = nil)
  request('REPORT', message, options)
end

.request(command, message, options = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



24
25
26
# File 'lib/em_spamc/connection.rb', line 24

def self.symbols(message, options = nil)
  request('SYMBOLS', message, options)
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/em_spamc/connection.rb', line 104

def error?
  !!@error
end

#post_initObject

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



84
85
86
87
# File 'lib/em_spamc/connection.rb', line 84

def receive_data(data)
  @data ||= ''
  @data << data
end

#unbindObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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