Sha256: 5d9ade752a6aba93fed0eff92bd6199bb6cf8c7ceb423ca3e0ebd6f4ef343faa

Contents?: true

Size: 1.97 KB

Versions: 13

Compression:

Stored size: 1.97 KB

Contents

module Adyen
  module API
    # The base class of all responses returned by API calls to Adyen.
    class Response
      # Defines shortcut accessor methods, to {Response#params}, for the given parameters.
      def self.response_attrs(*attrs)
        attrs.each do |attr|
          define_method(attr) { params[attr] }
        end
      end

      # @return [Net::HTTPResponse] The response object returned by Net::HTTP.
      attr_reader :http_response

      # @param [Net::HTTPResponse] http_response The response object returned by Net::HTTP.
      def initialize(http_response)
        @http_response = http_response
      end

      # @return [String] The raw body of the response object.
      def body
        @http_response.body
      end

      # @return [Boolean] Whether or not the request was successful.
      def success?
        !http_failure?
      end

      # @return [Boolean] Whether or not the HTTP request was a success.
      def http_failure?
        !@http_response.is_a?(Net::HTTPSuccess)
      end

      # @return [Boolean] Whether or not the SOAP request itself was a success.
      # Adyen returns a 500 status code for e.g. failed CC validation and in this case, we don't
      # want to throw a server error but rather treat it as something normal.
      def server_error?
        @http_response.is_a?(Net::HTTPServerError) && fault_message.nil?
      end

      # @return [XMLQuerier] The response body wrapped in a XMLQuerier.
      def xml_querier
        @xml_querier ||= XMLQuerier.xml(@http_response.body)
      end

      # @return [Hash] Subclasses return the parsed response body.
      def params
        raise "The Adyen::API::Response#params method should be overridden in a subclass."
      end

      # @return [String,nil] The SOAP failure message, if there is one.
      def fault_message
        @fault_message ||= begin
          message = xml_querier.text('//soap:Fault/faultstring')
          message unless message.empty?
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
adyen-2.4.0 lib/adyen/api/response.rb
adyen_jpiqueras-2.5.0 lib/adyen/api/response.rb
adyen-2.3.0 lib/adyen/api/response.rb
adyen_jpiqueras-2.4.0 lib/adyen/api/response.rb
adyen_jpiqueras-2.3.0 lib/adyen/api/response.rb
adyen-2.2.0 lib/adyen/api/response.rb
adyen-2.1.0 lib/adyen/api/response.rb
adyen-2.0.0 lib/adyen/api/response.rb
adyen-2.0.0.pre2 lib/adyen/api/response.rb
adyen-2.0.0.pre1 lib/adyen/api/response.rb
adyen-1.6.0 lib/adyen/api/response.rb
adyen-1.5.0 lib/adyen/api/response.rb
adyen-1.4.1 lib/adyen/api/response.rb