Sha256: c0521c9a9c32f247e812d1ac2786cc1fb9c1a735be6f52bce84c6022231f9624
Contents?: true
Size: 1.68 KB
Versions: 2
Compression:
Stored size: 1.68 KB
Contents
require 'cgi' require 'net/http' require 'uri' require 'paidy/errors/paidy_error' require 'paidy/errors/authentication_error' require 'paidy/errors/api_error' require 'paidy/charge' require 'paidy/version' module Paidy @api_base = 'https://api.paidy.com' @api_version = '2018-04-10' @use_ssl = true class << self attr_accessor :secret_key end def self.api_uri(path: '') URI.parse([@api_base, path].join('/')) end def self.request(method, path, params = {}, headers = {}) if secret_key.nil? raise Paidy::AuthenticationError.new('API key does not set.' \ 'You should set `Paidy.secret_key = YOUR_PAIDY_SECRET_KEY`.' ) end uri = api_uri(path: path) case method.to_s.downcase.to_sym # when :get # uri += (URI.parse(uri).query.present? ? '&' : '?') + query_parameter(params) when :post req = Net::HTTP::Post.new(uri) req['Content-Type'] = 'application/json' req['Paidy-Version'] = @api_version req['Authorization'] = "Bearer #{secret_key}" req.body = params.to_json req_options = { use_ssl: @use_ssl, } end response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| http.request(req) end body = JSON.parse(response.body) case response.code.to_i when 200 body else raise Paidy::ApiError.new "code: #{body['code']}, title: #{body['title']}, description: #{body['description']}" end end private def self.query_parameter(params) params.map do |k, v| if v.is_a?(Array) v.map{ |vv| "#{k}[]=#{CGI.encode(vv)}" }.join('&') else "#{k}=#{CGI.encode(v)}" end end.join('&') end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
paidy-0.0.2 | lib/paidy.rb |
paidy-0.0.1 | lib/paidy.rb |