Sha256: 3e31743da5f34a8ba9440c0742bc40ebf74a6007da6b3d5960c894a85631a71c

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require 'typhoeus' unless defined?(Typhoeus)

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)
        instance = new(name, host: host, country_id: country_id, language_id: language_id)
        instance.determine
        instance
      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.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

1 entries across 1 versions & 1 rubygems

Version Path
genderize-io-1.4.0 lib/genderize/io/base.rb