Sha256: 24a330824e923b5fb5a6cd326ce767e4f485feea3cbd4e7ae20f68a22a2c732c

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

require 'typhoeus'
require 'user_agent_db'

module Genderize
  module Io
    class Base

      DEFAULT_HOST ||= 'https://api.genderize.io'
      HEADER_KEYS ||= %w[x_rate_limit_limit x_rate_limit_remaining x_rate_reset].freeze

      attr_reader :country_id, :data, :host, :language_id, :name, :request

      def initialize(name, host: DEFAULT_HOST, country_id: nil, language_id: nil)
        @name = name
        @host = host
        @country_id = country_id
        @language_id = language_id
        @request = nil
      end

      def self.determine(name, host: DEFAULT_HOST, country_id: nil, language_id: nil)
        klass = new(name, host: host, country_id: country_id, language_id: language_id)
        klass.determine
        klass
      end

      def url
        connector = @host.include?('?') ? '&' : '?'
        "#{@host}#{connector}#{param_name}#{param_country_id}#{param_language_id}"
      end

      private

      def generate_data
        return if @request.nil?
        Genderize::Io::Parser::Json.parse(@request.response_body)
      end

      def generate_headers
        return if @request.nil?
        Genderize::Io::Parser::Header.parse(@request.response_headers)
      end

      def generate_rate_limits
        headers = generate_headers
        return if headers.nil?
        HEADER_KEYS.each_with_object({}) { |key, hash| hash[key] = headers.send(key).to_i }
      end

      def generate_request
        return @request unless @request.nil?
        Typhoeus::Config.user_agent = UserAgentDB.random
        Typhoeus.get(url, accept_encoding: 'gzip,deflate')
      end

      def param_country_id
        "&country_id=#{@country_id}" unless @country_id.nil?
      end

      def param_language_id
        "&language_id=#{@language_id}" unless @language_id.nil?
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
genderize-io-1.0.1 lib/genderize/io/base.rb
genderize-io-1.0.0 lib/genderize/io/base.rb