<commit_before>module StripeMock
  class Instance

    include StripeMock::RequestHandlers::Helpers
    include StripeMock::RequestHandlers::ParamValidators

    # Handlers are ordered by priority
    @@handlers = []

    def self.add_handler(route, name)
      @@handlers << {
        :route => %r{^#{route}$},
        :name => name
      }
    end

    def self.handler_for_method_url(method_url)
      @@handlers.find {|h| method_url =~ h[:route] }
    end

    include StripeMock::RequestHandlers::Charges
    include StripeMock::RequestHandlers::Cards
    include StripeMock::RequestHandlers::Sources
    include StripeMock::RequestHandlers::Subscriptions # must be before Customers
    include StripeMock::RequestHandlers::Customers
    include StripeMock::RequestHandlers::Coupons
    include StripeMock::RequestHandlers::Events
    include StripeMock::RequestHandlers::Invoices
    include StripeMock::RequestHandlers::InvoiceItems
    include StripeMock::RequestHandlers::Plans
    include StripeMock::RequestHandlers::Recipients
    include StripeMock::RequestHandlers::Transfers
    include StripeMock::RequestHandlers::Tokens


    attr_reader :bank_tokens, :charges, :coupons, :customers, :events,
                :invoices, :invoice_items, :plans, :recipients, :transfers, :subscriptions

    attr_accessor :error_queue, :debug

    def initialize
      @bank_tokens = {}
      @card_tokens = {}
      @customers = {}
      @charges = {}
      @coupons = {}
      @events = {}
      @invoices = {}
      @invoice_items = {}
      @plans = {}
      @recipients = {}
      @transfers = {}
      @subscriptions = {}

      @debug = false
      @error_queue = ErrorQueue.new
      @id_counter = 0
      @balance_transaction_counter = 0

      # This is basically a cache for ParamValidators
      @base_strategy = TestStrategies::Base.new
    end

    def mock_request(method, url, api_key, params={}, headers={}, api_base_url=nil)
      return {} if method == :xtest

      # Ensure params hash has symbols as keys
      params = Stripe::Util.symbolize_names(params)

      method_url = "#{method} #{url}"

      if handler = Instance.handler_for_method_url(method_url)
        if @debug == true
          puts "- - - - " * 8
          puts "[StripeMock req]::#{handler[:name]} #{method} #{url}"
          puts "                  #{params}"
        end

        if mock_error = @error_queue.error_for_handler_name(handler[:name])
          @error_queue.dequeue
          raise mock_error
        else
          res = self.send(handler[:name], handler[:route], method_url, params, headers)
          puts "           [res]  #{res}" if @debug == true
          [res, api_key]
        end
      else
        puts "[StripeMock] Warning : Unrecognized endpoint + method : [#{method} #{url}]"
        puts "[StripeMock] params: #{params}"
        [{}, api_key]
      end
    end
<commit_msg>Show params only if present <commit_after>module StripeMock
  class Instance

    include StripeMock::RequestHandlers::Helpers
    include StripeMock::RequestHandlers::ParamValidators

    # Handlers are ordered by priority
    @@handlers = []

    def self.add_handler(route, name)
      @@handlers << {
        :route => %r{^#{route}$},
        :name => name
      }
    end

    def self.handler_for_method_url(method_url)
      @@handlers.find {|h| method_url =~ h[:route] }
    end

    include StripeMock::RequestHandlers::Charges
    include StripeMock::RequestHandlers::Cards
    include StripeMock::RequestHandlers::Sources
    include StripeMock::RequestHandlers::Subscriptions # must be before Customers
    include StripeMock::RequestHandlers::Customers
    include StripeMock::RequestHandlers::Coupons
    include StripeMock::RequestHandlers::Events
    include StripeMock::RequestHandlers::Invoices
    include StripeMock::RequestHandlers::InvoiceItems
    include StripeMock::RequestHandlers::Plans
    include StripeMock::RequestHandlers::Recipients
    include StripeMock::RequestHandlers::Transfers
    include StripeMock::RequestHandlers::Tokens


    attr_reader :bank_tokens, :charges, :coupons, :customers, :events,
                :invoices, :invoice_items, :plans, :recipients, :transfers, :subscriptions

    attr_accessor :error_queue, :debug

    def initialize
      @bank_tokens = {}
      @card_tokens = {}
      @customers = {}
      @charges = {}
      @coupons = {}
      @events = {}
      @invoices = {}
      @invoice_items = {}
      @plans = {}
      @recipients = {}
      @transfers = {}
      @subscriptions = {}

      @debug = false
      @error_queue = ErrorQueue.new
      @id_counter = 0
      @balance_transaction_counter = 0

      # This is basically a cache for ParamValidators
      @base_strategy = TestStrategies::Base.new
    end

    def mock_request(method, url, api_key, params={}, headers={}, api_base_url=nil)
      return {} if method == :xtest

      # Ensure params hash has symbols as keys
      params = Stripe::Util.symbolize_names(params)

      method_url = "#{method} #{url}"

      if handler = Instance.handler_for_method_url(method_url)
        if @debug == true
          puts "- - - - " * 8
          puts "[StripeMock req]::#{handler[:name]} #{method} #{url}"
          puts "                  #{params}"
        end

        if mock_error = @error_queue.error_for_handler_name(handler[:name])
          @error_queue.dequeue
          raise mock_error
        else
          res = self.send(handler[:name], handler[:route], method_url, params, headers)
          puts "           [res]  #{res}" if @debug == true
          [res, api_key]
        end
      else
        puts "[StripeMock] Warning : Unrecognized endpoint + method : [#{method} #{url}]"
        puts "[StripeMock] params: #{params}" unless params.empty?
        [{}, api_key]
      end
    end

{'fraction_non_alphanumeric': 0.11496225120109815, 'fraction_numerical': 0.001029512697323267, 'mean_word_length': 2.817288801571709, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3367182', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1729, 'n_words': 375}