<commit_before>module Barometer
  class Base
    attr_reader :query, :weather

    def initialize(query)
      @query = Barometer::Query.new(query)
      @weather = Barometer::Weather.new
    end

    def measure(metric=nil)
      @weather.start_at = Time.now.utc

      level = 1
      until @weather.success?
        if sources = Barometer.config[level]
          _dig(sources, nil, metric)
        else
          raise OutOfSources
        end
        level += 1
      end

      @weather.end_at = Time.now.utc
      @weather
    end

    private

    # iterate through the setup until we have a source name (and possibly
    # a config for that source), then measure with that source
    #
    # this allows for many different config formats, like
    # { 1 => :wunderground }
    # { 1 => [:wunderground]}
    # { 1 => [:wunderground, :yahoo]}
    # { 1 => [:wunderground, {:yahoo => {:weight => 2}}]}
    # { 1 => {:wunderground => {:weight => 2}}}
    # { 1 => [{:wunderground => {:weight => 2}}]}
    #
    def _dig(data, config, metric)
      if data.is_a?(String) || data.is_a?(Symbol)
        _measure(data, config, metric)
      elsif data.is_a?(Array)
        data.each do |datum|
          _dig(datum, nil, metric)
        end
      elsif data.is_a?(Hash)
        data.each do |datum, config|
          _dig(datum, config, metric)
        end
      end
    end

    def _measure(source, config, metric)
      options = { :metric => metric }
      options.merge!(config)

      response = Barometer::WeatherService.measure(source.to_sym, @query, options)
      response.weight = config[:weight] if config && config[:weight]

      @weather.responses << response
    end
  end
end<commit_msg>use ConfigReader in base<commit_after>module Barometer
  class Base
    attr_reader :query, :weather

    def initialize(query)
      @query = Barometer::Query.new(query)
      @weather = Barometer::Weather.new
    end

    def measure(metric=nil)
      @weather.start_at = Time.now.utc

      tier = 1
      until @weather.success?
        Utils::ConfigReader.each_service(tier) do |source, config|
          options = { :metric => metric }.merge!(config)
          _measure(source, options)
        end
        tier += 1
      end

      @weather.end_at = Time.now.utc
      @weather
    end

    private

    def _measure(source, options)
      response = Barometer::WeatherService.measure(source.to_sym, @query, options)
      response.weight = options[:weight] if options && options[:weight]

      @weather.responses << response
    end
  end
end
{'fraction_non_alphanumeric': 0.11697965571205007, 'fraction_numerical': 0.005086071987480438, 'mean_word_length': 2.3556430446194225, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '1164535', 'n_tokens_mistral': 823, 'n_tokens_neox': 773, 'n_words': 203}