class Payrix::Resource::Base
Attributes
resource_name[R]
Public Class Methods
new(params, attrs)
click to toggle source
# File lib/payrix/resource/base.rb, line 6 def initialize(params, attrs) @attrs = attrs @request_options = Payrix::Http::RequestParams.new set(params) end
Public Instance Methods
create(params = {})
click to toggle source
# File lib/payrix/resource/base.rb, line 113 def create(params = {}) set(params) headers = build_headers headers['Content-Type'] = "application/json" method = 'post' url = Payrix.configuration.url endpoint = "#{@resource_name}" data = to_json body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers) @response = Payrix::Http::Response.new(body, status, self.class) validate_response end
delete(params = {})
click to toggle source
# File lib/payrix/resource/base.rb, line 153 def delete(params = {}) set(params) if !id if Payrix.configuration.exception_enabled raise Payrix::Exceptions::InvalidRequest.new('ID is required for this delete') else return false end end headers = build_headers headers['Content-Type'] = "application/json" method = 'delete' url = Payrix.configuration.url endpoint = "#{@resource_name}/#{id}" data = {} body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers) @response = Payrix::Http::Response.new(body, status, self.class) validate_response end
details()
click to toggle source
# File lib/payrix/resource/base.rb, line 68 def details @response.nil? ? [] : @response.details end
has_errors?()
click to toggle source
# File lib/payrix/resource/base.rb, line 60 def has_errors? @response.nil? ? false : @response.has_errors? end
has_more?()
click to toggle source
# File lib/payrix/resource/base.rb, line 76 def has_more? @response.nil? ? true : @response.has_more? end
request_options()
click to toggle source
# File lib/payrix/resource/base.rb, line 13 def request_options @request_options end
request_options=(params = {})
click to toggle source
# File lib/payrix/resource/base.rb, line 17 def request_options=(params = {}) @request_options.sort = params['sort'] unless params['sort'].nil? @request_options.expand = params['expand'] unless params['expand'].nil? @request_options.totals = params['totals'] unless params['totals'].nil? @request_options.page = params['page'] unless params['page'].nil? end
response()
click to toggle source
# File lib/payrix/resource/base.rb, line 64 def response @response.nil? ? [] : @response.response end
retrieve(params = {})
click to toggle source
# File lib/payrix/resource/base.rb, line 80 def retrieve(params = {}) set(params) headers = build_headers search = build_search(to_json) search += "&#{request_options.sort}" if request_options headers['SEARCH'] = search headers['Content-Type'] = "application/json" query_params = [] if request_options query_params << request_options.expand if request_options.expand query_params << request_options.page if request_options.page if request_options.totals headers['TOTALS'] = request_options.totals end end method = 'get' url = Payrix.configuration.url endpoint = "#{@resource_name}?#{query_params.join('&')}" data = {} body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers) @response = Payrix::Http::Response.new(body, status, self.class) success = validate_response request_options.go_next_page if success success end
set(params)
click to toggle source
# File lib/payrix/resource/base.rb, line 24 def set(params) if params.is_a?(Hash) && !params.empty? params.each do |k, v| if @attrs.include? k public_send("#{k}=", v) if respond_to? "#{k}=" else self.class.send(:attr_accessor, k) self.instance_variable_set("@#{k}", v); end end end end
status()
click to toggle source
# File lib/payrix/resource/base.rb, line 56 def status @response.nil? ? false : @response.status end
to_json(nested = false)
click to toggle source
# File lib/payrix/resource/base.rb, line 37 def to_json(nested = false) excludes = ['request_options', 'resource_name', 'response', 'attrs'] instance_variables.inject({}) do |hash, var| key = var.to_s.delete('@') val = instance_variable_get(var) if !excludes.include? key if val.is_a? Base hash[key] = val.to_json(true) if nested else hash[key] = val end end hash end end
totals()
click to toggle source
# File lib/payrix/resource/base.rb, line 72 def totals @response.nil? ? [] : @response.totals end
update(params = {})
click to toggle source
# File lib/payrix/resource/base.rb, line 129 def update(params = {}) set(params) if !id if Payrix.configuration.exception_enabled raise Payrix::Exceptions::InvalidRequest.new('ID is required for this action') else return false end end headers = build_headers headers['Content-Type'] = "application/json" method = 'put' url = Payrix.configuration.url endpoint = "#{@resource_name}/#{id}" data = to_json body, status = Payrix::Http::Request.instance.send_http(method, url, endpoint, data, headers) @response = Payrix::Http::Response.new(body, status, self.class) validate_response end
Protected Instance Methods
build_headers()
click to toggle source
# File lib/payrix/resource/base.rb, line 178 def build_headers() config = Payrix.configuration if !config.url raise Payrix::Exceptions::InvalidRequest.new('Invalid URL') end headers = {} if config.api_key headers['APIKEY'] = config.api_key elsif config.session_key headers['SESSIONKEY'] = config.session_key end headers end
build_search(values = {})
click to toggle source
# File lib/payrix/resource/base.rb, line 196 def build_search(values = {}) values .delete_if { |k, v| v.nil? || v.empty? } .map { |k, v| "#{k}[equals]=#{v}" } .join('&') end
errors()
click to toggle source
# File lib/payrix/resource/base.rb, line 215 def errors @response.nil? ? [] : @response.errors end
validate_response()
click to toggle source
# File lib/payrix/resource/base.rb, line 203 def validate_response if @response.has_errors? if Payrix.configuration.exception_enabled raise Payrix::Exceptions::ApiError.new('There are errors in the response') end false else true end end