Class: PayTrace::API::Response

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(response_string, multi_value_fields = []) ⇒ Response

Called by the PayTrace::API::Gateway object to initialize a response



8
9
10
11
12
13
14
15
# File 'lib/paytrace/api/response.rb', line 8

def initialize(response_string, multi_value_fields = [])
  @field_delim = "|"
  @value_delim = "~"
  @multi_value_delim = "+"
  @values = {}
  @errors = {}
  parse_response(response_string, multi_value_fields)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/paytrace/api/response.rb', line 5

def errors
  @errors
end

#valuesObject (readonly)

Returns the value of attribute values.



5
6
7
# File 'lib/paytrace/api/response.rb', line 5

def values
  @values
end

Instance Method Details

#generate_error_key(key, value) ⇒ Object

Internal use only



57
58
59
60
# File 'lib/paytrace/api/response.rb', line 57

def generate_error_key(key,value)
  #get the error number from the value
  return key +'-'+ value[/([1-9]*)/,1]
end

#get_error_responseObject

Returns any error code(s) received



71
72
73
74
75
76
77
# File 'lib/paytrace/api/response.rb', line 71

def get_error_response()
  error_message = ""
  @errors.each do  |k,v|
    error_message << v + ","
  end
  error_message
end

#get_responseObject

Returns any status code(s) or error code(s) received



63
64
65
66
67
68
# File 'lib/paytrace/api/response.rb', line 63

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

Returns:

  • (Boolean)


23
24
25
# File 'lib/paytrace/api/response.rb', line 23

def has_errors?
  @errors.length > 0
end

#parse_errors(response_string) ⇒ Object

Called by the framework in the event of an error response



47
48
49
50
51
52
53
54
# File 'lib/paytrace/api/response.rb', line 47

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_response(response_string, multi_value_fields = []) ⇒ Object

Called by the initialize method



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/paytrace/api/response.rb', line 28

def parse_response(response_string, multi_value_fields = [])

  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 multi_value_fields.include?(k)
      @values[k] ||= []
      @values[k] << Hash[v.split(@multi_value_delim).map {|pair| pair.split('=')}]
    else
      @values[k] = v
    end
  end
end

#response_codeObject

Returns the response code(s) received



18
19
20
# File 'lib/paytrace/api/response.rb', line 18

def response_code
  get_response
end