Sha256: c77afe96ca581bf9da026fa40bf70da877a8c1a236982f3a54346b39a53f39ba

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require "net/http"
require "net/https"
require "json"
require "paymill/version"

module Paymill
  API_BASE = "api.paymill.de"

  @@api_key = nil

  autoload :Client,       "paymill/client"
  autoload :CreditCard,   "paymill/credit_card"
  autoload :Offer,        "paymill/offer"
  autoload :Subscription, "paymill/subscription"
  autoload :Transaction,  "paymill/transaction"

  module Operations
    autoload :All,    "paymill/operations/all"
    autoload :Create, "paymill/operations/create"
    autoload :Delete, "paymill/operations/delete"
    autoload :Find,   "paymill/operations/find"
  end

  class PaymillError < StandardError
  end

  class AuthenticationError < PaymillError; end
  class APIError            < PaymillError; end

  class << self
    def api_key
      @@api_key
    end

    def api_key=(api_key)
      @@api_key = api_key
    end

    def request(http_method, api_url, data)
      raise AuthenticationError if api_key.nil?

      https              = Net::HTTP.new(API_BASE, Net::HTTP.https_default_port)
      https.use_ssl      = true
      https.verify_mode  = OpenSSL::SSL::VERIFY_PEER
      https.ca_file      = File.join(File.dirname(__FILE__), "data/paymill.crt")
      https.start do |connection|
        url = "/v1/#{api_url}"
        https_request = case http_method
              when :post
                Net::HTTP::Post.new(url)
              when :delete
                Net::HTTP::Delete.new(url)
              else
                Net::HTTP::Get.new(url)
              end
        https_request.basic_auth(api_key,"")
        https_request.set_form_data(data) if http_method == :post
        @response = https.request(https_request)
      end
      raise AuthenticationError if @response.code.to_i == 401
      raise APIError if @response.code.to_i >= 500

      data = JSON.parse(@response.body)
      raise APIError.new(data["error"]) if data["error"]

      data
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
paymill-0.0.3 lib/paymill.rb