<commit_before>require 'json'
require 'open-uri'
require 'active_support/core_ext/module/attribute_accessors'
require "currency_duo"

module RateMan

  @@_ran_once = false
  @@google_api_url = "https://www.googleapis.com/customsearch/v1"

  mattr_accessor :api_key, :google_api_url, :custom_search_id

  def self.query_url(from_cur, to_cur)
    "#{@@google_api_url}?key=#{self.api_key}&cx=#{@@custom_search_id}&q=1%20#{from_cur}%20to%20#{to_cur}"
  end

  def self.setup
    yield self if @@_ran_once == false
    @@_ran_once = true
  end

  def self.cleanup
    @@_ran_once = false
    self.api_key = nil
    self.custom_search_id = nil
  end

  def self.raw_response(from_cur, to_cur)
    IO.read open(self.query_url(from_cur, to_cur))
  end

  def self.query(from_cur, to_cur)
    string = JSON.parse(RateMan.raw_response(from_cur, to_cur))['items'].first['snippet'].split('...')[1]
    unless string =~ /\d EUR = \d*\.\d* CHF [-\+]?\d*\.\d* \([-\+]?\d*\.\d*\%\)/
      puts string
      raise ArgumentError, "incorrect json"
    end
    string.split(' ')
  end

  def self.get(from_cur, to_cur)
    RateMan::CurrencyDuo.new('EUR', 'CHF').query.json
  end

  class << self
    alias_method :rate, :get
  end

end
<commit_msg>adding a loop to insure better chances to get a correct answer<commit_after>require 'json'
require 'open-uri'
require 'active_support/core_ext/module/attribute_accessors'
require "currency_duo"

module RateMan

  @@_ran_once = false
  @@google_api_url = "https://www.googleapis.com/customsearch/v1"

  mattr_accessor :api_key, :google_api_url, :custom_search_id

  def self.query_url(from_cur, to_cur)
    "#{@@google_api_url}?key=#{self.api_key}&cx=#{@@custom_search_id}&q=1%20#{from_cur}%20to%20#{to_cur}"
  end

  def self.setup
    yield self if @@_ran_once == false
    @@_ran_once = true
  end

  def self.cleanup
    @@_ran_once = false
    self.api_key = nil
    self.custom_search_id = nil
  end

  def self.raw_response(from_cur, to_cur)
    IO.read open(self.query_url(from_cur, to_cur))
  end

  def self.query(from_cur, to_cur)
    incorrect = true
    acceptable_time = true
    i = 0
    while incorrect && acceptable_time
      string = JSON.parse(RateMan.raw_response(from_cur, to_cur))['items'].first['snippet'].split('...')[1]
      incorrect = false if string =~ /\d EUR = \d*\.\d* CHF [-\+]?\d*\.\d* \([-\+]?\d*\.\d*\%\)/
      i += 1
      acceptable_time = false if i > 5
    end
    if incorrect
      raise ArgumentError, "incorrect json"
    end
    string.split(' ')
  end

  def self.get(from_cur, to_cur)
    RateMan::CurrencyDuo.new('EUR', 'CHF').query.json
  end

  class << self
    alias_method :rate, :get
  end

end

{'fraction_non_alphanumeric': 0.1522956326987682, 'fraction_numerical': 0.007838745800671893, 'mean_word_length': 3.519392917369309, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '7080986', 'n_tokens_mistral': 1053, 'n_tokens_neox': 1017, 'n_words': 200}