Class: EmSpamc::Connection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- EmSpamc::Connection
- 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
-
#error ⇒ Object
readonly
Properties ===========================================================.
Class Method Summary collapse
-
.check(message, options = nil) ⇒ Object
Class Methods ========================================================.
- .ping(options = nil) ⇒ Object
- .report(message, options = nil) ⇒ Object
- .request(command, message, options = nil) ⇒ Object
- .symbols(message, options = nil) ⇒ Object
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize(options) ⇒ Connection
constructor
Instance Methods =====================================================.
-
#post_init ⇒ Object
EventMachine hook that’s engaged after the client is initialized.
- #receive_data(data) ⇒ Object
- #unbind ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Instance Methods =====================================================
59 60 61 62 63 64 65 |
# File 'lib/em_spamc/connection.rb', line 59 def initialize() @fiber = [0] @command = [1] @message = [2] && ([2].gsub(/\r?\n/, "\r\n") + "\r\n") @error = nil @data = nil end |
Instance Attribute Details
#error ⇒ Object (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(, = nil) request('CHECK', , ) end |
.ping(options = nil) ⇒ Object
32 33 34 |
# File 'lib/em_spamc/connection.rb', line 32 def self.ping( = nil) request('PING', nil, ) end |
.report(message, options = nil) ⇒ Object
28 29 30 |
# File 'lib/em_spamc/connection.rb', line 28 def self.report(, = nil) request('REPORT', , ) 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, , = nil) if () = DEFAULT_OPTIONS.merge() else = DEFAULT_OPTIONS end EventMachine.connect( [:host], [:port], self, [ Fiber.current, command, ] ) Fiber.yield end |
.symbols(message, options = nil) ⇒ Object
24 25 26 |
# File 'lib/em_spamc/connection.rb', line 24 def self.symbols(, = nil) request('SYMBOLS', , ) end |
Instance Method Details
#error? ⇒ Boolean
104 105 106 |
# File 'lib/em_spamc/connection.rb', line 104 def error? !!@error end |
#post_init ⇒ Object
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 |
#unbind ⇒ Object
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 |