Class: OrbipayPaymentsapiClient::CommonUtil::CommonUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/orbipay_paymentsapi_client/common_util.rb

Overview

.

############################################################################################
## This class subject to change without prior notice, Please dont use this class directly.##
############################################################################################

Class Method Summary collapse

Class Method Details

.convert_to_type(data, return_type) ⇒ Object

[View source]

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 115

def self.convert_to_type(data, return_type)
    return nil if data.nil?
    case return_type
    when 'String'
        data.to_s
    when 'Integer'
        data.to_i
    when 'Float'
        data.to_f
    when 'BOOLEAN'
        data == true
    when 'DateTime'
        # parse date time (expecting ISO 8601 format)
        DateTime.parse data
    when 'Date'
        # parse date time (expecting ISO 8601 format)
        Date.parse data
    when 'Object'
        # generic object (usually a Hash), return directly
        data
    when /\AArray<(.+)>\z/
        # e.g. Array<Pet>
        sub_type = $1
        data.map { |item| convert_to_type(item, sub_type) }
    when /\AHash\<String, (.+)\>\z/
        # e.g. Hash<String, Integer>
        sub_type = $1
        {}.tap do |hash|
            data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
        end
    else
        # models, e.g. Pet
        OrbipayPaymentsapiClient::OrbipayPaymentsapiClientModels.const_get(return_type).new.tap do |model|
            model.build_from_hash data
        end
    end
end

.deserialize(response, return_type) ⇒ Object

[View source]

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 86

def self.deserialize(response, return_type)
    body = response

    # handle file downloading - return the File instance processed in request callbacks
    # note that response body is empty when the file is written in chunks in request on_body callback
    return @tempfile if return_type == 'File'

    return nil if body.nil?
    # return response body directly for String return type
    return body if return_type == 'String'

    # ensuring a default content type
    # content_type = response.headers['Content-Type'] || 'application/json'

    # fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)

    begin
        data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
    rescue JSON::ParserError => e
        if %w(String Date DateTime).include?(return_type)
            data = body
        else
            raise e
        end
    end

    convert_to_type data, return_type
end

.get_invocation_context(headers) ⇒ Object

[View source]

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 46

def self.get_invocation_context(headers)
	invocation_context = InvocationContext.new
	unless headers.nil?
		if headers.key?("product") && !headers["product"].nil?
			invocation_context.set_values(product: headers["product"])
		end
		if headers.key?("idempotent_request_key") && !headers["idempotent_request_key"].nil?
			invocation_context.set_values(idempotent_request_key: headers["idempotent_request_key"])
		end
		if headers.key?("request_uuid") && !headers["request_uuid"].nil?
			invocation_context.set_values(request_uuid: headers["request_uuid"])
		end
		if headers.key?("channel") && !headers["channel"].nil?
			invocation_context.set_values(channel: headers["channel"])
		end
		if headers.key?("requestor") && !headers["requestor"].nil?
			invocation_context.set_values(requestor: headers["requestor"])
		end
		if headers.key?("client_key") && !headers["client_key"].nil?
			invocation_context.set_values(client_key: headers["client_key"])
		end
		if headers.key?("X-OPAY-Headers") && !headers["X-OPAY-Headers"].nil?
			invocation_context.set_values(x_opay_headers: headers["X-OPAY-Headers"])
		end
		if headers.key?("requestor_type") && !headers["requestor_type"].nil?
			invocation_context.set_values(requestor_type: headers["requestor_type"])
		end
		if headers.key?("response_codes") && !headers["response_codes"].nil?
			invocation_context.set_values(response_codes: headers["response_codes"])
		end
		if headers.key?("response_text") && !headers["response_text"].nil?
			invocation_context.set_values(response_text: headers["response_text"])
		end
		if headers.key?("timestamp") && !headers["timestamp"].nil?
			invocation_context.set_values(timestamp: headers["timestamp"])
		end
	end		    
	invocation_context
end

.get_masked_properties(obj) ⇒ Object

[View source]

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 191

def self.get_masked_properties(obj)
  data = obj.clone
  hash_data = CommonUtil.object_to_hash(data)
  sensitive_info = CommonUtil.get_sensitive_info
  if hash_data.is_a?(Hash)
    hash_data = CommonUtil.mask_hash_data(hash_data, sensitive_info)
  elsif hash_data.is_a?(Array)
    hash_data = CommonUtil.mask_list_data(hash_data, sensitive_info)
  elsif hash_data.is_a?(String) or hash_data.is_a?(Numeric)
    sensitive_info.each do |s_key, s_value|
      if s_key.to_s == hash_data.to_s
        hash_data = CommonUtil.mask_string(hash_data, s_value)
      end
    end
  end
  return hash_data
end

.get_proxy_urlObject

[View source]

254
255
256
257
258
259
260
261
262
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 254

def self.get_proxy_url
begin
 proxy_url = nil
   if ENV.key?(PROXY_VARIABLE) and !ENV[PROXY_VARIABLE].nil?
     proxy_url = ENV[PROXY_VARIABLE].to_s
   end
 proxy_url
end
end

.get_sensitive_infoObject

[View source]

154
155
156
157
158
159
160
161
162
163
164
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 154

def self.get_sensitive_info
  return {
      account_number: 3,
      ssn: 0,
      card_cvv_number: 0,
      secret: 0,
      expiry_date: 0,
      custom_fields: 2,
      customer_reference: 3,
  }
end

.is_valid_json(json) ⇒ Object

[View source]

20
21
22
23
24
25
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 20

def self.is_valid_json(json)
   	JSON.parse(json)
   	return true
 		rescue JSON::ParserError => e
   	return false
end

.mask_complete_hash(data, visible_length) ⇒ Object

[View source]

176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 176

def self.mask_complete_hash(data, visible_length)
  data.each do |key, value|
    if key.is_a?(String) or key.is_a?(Symbol)
      if value.is_a?(String) or value.is_a?(Numeric)
          data[key] = CommonUtil.mask_string(value, visible_length)
      end
      elsif value.is_a?(Hash)
        data[key] = CommonUtil.mask_complete_hash(value, visible_length)
      elsif value.is_a?(Array)
        data[key] = CommonUtil.mask_list_data(value, sensitive_info)
      end
    end
  data
end

.mask_hash_data(data, sensitive_info) ⇒ Object

[View source]

210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 210

def self.mask_hash_data(data, sensitive_info)
  data.each do |key, value|
    if key.is_a?(String) or key.is_a?(Symbol)
      if value.is_a?(String) or value.is_a?(Numeric)
        sensitive_info.each do |s_key, s_value|
          if s_key.to_s == key.to_s
            data[key] = CommonUtil.mask_string(value, s_value)
          end
        end
      elsif value.is_a?(Hash)
        sensitive_info.each do |s_key, s_value|
          if s_key.to_s == key.to_s
            data[key] = CommonUtil.mask_complete_hash(value, s_value)
          else
            data[key] = CommonUtil.mask_hash_data(value, sensitive_info)
          end
          end
      elsif value.is_a?(Array)
        data[key] = CommonUtil.mask_list_data(value, sensitive_info)
      end
    end
  end
  return data
end

.mask_list_data(data, sensitive_info) ⇒ Object

[View source]

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 236

def self.mask_list_data(data, sensitive_info)
  data.each_with_index do |item, index|
    if item.is_a?(String) or item.is_a?(Numeric)
      sensitive_info.each do |s_key, s_value|
        if s_key.to_s == item.to_s
          data[index] = CommonUtil.mask_string(item, s_value)
        end
      end
    elsif item.is_a?(Array)
      data[index] = CommonUtil.mask_list_data(item, sensitive_info)
    elsif item.is_a?(Hash)
      data[index] = CommonUtil.mask_hash_data(item, sensitive_info)
    end
  end
  return data
end

.mask_string(data, visible_length) ⇒ Object

[View source]

167
168
169
170
171
172
173
174
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 167

def self.mask_string(data, visible_length)
  data = data.to_s
  result = "**********"
  if data.length > visible_length
  	result += data.chars.last(visible_length).join
  end
  result
end

.object_to_hash(obj) ⇒ Object

[View source]

38
39
40
41
42
43
44
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 38

def self.object_to_hash(obj)
    if obj.respond_to?(:to_hash)
        obj.to_hash
    else
        obj
    end
end

.object_to_http_body(model) ⇒ Object

[View source]

27
28
29
30
31
32
33
34
35
36
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 27

def self.object_to_http_body(model)
    return model if model.nil? || model.is_a?(String)
    local_body = nil
    if model.is_a?(Array)
        local_body = model.map { |m| object_to_hash(m) }
    else
        local_body = object_to_hash(model)
    end
    local_body.to_json
end

.time_stampObject

[View source]

14
15
16
17
18
# File 'lib/orbipay_paymentsapi_client/common_util.rb', line 14

def self.time_stamp()
    time_stamp = Time.now
    time_stamp.gmtime
    time_stamp.strftime('%Y-%m-%d %H:%M:%S.%3N%:z')
end