<commit_before># encoding: utf-8

require 'sinatra/base'
require 'yaml'

module Sinatra

  module WaysAndMeans

    # HTTP verbs list
    VERBS = %w|get post patch put delete head options|.freeze

    def ways_and_means!(ways_and_means=nil)
      # Pfff'... Should probably be more tyrannical here...
      # if you registered, you should know what you're doing, right ?
      return unless @config = ways_and_means || config

      # the verb
      # the endpoint to string prefixed by '/'
      # the callback
      #
      # get '/plop' do
      #   callback
      # end
      ways do |endpoint, dispatch|
        send dispatch[:verb], "/#{endpoint}" do
          send dispatch[:to]
        end
      end

      # settings
      means
    end

    private

    def ways
      # i allow people to be less funky than i am with key names, hence 'routes'
      (config['ways'] || config['routes'] || {}).each do |endpoint, dispatch|
        # home: { get: { to: 'home', other_params: "plop" }, patch: { to: 'patch_home', other_params: "plip" } }
        # get '/home' do
        #   home
        # end
        #
        # patch '/home' do
        #   patch_home
        # end
        if dispatch.is_a?(Hash) && dispatch.keys.any? { |k| VERBS.include? k }
          dispatch.each do |k, v|
            yield endpoint, v.merge(verb: k)
          end
        # index: { to: 'show_index', other_params: "plop" }
        # get '/index' do  ## no verb specified, defaulted to 'get'
        #   show_index
        # end
        elsif dispatch.is_a?(Hash)
          yield endpoint, { verb: 'get' }.merge!(dispatch)
        # show: nil
        # get '/show' do  ## no verb specified, defaulted to 'get'
        #   show
        # end
        #
        # 'show/:person_id': "show_person"
        # get '/show/:person_id' do  ## no verb specified, defaulted to 'get'
        #   show_person
        # end
        else
          yield endpoint, { verb: 'get', to: (dispatch || endpoint) }
        end
      end
    end

    def means
      # set key / values in App.settings
      means_config do |mean, it|<commit_msg>Accepts symbol and string keys in configuration<commit_after># encoding: utf-8

require 'sinatra/base'
require 'yaml'

module Sinatra

  module WaysAndMeans

    # HTTP verbs list
    VERBS = %w|get post patch put delete head options|.freeze

    def ways_and_means!(ways_and_means=nil)
      # Pfff'... Should probably be more tyrannical here...
      # if you registered, you should know what you're doing, right ?
      return unless @config = ways_and_means || config

      # the verb
      # the endpoint to string prefixed by '/'
      # the callback
      #
      # get '/plop' do
      #   callback
      # end
      ways do |endpoint, dispatch|
        send dispatch[:verb], "/#{endpoint}" do
          send dispatch[:to]
        end
      end

      # settings
      means
    end

    private

    def ways
      ways_config.each do |endpoint, dispatch|
        # home: { get: { to: 'home', other_params: "plop" }, patch: { to: 'patch_home', other_params: "plip" } }
        # get '/home' do
        #   home
        # end
        #
        # patch '/home' do
        #   patch_home
        # end
        if dispatch.is_a?(Hash) && dispatch.keys.any? { |k| VERBS.include? k }
          dispatch.each do |k, v|
            yield endpoint, v.merge(verb: k)
          end
        # index: { to: 'show_index', other_params: "plop" }
        # get '/index' do  ## no verb specified, defaulted to 'get'
        #   show_index
        # end
        elsif dispatch.is_a?(Hash)
          yield endpoint, { verb: 'get' }.merge!(dispatch)
        # show: nil
        # get '/show' do  ## no verb specified, defaulted to 'get'
        #   show
        # end
        #
        # 'show/:person_id': "show_person"
        # get '/show/:person_id' do  ## no verb specified, defaulted to 'get'
        #   show_person
        # end
        else
          yield endpoint, { verb: 'get', to: (dispatch || endpoint) }
        end
      end
    end

    def ways_config
      # at least this is safe and very explicit
      # hash.slice, i miss you...
      config['ways'] || config[:ways] || config['routes'] || config[:routes] || {}

      # while this is not
      # WAYS_KEYS.inject({}) { |w, k| w.merge!(config[k] || config[k.to_sym] || {}) }

      # and this is ruby-coated Perl
      # WAYS_KEYS.map { |k| [k, k.to_sym] }.flatten.map { |k| config[k] }.compact.first
    end
 
    def means
      # set key / values in App.settings
      means_config do |mean, it|
{'fraction_non_alphanumeric': 0.11952714535901926, 'fraction_numerical': 0.00043782837127845885, 'mean_word_length': 1.852059925093633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3860542', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1405, 'n_words': 443}