Sha256: 84788183a47350d6a639c01454448ea16464b9bfe7d6b82ea1107bf4ae0c7d05

Contents?: true

Size: 933 Bytes

Versions: 2

Compression:

Stored size: 933 Bytes

Contents

module Gendered
  class Guesser

    attr_accessor :names

    def initialize(values)
      raise ArgumentError, "cannot be empty" if Array(values).empty?

      @names = Array(values)
    end

    def guess!
      response = HTTP.get(url)
      case response.code
      when 200
        @names = JSON.parse(response.body).collect do |guess|
          name = names.find { |n| n.to_s == guess["name"] }

          if name.is_a?(String)
            name = Name.new(guess["name"])
          end

          name.tap do |n|
            n.gender = guess["gender"].to_sym
            n.probability = guess["probability"]
            n.sample_size = guess["count"]
          end
        end
        self.names
      end
    end

    def url
      url = "http://api.genderize.io/?"
      name_queries = names.collect.with_index do |name, index|
        "name[#{index}]=#{name}"
      end
      url + name_queries.join("&")
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gendered-0.0.2 lib/gendered/guesser.rb
gendered-0.0.1 lib/gendered/guesser.rb