class Paymium::Client
Authenticated connection to the Paymium
API
Attributes
config[RW]
Public Class Methods
new(config = {})
click to toggle source
Initialize a client
@param config [Hash] Symbol-keyed hash of the configuration
# File lib/paymium/client.rb, line 19 def initialize(config = {}) @config = config @host = URI.parse @config.delete(:host) || Paymium::DEFAULT_HOST end
Public Instance Methods
delete(path, params = {}, &block)
click to toggle source
Issue a DELETE request against the API
@param path [String] The request path @param params [Hash] The request parameters
# File lib/paymium/client.rb, line 54 def delete(path, params = {}, &block) uri = uri_from_path(path) uri.query = URI.encode_www_form params unless params.empty? request(Net::HTTP::Delete.new(uri), &block) end
get(path, params = {}, &block)
click to toggle source
Issue a GET request against the API
@param path [String] The request path @param params [Hash] The request parameters
# File lib/paymium/client.rb, line 30 def get(path, params = {}, &block) uri = uri_from_path(path) uri.query = URI.encode_www_form params unless params.empty? request(Net::HTTP::Get.new(uri), &block) end
post(path, params = {}, &block)
click to toggle source
Issue a POST request against the API
@param path [String] The request path @param params [Hash] The request parameters
# File lib/paymium/client.rb, line 42 def post(path, params = {}, &block) req = Net::HTTP::Post.new(uri_from_path(path), {}) req.body = Oj.dump(params) request(req, &block) end
Private Instance Methods
handle_response(resp, &block)
click to toggle source
# File lib/paymium/client.rb, line 88 def handle_response(resp, &block) if resp.class == Net::HTTPNoContent block.nil? ? nil : block.call(resp) elsif resp.class < Net::HTTPSuccess parsed_resp = Oj.load(resp.body) block.nil? ? parsed_resp : block.call(resp) else raise Error, resp.body end end
request(req, &block)
click to toggle source
# File lib/paymium/client.rb, line 79 def request(req, &block) req.content_type = 'application/json' set_header_fields(req) if @config[:key] and @config[:secret] Net::HTTP.start(@host.host, @host.port, :use_ssl => @host.scheme == 'https') do |http| resp = http.request(req) handle_response(resp, &block) end end
set_header_fields(req)
click to toggle source
# File lib/paymium/client.rb, line 62 def set_header_fields(req) key = @config[:key] nonce = (Time.now.to_f * 10**6).to_i data = [nonce, req.uri.to_s, req.body].compact.join sig = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @config[:secret], data).strip req.add_field "Api-Key", key req.add_field "Api-Nonce", nonce req.add_field "Api-Signature", sig req end
uri_from_path(path)
click to toggle source
# File lib/paymium/client.rb, line 73 def uri_from_path(path) uri = @host.dup uri.path = "#{@host.path}/#{path}".gsub('//','/') uri end