Class: EmmyExtends::Remailer::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/emmy_extends/remailer/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#completedObject

Returns the value of attribute completed.



8
9
10
# File 'lib/emmy_extends/remailer/operation.rb', line 8

def completed
  @completed
end

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/emmy_extends/remailer/operation.rb', line 7

def connection
  @connection
end

#requestObject

Returns the value of attribute request.



5
6
7
# File 'lib/emmy_extends/remailer/operation.rb', line 5

def request
  @request
end

#responseObject

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

#connectObject



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.error_message)) unless completed
  end
end

#send_emailsObject



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

#syncObject



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_aObject



65
66
67
# File 'lib/emmy_extends/remailer/operation.rb', line 65

def to_a
  ["tcp://#{request.options.host}:#{request.options.port}", EmmyExtends::Remailer::Connection, method(:initialize_connection), self]
end