Sha256: 728cddd2e6586f5802ba4451bbb4774879c5c71f75b624b94af22279d77ae8ea

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'json'
require 'pin_payment/error'

module PinPayment
  class Base

    protected

    def self.post uri, options
      fetch Net::HTTP::Post, uri, options
    end

    def self.put uri, options
      fetch Net::HTTP::Put, uri, options
    end

    def self.get uri, options
      fetch Net::HTTP::Get, uri, options
    end

    # TODO: Accept card as a hash that would create the card at the same time as the charge
    def self.fetch klass, uri, options
      client             = Net::HTTP.new(uri.host, uri.port)
      client.use_ssl     = true
      client.verify_mode = OpenSSL::SSL::VERIFY_PEER
      response           = client.request(
        klass.new(uri.request_uri).tap do |http|
          http.basic_auth(PinPayment.secret_key, '')
          http['User-Agent'] = "#{self}/#{PinPayment::Version::STRING}"
          http.set_form_data options
        end
      )
      begin
        response = JSON.parse(response.body)
      rescue JSON::ParserError => e
        raise Error::InvalidResponse.new(e.message)
      end
      raise(Error.create(response['error'], response['error_description'], response['messages'])) if response['error']
      response['response']
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pin_payment-0.0.5 lib/pin_payment/base.rb