class NitroPay::Connection
Attributes
api_version[RW]
auth[RW]
Attrs
domain[RW]
end_point[RW]
end_point_versioned[RW]
path[RW]
protocol[RW]
recurrent_tid[RW]
request_params[RW]
Public Class Methods
new(params = {})
click to toggle source
Constructor
# File lib/nitro_pay/connection.rb, line 15 def initialize(params = {}) # An work around added to prevent a lot of changes params = params.merge({test_env:true}) if NitroPay.test_env params = params.merge({debug:true}) if NitroPay.debug # Static part self.request_params = {transaction:params} self.domain = 'pay.nitrostart.me' # If using test or Debug it is not production if params[:debug] || params[:test] self.protocol = 'http' self.domain = 'pay.dev:4000' else self.protocol = 'https' self.domain = 'pay.nitrostart.me' end self.api_version = 'v1' self.end_point = "#{self.protocol}://#{self.domain}/api" self.end_point_versioned = "#{self.protocol}://#{self.domain}/api/#{self.api_version}" # Dynamic env setup_default_app if params[:test_env] # Setups setup_config setup_attrs(params) self.recurrent_tid = params[:tid] unless params[:tid].nil? end
Public Instance Methods
delete_json_request()
click to toggle source
DELETE json
# File lib/nitro_pay/connection.rb, line 91 def delete_json_request auth = self.request_params[:auth] resp = RestClient.delete self.url_requested, auth_app_id:auth[:app_id], auth_secret_key:auth[:secret_key] to_hash_with_symbols(resp) end
delete_request()
click to toggle source
DELETE http
# File lib/nitro_pay/connection.rb, line 85 def delete_request auth = self.request_params[:auth] RestClient.delete self.url_requested, app_id:auth[:app_id], secret_key:auth[:secret_key] end
get_json_request()
click to toggle source
GET json
# File lib/nitro_pay/connection.rb, line 57 def get_json_request resp = RestClient.get(self.url_requested) to_hash_with_symbols(resp).it_keys_to_sym end
get_request()
click to toggle source
GET http
# File lib/nitro_pay/connection.rb, line 52 def get_request RestClient.get self.url_requested end
post_json_request()
click to toggle source
POST json
# File lib/nitro_pay/connection.rb, line 68 def post_json_request resp = RestClient.post(self.url_requested, self.request_params) to_hash_with_symbols(resp) end
post_request()
click to toggle source
POST http
# File lib/nitro_pay/connection.rb, line 63 def post_request RestClient.post self.url_requested, self.request_params end
put_json_request()
click to toggle source
PUT json
# File lib/nitro_pay/connection.rb, line 79 def put_json_request resp = RestClient.put(self.url_requested, self.request_params) to_hash_with_symbols(resp) end
put_request()
click to toggle source
PUT http
# File lib/nitro_pay/connection.rb, line 74 def put_request RestClient.put self.url_requested, self.request_params end
url_requested()
click to toggle source
Full URL for the last request
# File lib/nitro_pay/connection.rb, line 47 def url_requested "#{self.end_point}/#{self.api_version}/#{self.path}" end
Protected Instance Methods
custom_http_params(skip_formatters=false)
click to toggle source
HTTP requests must have ‘[]’ on it key name to send Array
# File lib/nitro_pay/connection.rb, line 134 def custom_http_params(skip_formatters=false) setup_format_and_validators unless skip_formatters return if self.sold_items.nil? self.sold_items.each_with_index do |sold_item, i| self.request_params[:transaction]["sold_items[#{i}]"] = sold_item end end
get_global_subscription()
click to toggle source
get global subscription
# File lib/nitro_pay/connection.rb, line 117 def get_global_subscription # setup test_subscription path self.path = 'global_subscription' get_json_request end
setup_attrs(params)
click to toggle source
SetUp all attrs
# File lib/nitro_pay/connection.rb, line 124 def setup_attrs(params) # Dynamic part params.each do |key, value| next unless key.to_s.index('[]').nil? self.class.__send__(:attr_accessor, :"#{key}") self.__send__("#{key}=", value) end end
setup_config()
click to toggle source
Config Attrs
# File lib/nitro_pay/connection.rb, line 100 def setup_config self.auth = {app_id:NitroPay.app_id, secret_key:NitroPay.secret_key} self.request_params.merge!(auth:self.auth) end
setup_default_app()
click to toggle source
SetUp a default app
# File lib/nitro_pay/connection.rb, line 106 def setup_default_app # setup test_app path self.path = 'global_app' # Get the App & setup config app = get_json_request[:app] NitroPay.app_id = app[:id] NitroPay.secret_key = app[:secret] end
setup_format_and_validators()
click to toggle source
Validate params to prevent errors like BAD Request & format values like value to Operator format
# File lib/nitro_pay/connection.rb, line 144 def setup_format_and_validators validate_operator_format end
to_hash_with_symbols(json)
click to toggle source
Return the JSON in a Hash
with it keys in symbols
# File lib/nitro_pay/connection.rb, line 166 def to_hash_with_symbols(json) hashed = JSON.parse(json) hashed.is_a?(Array) ? hashed.each_with_index { |hash, i| hashed[i] = hash.it_keys_to_sym } : hashed.it_keys_to_sym hashed end
validate_operator_format()
click to toggle source
if necessary convert amount to operator value
# File lib/nitro_pay/connection.rb, line 149 def validate_operator_format # prevent fatal error return if self.amount.nil? # aux vars amount_str = self.amount.to_s format_regex = /[.,]/ # if nil (it is not formatted, so it is not necessary to convert it format) unless amount_str.match(format_regex).nil? return if self.request_params.nil? || self.request_params[:transaction].nil? self.amount = NitroPay::Currency.to_operator_str(self.amount) self.request_params[:transaction][:amount] = self.amount end end