Class: Cardiac::OperationHandler
- Inherits:
-
Object
- Object
- Cardiac::OperationHandler
- Includes:
- ActiveSupport::Callbacks, ActiveSupport::Configurable, ActiveSupport::Rescuable
- Defined in:
- lib/cardiac/operation_handler.rb
Overview
A base operation handler.
Constant Summary collapse
- DEFAULT_RESPONSE_HANDLER =
Proc.new do |response| raise RequestFailedError, response unless response.successful? response end
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#options ⇒ Object
Returns the value of attribute options.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#response_handler ⇒ Object
Returns the value of attribute response_handler.
-
#result ⇒ Object
Returns the value of attribute result.
-
#url ⇒ Object
Returns the value of attribute url.
-
#verb ⇒ Object
Returns the value of attribute verb.
Instance Method Summary collapse
-
#aborted? ⇒ Boolean
Checks if the operation was aborted due to an exception being thrown.
-
#completed? ⇒ Boolean
Checks if the operation was completed.
-
#initialize(client_options, payload = nil, &response_handler) ⇒ OperationHandler
constructor
A new instance of OperationHandler.
- #transmit! ⇒ Object
-
#transmitted? ⇒ Boolean
Checks if the request was transmitted and a response was received.
Constructor Details
#initialize(client_options, payload = nil, &response_handler) ⇒ OperationHandler
Returns a new instance of OperationHandler.
45 46 47 48 49 50 51 52 |
# File 'lib/cardiac/operation_handler.rb', line 45 def initialize , payload=nil, &response_handler @verb = [:method] or raise InvalidOperationError, 'no HTTP verb was specified' @url = [:url] or raise InvalidOperationError, 'no URL was specified' @headers = [:headers] @payload = payload @options = .except(:method, :url, :headers) @response_handler = response_handler || DEFAULT_RESPONSE_HANDLER end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
43 44 45 |
# File 'lib/cardiac/operation_handler.rb', line 43 def headers @headers end |
#options ⇒ Object
Returns the value of attribute options.
43 44 45 |
# File 'lib/cardiac/operation_handler.rb', line 43 def @options end |
#payload ⇒ Object
Returns the value of attribute payload.
43 44 45 |
# File 'lib/cardiac/operation_handler.rb', line 43 def payload @payload end |
#response_handler ⇒ Object
Returns the value of attribute response_handler.
43 44 45 |
# File 'lib/cardiac/operation_handler.rb', line 43 def response_handler @response_handler end |
#result ⇒ Object
Returns the value of attribute result.
43 44 45 |
# File 'lib/cardiac/operation_handler.rb', line 43 def result @result end |
#url ⇒ Object
Returns the value of attribute url.
43 44 45 |
# File 'lib/cardiac/operation_handler.rb', line 43 def url @url end |
#verb ⇒ Object
Returns the value of attribute verb.
43 44 45 |
# File 'lib/cardiac/operation_handler.rb', line 43 def verb @verb end |
Instance Method Details
#aborted? ⇒ Boolean
Checks if the operation was aborted due to an exception being thrown. Even though this does not involve the interpretation a response body, an aborted transmission could instead be considered completed by handling the exception.
92 93 94 |
# File 'lib/cardiac/operation_handler.rb', line 92 def aborted? @aborted end |
#completed? ⇒ Boolean
Checks if the operation was completed. This means that either the operation was transmitted, or an exception was handled successfully.
98 99 100 |
# File 'lib/cardiac/operation_handler.rb', line 98 def completed? @aborted.nil? ? @transmitted : !@aborted end |
#transmit! ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cardiac/operation_handler.rb', line 54 def transmit! # Reset any old state before actually performing the transmission. self.result = @aborted = @transmitted = nil # Perform the actual request and receive the response. run_callbacks :transmission do self.result = nil begin self.result = @response_handler.call(perform_request) # A response was received, so consider it transmitted. @transmitted = true rescue Exception => exception # An exception was received, so consider it untransmitted. @transmitted = false # The exception may still be handled, to prevent the operation from aborting. abort! exception end end # If we get here, then we must have a result to return. complete! ensure # Always clear out our result instance before returning it. self.result = nil end |
#transmitted? ⇒ Boolean
Checks if the request was transmitted and a response was received.
85 86 87 |
# File 'lib/cardiac/operation_handler.rb', line 85 def transmitted? @transmitted end |