Class: EmmyHttp::Operation
- Inherits:
-
Object
- Object
- EmmyHttp::Operation
- Defined in:
- lib/emmy_http/operation.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #connect ⇒ Object
-
#initialize(request, adapter, connection = nil) ⇒ Operation
constructor
A new instance of Operation.
- #reconnect ⇒ Object
- #serializable_hash ⇒ Object
- #sync ⇒ Object
- #to_a ⇒ Object
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
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
7 8 9 |
# File 'lib/emmy_http/operation.rb', line 7 def adapter @adapter end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
8 9 10 |
# File 'lib/emmy_http/operation.rb', line 8 def connection @connection end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
5 6 7 |
# File 'lib/emmy_http/operation.rb', line 5 def request @request end |
#response ⇒ Object (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
#connect ⇒ Object
20 21 22 |
# File 'lib/emmy_http/operation.rb', line 20 def connect @connection ||= EmmyMachine.connect(*self) end |
#reconnect ⇒ Object
24 25 26 |
# File 'lib/emmy_http/operation.rb', line 24 def reconnect EmmyMachine.reconnect(*self) end |
#serializable_hash ⇒ Object
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 |
#sync ⇒ Object
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_a ⇒ Object
59 60 61 |
# File 'lib/emmy_http/operation.rb', line 59 def to_a adapter.to_a end |