Class: Gendered::Guesser

Inherits:
Object
  • Object
show all
Defined in:
lib/gendered/guesser.rb

Constant Summary collapse

ENDPOINT =
"https://api.genderize.io".freeze
USAGE_HEADERS =
{
  "X-Rate-Limit-Limit" => :limit,
  "X-Rate-Limit-Remaining" => :remaining,
  "X-Rate-Reset" => :reset
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names, options = {}) ⇒ Guesser

Returns a new instance of Guesser.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/gendered/guesser.rb', line 15

def initialize(names, options = {})
  @names = Array(names)
  raise ArgumentError, "names cannot be empty" if @names.empty?

  @options = Gendered.config.merge(options || {})
  @options[:connection] ||= {}
  @usage = { :limit => nil, :remaining => nil, :reset => nil }
end

Instance Attribute Details

#namesObject

Returns the value of attribute names.



13
14
15
# File 'lib/gendered/guesser.rb', line 13

def names
  @names
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/gendered/guesser.rb', line 13

def options
  @options
end

#usageObject (readonly)

Returns the value of attribute usage.



12
13
14
# File 'lib/gendered/guesser.rb', line 12

def usage
  @usage
end

Instance Method Details

#guess!Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gendered/guesser.rb', line 24

def guess!
  response = request(request_options)
  update_usage(response)
  body = parse(response.body)
  case response.code
  when 200
    create_names(body)
  when 429
    raise RateLimitError.new(body["error"], *@usage.values_at(:limit, :remaining, :reset))
  else
    raise GenderedError.new(body["error"])
  end
end