Class: EmmyExtends::Remailer::Operation
- Inherits:
-
Object
- Object
- EmmyExtends::Remailer::Operation
- Defined in:
- lib/emmy_extends/remailer/operation.rb
Instance Attribute Summary collapse
-
#completed ⇒ Object
Returns the value of attribute completed.
-
#connection ⇒ Object
Returns the value of attribute connection.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(req) ⇒ Operation
constructor
A new instance of Operation.
- #initialize_connection(conn) ⇒ Object
- #send_emails ⇒ Object
- #sync ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(req) ⇒ Operation
Returns a new instance of Operation.
11 12 13 14 |
# File 'lib/emmy_extends/remailer/operation.rb', line 11 def initialize(req) @request = req @completed = false end |
Instance Attribute Details
#completed ⇒ Object
Returns the value of attribute completed.
8 9 10 |
# File 'lib/emmy_extends/remailer/operation.rb', line 8 def completed @completed end |
#connection ⇒ Object
Returns the value of attribute connection.
7 8 9 |
# File 'lib/emmy_extends/remailer/operation.rb', line 7 def connection @connection end |
#request ⇒ Object
Returns the value of attribute request.
5 6 7 |
# File 'lib/emmy_extends/remailer/operation.rb', line 5 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
6 7 8 |
# File 'lib/emmy_extends/remailer/operation.rb', line 6 def response @response end |
Instance Method Details
#connect ⇒ Object
16 17 18 |
# File 'lib/emmy_extends/remailer/operation.rb', line 16 def connect @connection ||= EmmyMachine.connect(*self) end |
#initialize_connection(conn) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/emmy_extends/remailer/operation.rb', line 52 def initialize_connection(conn) conn.on :connect do |*a| end conn.on :error do |*a| error! end conn.on :disconnect do |*a| error!(Remailer::Error.new(conn.)) unless completed end end |
#send_emails ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/emmy_extends/remailer/operation.rb', line 20 def send_emails raise "no emails to send" if request.emails.empty? request.emails.each_with_index do |email, index| if index.zero? connection.send_email(email.from, email.to, email.content) do |status| @response = Remailer::Response.new(status) success!(response, self, connection) unless completed end else connection.send_email(email.from, email.to, email.content) end end end |
#sync ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/emmy_extends/remailer/operation.rb', line 35 def sync Fiber.sync do |fiber| connect send_emails on :success do |res, operation, conn| @completed = true fiber.resume response end on :error do |error, operation, conn| @completed = true fiber.leave error end end end |
#to_a ⇒ Object
65 66 67 |
# File 'lib/emmy_extends/remailer/operation.rb', line 65 def to_a ["tcp://#{request..host}:#{request..port}", EmmyExtends::Remailer::Connection, method(:initialize_connection), self] end |