class MercadoPago::Core::Client
Constants
- API_VERSION
Attributes
public_key[R]
version[R]
Public Class Methods
new(public_key = nil, access_token = nil)
click to toggle source
Creates an instance to make calls to the MercadoPago
CustomCheckout
API.
# File lib/mercadopago/core/client.rb, line 8 def initialize(public_key = nil, access_token = nil) check_credentials!(public_key, access_token) @access_token = access_token if access_token @public_key = public_key if public_key @gateway = MercadoPago::Core::Gateway @version = API_VERSION end
Public Instance Methods
call(resource, action, data = {})
click to toggle source
# File lib/mercadopago/core/client.rb, line 17 def call(resource, action, data = {}) request = request_for!(resource.to_sym, action.to_sym) endpoint_path, payload = build_endpoint_path(request, data) @gateway.send(request[:method], endpoint_path, payload) end
Private Instance Methods
build_endpoint_path(request, data)
click to toggle source
# File lib/mercadopago/core/client.rb, line 49 def build_endpoint_path(request, data) path = request[:endpoint] % data.merge(version: version) ["#{path}?#{token_to_use(request)}", purged_data_keys(request[:endpoint], data)] rescue KeyError nil end
check_credentials!(public_key = nil, access_token = nil)
click to toggle source
# File lib/mercadopago/core/client.rb, line 26 def check_credentials!(public_key = nil, access_token = nil) if (public_key.nil? || public_key.empty?) && (access_token.nil? || access_token.empty?) raise ClientError.new('You must provide a :public_key or an :access_token') end end
purged_data_keys(raw_endpoint, data)
click to toggle source
# File lib/mercadopago/core/client.rb, line 63 def purged_data_keys(raw_endpoint, data) keys = raw_endpoint.scan(/%{(\w*)}/).flatten.map(&:to_sym) data.delete_if { |k,v| keys.include?(k) } end
request_for!(resource, action)
click to toggle source
# File lib/mercadopago/core/client.rb, line 33 def request_for!(resource, action) unless MercadoPago::Core::ENDPOINTS.keys.include?(resource) error = "There is no :#{resource} resource. The available ones are: " \ "[:#{MercadoPago::Core::ENDPOINTS.keys.join(', :')}]" raise ResourceError.new(error) end resource = MercadoPago::Core::ENDPOINTS[resource] unless resource.keys.include?(action) error = "There is no action called :#{action}. The available actions are: "\ "[:#{resource.keys.join(', :')}]" raise ResourceError.new(error) end resource[action] end
token_to_use(request)
click to toggle source
# File lib/mercadopago/core/client.rb, line 56 def token_to_use(request) token_name = request[:allowed_tokens].detect do |token| instance_variable_get("@#{token}") end "#{token_name}=#{instance_variable_get("@#{token_name}")}" if token_name end