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, connection = nil) ⇒ 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, connection=nil)
  raise "invalid adapter" if adapter.nil? || !adapter.respond_to?(:to_a) || !adapter.respond_to?(:delegate=)
  @request          = request
  @connection       = connection
  @adapter          = adapter
  @adapter.delegate = self
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

#reconnectObject



24
25
26
# File 'lib/emmy_http/operation.rb', line 24

def reconnect
  EmmyMachine.reconnect(*self)
end

#serializable_hashObject



63
64
65
66
67
68
# File 'lib/emmy_http/operation.rb', line 63

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

#syncObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/emmy_http/operation.rb', line 28

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

    on :init do |connection|
      # update connection
      @connection = connection
    end

    on :head do |response, operation, conn|
      # set response
      @response = response
    end

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

    on :error do |error, operation, conn|
      # return error as exception
      if request.raise_error?
        fiber.leave ConnectionError, error.to_s
      else
        fiber.resume nil
      end
    end
  end
end

#to_aObject



59
60
61
# File 'lib/emmy_http/operation.rb', line 59

def to_a
  adapter.to_a
end