Class: Cardiac::OperationHandler

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 client_options, payload=nil, &response_handler
  @verb = client_options[:method] or raise InvalidOperationError, 'no HTTP verb was specified'
  @url = client_options[:url] or raise InvalidOperationError, 'no URL was specified'
  @headers = client_options[:headers]
  @payload = payload
  @options = client_options.except(:method, :url, :headers)
  @response_handler = response_handler || DEFAULT_RESPONSE_HANDLER
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



43
44
45
# File 'lib/cardiac/operation_handler.rb', line 43

def headers
  @headers
end

#optionsObject

Returns the value of attribute options.



43
44
45
# File 'lib/cardiac/operation_handler.rb', line 43

def options
  @options
end

#payloadObject

Returns the value of attribute payload.



43
44
45
# File 'lib/cardiac/operation_handler.rb', line 43

def payload
  @payload
end

#response_handlerObject

Returns the value of attribute response_handler.



43
44
45
# File 'lib/cardiac/operation_handler.rb', line 43

def response_handler
  @response_handler
end

#resultObject

Returns the value of attribute result.



43
44
45
# File 'lib/cardiac/operation_handler.rb', line 43

def result
  @result
end

#urlObject

Returns the value of attribute url.



43
44
45
# File 'lib/cardiac/operation_handler.rb', line 43

def url
  @url
end

#verbObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


85
86
87
# File 'lib/cardiac/operation_handler.rb', line 85

def transmitted?
  @transmitted
end