Class: PayTrace::API::Request
- Inherits:
-
Object
- Object
- PayTrace::API::Request
- Defined in:
- lib/paytrace/api/request.rb
Overview
An object representing an API request to be sent using a PayTrace::API::Gateway object
Instance Attribute Summary collapse
-
#discretionary_data ⇒ Object
readonly
:nodoc:.
-
#field_delim ⇒ Object
readonly
:nodoc:.
-
#params ⇒ Object
readonly
:nodoc:.
-
#value_delim ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#initialize ⇒ Request
constructor
Initializes a new Request object.
-
#set_discretionary(key, value = nil) ⇒ Object
Sets discretionary data keys and values * :key – the name of the setting * :value – the value of the setting.
-
#set_multivalue(param_name, items = {}) ⇒ Object
Sets multiple parameters with the same name using the custom delimiter * :param_name – the name of the “top level” setting * :items – a hash of “second level” settings.
-
#set_param(key, value = nil) ⇒ Object
Sets a single request parameters * :key – the name of the setting * :value – the value of the setting.
-
#set_params(keys, params) ⇒ Object
Sets multiple parameters at once * :keys – an array of key names to extract from the params hash * :params – the parameters hash to be extracted from.
-
#to_parms_string ⇒ Object
Returns the formatted URL that this request will send.
-
#validate_param(k, v) ⇒ Object
:nodoc:.
Constructor Details
#initialize ⇒ Request
Initializes a new Request object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/paytrace/api/request.rb', line 10 def initialize @field_delim = "|" @multi_field_delim = "+" @value_delim = "~" @multi_value_delim = "=" @params= { user_name: [PayTrace.configuration.user_name], password: [PayTrace.configuration.password], terms: ["Y"] } @discretionary_data = {} end |
Instance Attribute Details
#discretionary_data ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/paytrace/api/request.rb', line 6 def discretionary_data @discretionary_data end |
#field_delim ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/paytrace/api/request.rb', line 6 def field_delim @field_delim end |
#params ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/paytrace/api/request.rb', line 6 def params @params end |
#value_delim ⇒ Object (readonly)
:nodoc:
6 7 8 |
# File 'lib/paytrace/api/request.rb', line 6 def value_delim @value_delim end |
Instance Method Details
#set_discretionary(key, value = nil) ⇒ Object
Sets discretionary data keys and values
-
:key – the name of the setting
-
:value – the value of the setting
45 46 47 48 49 50 51 |
# File 'lib/paytrace/api/request.rb', line 45 def set_discretionary(key, value = nil) if key.is_a?(Hash) @discretionary_data = key else @discretionary_data[key] = value unless value.nil? end end |
#set_multivalue(param_name, items = {}) ⇒ Object
Sets multiple parameters with the same name using the custom delimiter
-
:param_name – the name of the “top level” setting
-
:items – a hash of “second level” settings
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/paytrace/api/request.rb', line 75 def set_multivalue(param_name, items = {}) result = (items.map do |k,v| validate_param(k, v) "#{PayTrace::API.fields[k]}#{@multi_value_delim}#{v}" end.join(@multi_field_delim)) set_param(param_name, result) result end |
#set_param(key, value = nil) ⇒ Object
Sets a single request parameters
-
:key – the name of the setting
-
:value – the value of the setting
62 63 64 65 66 67 68 69 70 |
# File 'lib/paytrace/api/request.rb', line 62 def set_param(key, value = nil) validate_param(key, value) unless value.nil? @params[key] ||= [] @params[key] << value end end |
#set_params(keys, params) ⇒ Object
Sets multiple parameters at once
-
:keys – an array of key names to extract from the params hash
-
:params – the parameters hash to be extracted from
89 90 91 92 93 |
# File 'lib/paytrace/api/request.rb', line 89 def set_params(keys, params) keys.each do |key| set_param(key, params[key]) end end |
#to_parms_string ⇒ Object
Returns the formatted URL that this request will send
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/paytrace/api/request.rb', line 26 def to_parms_string() raw_request = @params.map do |k,items| items.map do |item| "#{PayTrace::API.fields[k]}#{@value_delim}#{item}" end end.join(@field_delim) << @field_delim if @discretionary_data.any? raw_request << @discretionary_data.map do |k,v| "#{k}#{@value_delim}#{v}" end.join(@field_delim) << @field_delim end raw_request end |
#validate_param(k, v) ⇒ Object
:nodoc:
54 55 56 |
# File 'lib/paytrace/api/request.rb', line 54 def validate_param(k, v) raise PayTrace::Exceptions::ValidationError.new("Unknown field '#{k}'") unless PayTrace::API.fields.has_key?(k) end |