<commit_before>module ForvoApiClient
  class Client
    attr_reader :api_key

    def initialize(api_key)
      @api_key = api_key
    end

    def word_pronunciations(word, options = {})
      wrap_into_objects = options.delete(:wrap_into_objects) || false

      response = send_request!('word-pronunciations', word, options)

      if wrap_into_objects
        response['items'].map { |item| OpenStruct.new(item) }
      else
        response['items']
      end
    end

    private

    # Options:
    #   - language
    #   - username
    #   - sex
    #     - m
    #     - f
    #   - rate
    #   - order
    #     - date-asc
    #     - date-desc
    #     - rate-asc
    #     - rate-desc
    #   - limit
    def send_request!(action, word, options = {})
      path = options_to_path(action, options.merge(word: word))
      response = Net::HTTP.get(API_DOMAIN, path)<commit_msg>improved work with network<commit_after>module ForvoApiClient
  class Client
    attr_reader :api_key

    def initialize(api_key)
      @api_key = api_key
    end

    def word_pronunciations(word, options = {})
      wrap_into_objects = options.delete(:wrap_into_objects) || false

      response = send_request!('word-pronunciations', word, options)

      if wrap_into_objects
        response['items'].map { |item| OpenStruct.new(item) }
      else
        response['items']
      end
    end

    private

    # Options:
    #   - language
    #   - username
    #   - sex
    #     - m
    #     - f
    #   - rate
    #   - order
    #     - date-asc
    #     - date-desc
    #     - rate-asc
    #     - rate-desc
    #   - limit

    def fetch!(path, limit = 10)
      raise ArgumentError, 'too many HTTP redirects' if limit == 0

      binding.pry

      response = Net::HTTP.get_response(API_DOMAIN, path)

      case response
      when Net::HTTPSuccess then response.body
      when Net::HTTPRedirection then fetch!(response['location'], limit - 1)
      else
        response.error!
      end
    end

    def send_request!(action, word, options = {})
      response = fetch!(options_to_path(action, options.merge(word: word)))
{'fraction_non_alphanumeric': 0.10694183864915573, 'fraction_numerical': 0.001876172607879925, 'mean_word_length': 2.0212464589235126, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '752524', 'n_tokens_mistral': 684, 'n_tokens_neox': 666, 'n_words': 142}