Class: RubyPsigate::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_psigate/response.rb

Overview

The Response class deals with the XML responses from Psigate’s servers

Constant Summary collapse

ACCOUNT_SUCCESS_CODES =
%w(
  RPA-0000
  RPA-0001
  RPA-0010
  RPA-0015
  RPA-0020
  RPA-0022
  RPA-0025
  RPA-0040
  RPA-0042
  RPA-0046
  RPA-0048
  RPA-0058
  PSI-0000
  RRC-0000
  RRC-0005
  RRC-0050
  RRC-0060
  RRC-0065
  RRC-0072
  RRC-0075
  RRC-0082
  RRC-0090
  RRC-0092
  RRC-0095
  RRC-0098
  RRC-0190
  CTL-0000
  CTL-0005
  CTL-0050
  CTL-0060
  CTL-0065
  CTL-0072
  CTL-0075
  CTL-0082
  CTL-0090
  CTL-0092
  CTL-0098
  CTL-0190
  CTL-0192
  RIV-0050
  RIV-0060
  RIV-0072
  RIV-0090
  RIV-0190
  RIV-0197
  RIV-0198
  EMR-0000
  EMR-0005
  EMR-0050
  EMR-0060
  EMR-0072
  EMR-0082
  EMR-0090
  EMR-0190
)

Instance Method Summary collapse

Constructor Details

#initialize(raw_response) ⇒ Response

The Response object is initialized with the response output from the response body of the HTTP request to Psigate’s servers The expected format of the raw_response is a properly formed XML document Please refer to Psigate’s technical documents (XML Interface PDF and Account Manager API PDF)

Once the raw_resposne is received, we use nokogiri to parse and format the output to a usable hash



69
70
71
# File 'lib/ruby_psigate/response.rb', line 69

def initialize(raw_response)
  @response = Crack::XML.parse(raw_response)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



86
87
88
89
90
91
# File 'lib/ruby_psigate/response.rb', line 86

def method_missing(name, *args, &block)
  @result = nil
  name = name.downcase.to_sym
  @result = find_value_in_hash(name, parsed)
  @result
end

Instance Method Details

#find_value_in_hash(input_key, hash) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ruby_psigate/response.rb', line 93

def find_value_in_hash(input_key, hash)
  result = nil
  hash.each_pair do |key, value|
    if value.is_a? Hash
      result = find_value_in_hash(input_key, value)
    else
      key = key.downcase.to_sym
      result = value if input_key == key
    end
    
    break unless result.nil?
  end
  result
end

#hashObject



73
74
75
# File 'lib/ruby_psigate/response.rb', line 73

def hash
  @response
end

#parsedObject



77
78
79
80
# File 'lib/ruby_psigate/response.rb', line 77

def parsed
  return @parsed if @parsed
  @parsed = @response["Result"] || @response["Response"]
end

#success?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ruby_psigate/response.rb', line 82

def success?
  parsed["Approved"] == "APPROVED" || ACCOUNT_SUCCESS_CODES.include?(self.returncode)
end