Class: EmmyHttp::Operation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, adapter) ⇒ Operation

Returns a new instance of Operation.



12
13
14
15
16
17
18
# File 'lib/emmy_http/operation.rb', line 12

def initialize(request, adapter)
  @request = request
  @adapter = adapter
  @adapter.delegate = self
  @response = Response.new
  setup
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



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

def adapter
  @adapter
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/emmy_http/operation.rb', line 6

def response
  @response
end

Instance Method Details

#connectObject



20
21
22
# File 'lib/emmy_http/operation.rb', line 20

def connect
  @connection ||= EmmyMachine.connect(*self)
end

#serializable_hashObject



43
44
45
46
47
48
# File 'lib/emmy_http/operation.rb', line 43

def serializable_hash
  {
    request: request && request.serializable_hash,
    response: response && response.serializable_hash
  }
end

#syncObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/emmy_http/operation.rb', line 24

def sync
  Fiber.sync do |fiber|
    # create connection
    connect

    on :success do |operation, conn|
      fiber.resume operation.response
    end

    on :error do |error, operation, conn|
      fiber.leave ConnectionError, error.to_s
    end
  end
end

#to_aObject



39
40
41
# File 'lib/emmy_http/operation.rb', line 39

def to_a
  adapter.to_a
end