<commit_before>require "net/http"
require "net/https"
require "json"
require "paymill/version"

module Paymill
  API_BASE    = "api.paymill.de"
  API_VERSION = "v2"

  @@api_key = nil

  autoload :Base,         "paymill/base"
  autoload :Client,       "paymill/client"
  autoload :Offer,        "paymill/offer"
  autoload :Payment,      "paymill/payment"
  autoload :Subscription, "paymill/subscription"
  autoload :Transaction,  "paymill/transaction"
  autoload :Webhook,      "paymill/webhook"

  module Operations
    autoload :All,    "paymill/operations/all"
    autoload :Create, "paymill/operations/create"
    autoload :Find,   "paymill/operations/find"
    autoload :Update, "paymill/operations/update"
    autoload :Delete, "paymill/operations/delete"
  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 = "/#{API_VERSION}/#{api_url}"
        https_request = case http_method
                        when :post
                          Net::HTTP::Post.new(url)
                        when :put
                          Net::HTTP::Put.new(url)
                        when :delete
                          Net::HTTP::Delete.new(url)
                        else
                          Net::HTTP::Get.new(path_with_params(url, data))
                        end
        https_request.basic_auth(api_key, "")
        https_request.set_form_data(data) if [:post, :put].include? http_method
        @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

    def path_with_params(path, params)
      if params != {}
        encoded_params = URI.encode_www_form(params)
        [path, encoded_params].join("?")
      else
        path<commit_msg>Clean up<commit_after>require "net/http"
require "net/https"
require "json"
require "paymill/version"

module Paymill
  API_BASE    = "api.paymill.de"
  API_VERSION = "v2"

  @@api_key = nil

  autoload :Base,         "paymill/base"
  autoload :Client,       "paymill/client"
  autoload :Offer,        "paymill/offer"
  autoload :Payment,      "paymill/payment"
  autoload :Subscription, "paymill/subscription"
  autoload :Transaction,  "paymill/transaction"
  autoload :Webhook,      "paymill/webhook"

  module Operations
    autoload :All,    "paymill/operations/all"
    autoload :Create, "paymill/operations/create"
    autoload :Find,   "paymill/operations/find"
    autoload :Update, "paymill/operations/update"
    autoload :Delete, "paymill/operations/delete"
  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 = "/#{API_VERSION}/#{api_url}"
        https_request = case http_method
                        when :post
                          Net::HTTP::Post.new(url)
                        when :put
                          Net::HTTP::Put.new(url)
                        when :delete
                          Net::HTTP::Delete.new(url)
                        else
                          Net::HTTP::Get.new(path_with_params(url, data))
                        end
        https_request.basic_auth(api_key, "")
        https_request.set_form_data(data) if [:post, :put].include? http_method
        @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

    def path_with_params(path, params)
      unless params.empty?
        encoded_params = URI.encode_www_form(params)
        [path, encoded_params].join("?")
      else
        path
{'fraction_non_alphanumeric': 0.10403225806451613, 'fraction_numerical': 0.00282258064516129, 'mean_word_length': 2.1862556197816314, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3985067', 'n_tokens_mistral': 1563, 'n_tokens_neox': 1516, 'n_words': 310}