class Rents::Connection
Attributes
api_version[RW]
auth[RW]
Attrs
domain[RW]
end_point[RW]
end_point_versioned[RW]
path[RW]
protocol[RW]
recurrent_rid[RW]
request_params[RW]
Public Class Methods
new(params = {})
click to toggle source
Constructor
# File lib/rents/connection.rb, line 15 def initialize(params = {}) # An work around added to prevent a lot of changes params = params.merge({test_env:true}) if Rents.test_env params = params.merge({debug:true}) if Rents.debug # Static part self.request_params = {transaction:params} setup_config self.domain = 'apprents.herokuapp.com' # 'rents.pagerenter.com.br' # If using test or Debug it is not production if params[:debug] || params[:test] self.protocol = 'http' self.domain = 'localhost:7000' else self.protocol = 'https' self.domain = 'rents-app.herokuapp.com' 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] setup_attrs(params) self.recurrent_rid = params[:rid] unless params[:rid].nil? end
Public Instance Methods
delete_json_request()
click to toggle source
DELETE json
# File lib/rents/connection.rb, line 89 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/rents/connection.rb, line 83 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/rents/connection.rb, line 55 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/rents/connection.rb, line 50 def get_request RestClient.get self.url_requested end
post_json_request()
click to toggle source
POST json
# File lib/rents/connection.rb, line 66 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/rents/connection.rb, line 61 def post_request RestClient.post self.url_requested, self.request_params end
put_json_request()
click to toggle source
PUT json
# File lib/rents/connection.rb, line 77 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/rents/connection.rb, line 72 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/rents/connection.rb, line 45 def url_requested "#{self.end_point}/#{self.api_version}/#{self.path}" end
Protected Instance Methods
custom_http_params()
click to toggle source
HTTP requests must have ‘[]’ on it key name to send Array
# File lib/rents/connection.rb, line 134 def custom_http_params setup_format_and_validators 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
setup_attrs(params)
click to toggle source
SetUp all attrs
# File lib/rents/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/rents/connection.rb, line 98 def setup_config self.auth = {app_id:Rents.app_id, secret_key:Rents.secret_key} self.request_params.merge!(auth:self.auth) end
setup_default_app()
click to toggle source
SetUp a default app
# File lib/rents/connection.rb, line 104 def setup_default_app # setup test_app path self.path = 'global_app' # Get the App & setup config app = get_json_request[:app] Rents.app_id = app[:id] Rents.secret_key = app[:secret] # Get the GlobalRecurrent & setup/config self.path = 'global_subscription' recurrence = get_json_request self.recurrent_rid = recurrence[:rid] return puts 'Please run: rails g rents:install' if Rents.app_id.nil? || Rents.secret_key.nil? self.auth = {app_id:Rents.app_id, secret_key:Rents.secret_key} self.request_params.merge!(auth:self.auth) 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/rents/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/rents/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/rents/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 = Rents::Currency.to_operator_str(self.amount) self.request_params[:transaction][:amount] = self.amount end end