class TransactPro::Request

Constants

RECURRING_METHODS
SUPPORTED_METHODS

Attributes

method[R]
options[R]

Public Class Methods

new(options) click to toggle source

Do not use this default initializer! Instead, initialize a Gateway and call request method on that.

# File lib/transact_pro/request.rb, line 16
def initialize(options)
  @options = options
  @method = @options[:method]

  unless SUPPORTED_METHODS.include?(@method)
    raise ArgumentError.new(
      "'#{@method}' is not a supported API request method"
    )
  end
end

Public Instance Methods

call() click to toggle source
# File lib/transact_pro/request.rb, line 27
def call
  # does prep
  details

  if options[:VERBOSE]
    puts(
      ">> About to make a POST request to TransactPro at #{Time.now} GMT.\n"\
      "  url: #{@url}\n"\
      "  params: #{@postable_params.merge(pwd: "..redacted..")}"
    )
  end

  @raw_response = RestClient.post(@url, @postable_params)
  @response = TransactPro::Response.new(@raw_response.to_s)
end
details() click to toggle source
# File lib/transact_pro/request.rb, line 43
def details
  return @details if defined?(@details)

  # DEPRECATED
  @defaults ||= TransactPro::RequestSpecs.const_get(
    "#{method.to_s.upcase}_DEFAULTS"
  )

  @request_options ||= @defaults.merge(options)
  @request_options[:rs] = routing_string

  @spec ||= spec_to_use

  @postable_params = {}
  @spec.each do |k, spec|
    do_validation =
      if spec[:mandatory]
        # mandatory key, always validate
        @postable_params[k] = @request_options[k]
        true
      else
        # non-mandatory key, include and validate only if it is present
        if @request_options[k].to_s.size > 0
          @postable_params[k] = @request_options[k]
          true
        else
          false
        end
      end

    validate(k, @postable_params[k], spec[:format]) if do_validation
  end

  @url = "#{@request_options[:API_URI]}?a=#{sendable_method}"

  @details = {url: @url, params: @postable_params}
end

Private Instance Methods

recurring_method?() click to toggle source
# File lib/transact_pro/request.rb, line 106
def recurring_method?
  RECURRING_METHODS.include?(method)
end
routing_string() click to toggle source
# File lib/transact_pro/request.rb, line 92
def routing_string
  @routing_string =
    if options[:rs].to_s.size > 0
      options[:rs]
    elsif recurring_method?
      options[:ACCOUNT_RECURRING]
    else
      # a regular user-facing method, preferring 3D account
      options[:ACCOUNT_3D].to_s.size > 0 ?
        options[:ACCOUNT_3D] :
        options[:ACCOUNT_NON3D]
    end
end
sendable_method() click to toggle source
# File lib/transact_pro/request.rb, line 110
def sendable_method
  method == :init_recurring_registration ? :init : method
end
spec_to_use() click to toggle source
# File lib/transact_pro/request.rb, line 114
def spec_to_use
  loosened_const = "LOOSENED_#{method.to_s.upcase}_SPEC" #=> "LOOSENED_INIT_SPEC"
  const = "#{method.to_s.upcase}_SPEC" #=> "INIT_SPEC"

  const =
    if options[:LOOSENED_VALIDATIONS]
      begin
        TransactPro::RequestSpecs.const_get(loosened_const)
      rescue
        TransactPro::RequestSpecs.const_get(const)
      end
    else
      TransactPro::RequestSpecs.const_get(const)
    end
end
validate(key, string, regex) click to toggle source
# File lib/transact_pro/request.rb, line 83
def validate(key, string, regex)
  unless string.to_s[regex]
    raise TransactPro::Request::ValidationError.new(
      ":#{key} with value '#{string}' is invalid for request, "\
      "expected a match for regex #{regex.inspect}"
    )
  end
end