Class: PayTrace::API::Response
- Inherits:
-
Object
- Object
- PayTrace::API::Response
- Defined in:
- lib/paytrace/api/response.rb
Overview
An object representing an API response from sending a PayTrace::API::Request with a PayTrace::API::Gateway object
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
:nodoc:.
-
#values ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#generate_error_key(key, value) ⇒ Object
Internal use only.
-
#get_error_response ⇒ Object
Returns any error code(s) received.
-
#get_response ⇒ Object
Returns any status code(s) or error code(s) received.
-
#has_errors? ⇒ Boolean
Returns true if the response contained any error codes.
-
#initialize(response_string) ⇒ Response
constructor
Called by the PayTrace::API::Gateway object to initialize a response.
-
#parse_errors(response_string) ⇒ Object
Called by the framework in the event of an error response.
-
#parse_records(field_name) ⇒ Object
given a field name, splits the data in that value into an array of record hashes.
-
#parse_response(response_string) ⇒ Object
Called by the initialize method.
Constructor Details
#initialize(response_string) ⇒ Response
Called by the PayTrace::API::Gateway object to initialize a response
10 11 12 13 14 15 16 17 |
# File 'lib/paytrace/api/response.rb', line 10 def initialize(response_string) @field_delim = "|" @value_delim = "~" @multi_value_delim = "+" @values = {} @errors = {} parse_response(response_string) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/paytrace/api/response.rb', line 6 def errors @errors end |
#values ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/paytrace/api/response.rb', line 6 def values @values end |
Instance Method Details
#generate_error_key(key, value) ⇒ Object
Internal use only
65 66 67 68 |
# File 'lib/paytrace/api/response.rb', line 65 def generate_error_key(key,value) #get the error number from the value return key +'-'+ value[/([1-9]*)/,1] end |
#get_error_response ⇒ Object
Returns any error code(s) received
79 80 81 82 83 84 85 |
# File 'lib/paytrace/api/response.rb', line 79 def get_error_response() = "" @errors.each do |k,v| << v + "," end end |
#get_response ⇒ Object
Returns any status code(s) or error code(s) received
71 72 73 74 75 76 |
# File 'lib/paytrace/api/response.rb', line 71 def get_response() if has_errors? return get_error_response() end @values["RESPONSE"] end |
#has_errors? ⇒ Boolean
Returns true if the response contained any error codes
20 21 22 |
# File 'lib/paytrace/api/response.rb', line 20 def has_errors? @errors.length > 0 end |
#parse_errors(response_string) ⇒ Object
Called by the framework in the event of an error response
55 56 57 58 59 60 61 62 |
# File 'lib/paytrace/api/response.rb', line 55 def parse_errors(response_string) pairs = response_string.split(@field_delim) pairs.each do |p| k,v = p.split(@value_delim) k = generate_error_key(k,v) @errors[k] = v end end |
#parse_records(field_name) ⇒ Object
given a field name, splits the data in that value into an array of record hashes
25 26 27 28 29 30 31 32 33 |
# File 'lib/paytrace/api/response.rb', line 25 def parse_records(field_name) records = [] [@values[field_name]].flatten.each do |raw_record| records << Hash[raw_record.split(@multi_value_delim).map {|pair| pair.split('=')}] end records end |
#parse_response(response_string) ⇒ Object
Called by the initialize method
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/paytrace/api/response.rb', line 36 def parse_response(response_string) if (response_string.include? "ERROR") return parse_errors(response_string) end pairs = response_string.split(@field_delim) pairs.each do |p| k,v = p.split(@value_delim) if @values.has_key?(k) @values[k] = [@values[k]] unless @values[k].is_a?(Array) @values[k] << v else @values[k] = v end end end |