Sha256: 8518b17d5ecf6292cc4bf134ccb3db9aad33d099b36b5b7ebb5a5e2d53f2f209

Contents?: true

Size: 1.87 KB

Versions: 6

Compression:

Stored size: 1.87 KB

Contents

module AdaptivePayments
  # AbstractRequest is a JsonModel that defines some default behaviour for all requests
  #
  # A request defines the operation it executes and provides a method for building the corresponding response.
  # All requests provide a RequestEnvelope by default.  There is no need to re-define it in descendants.
  class AbstractRequest < JsonModel
    # The RequestEnvelope, providing the detail_level and error_language.
    #
    # Aliases are defined for #detail_level and #error_language.  They do not need to be accessed through the
    # request_envelope.
    attribute :request_envelope, Node[RequestEnvelope], :param => "requestEnvelope"

    alias_params :request_envelope, {
      :detail_level   => :detail_level,
      :error_language => :error_language
    }

    class << self
      # Set or get the API operation for the request class
      #
      # @param [Symbol] name
      #   the name of the operation defined in the Adaptive Payments API, optional
      #
      # @return [Symbol]
      #   the name of the operation defined in the Adaptive Payments API
      def operation(name = nil)
        @operation = name unless name.nil?
        @operation
      end

      # Get the request class for the given operation.
      #
      # @param [Symbol] operation
      #   the name of the operation
      #
      # @return [AbstractRequest]
      #   the request subclass
      def for_operation(name)
        AdaptivePayments.const_get(name.to_s + "Request")
      end

      # Given a JSON string, return the corresponding response.
      #
      # @param [String] json
      #   the raw JSON string as returned by the API
      #
      # @return [AbstractResponse]
      #   the corresponding response for the JSON
      def build_response(json)
        klass = AdaptivePayments.const_get(operation.to_s + "Response")
        klass.from_json(json.to_s)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
creative-pp-adaptive-1.1.1 lib/pp-adaptive/abstract_request.rb
pp-adaptive-1.0.0 lib/pp-adaptive/abstract_request.rb
pp-adaptive-0.0.6 lib/pp-adaptive/abstract_request.rb
pp-adaptive-0.0.5 lib/pp-adaptive/abstract_request.rb
pp-adaptive-0.0.4 lib/pp-adaptive/abstract_request.rb
pp-adaptive-0.0.3 lib/pp-adaptive/abstract_request.rb