In Files

  • lib/fake_name_generator.rb
  • spec/fake_name_generator_spec.rb

FakeNameGenerator

Synopsis

The fake_name_generator gem provides a simple wrapper to the FakeNameGenerator.com API, which provides a randomized name, address information, as well as other indentifying information.

Example

require 'fake_name_generator'

fake_name = FakeNameGenerator.new(:api_key => 'VALID_API_KEY)
puts fake_name.full_name
puts fake_name.phone_number
puts fake_name.blood_type

If you would like to receive non-US English results, you can specify a country and nameset to use at the new call:

fake_name = FakeNameGenerator.new(
  :api_key => 'VALID_API_KEY',
  :country => 'it',
  :nameset => 'it')

To see what options are available for country and nameset, see FakeNameGenerator::VALID_COUNTRY_CODES and FakeNameGenerator::VALID_NAMESET_CODES, respectively.

You can also specify the gender you would prefer in the resulting fake name. To do so, use the :gender parameter:

fake_name = FakeNameGenerator.new(
  :api_key => 'VALID_API_KEY',
  :gender => '1')

Where ‘0’ is random, ‘1’ is male, and ‘2’ is female.

Constants

API_URL

The current FakeNameGenerator.com API endpoint

DEFAULT_COUNTRY
DEFAULT_GENDER
DEFAULT_NAMESET
DEFAULT_OUTPUT
METHOD_ALIASES
VALID_COUNTRY_CODES

The currently valid country codes you can use

VALID_GENDER_CODES

The currently valid gender codes you can use 0 = random 1 = male 2 = female

VALID_NAMESET_CODES

The currently valid nameset codes you can use

VERSION

The current version of the gem

Attributes

country[R]
data[R]
gender[R]
nameset[R]

Public Class Methods

new(options={}) click to toggle source

Parameters

  • api_key = API key for accessing the FakeNameGenerator.com API (required)

  • country = country-related values returned (default: 'us')

  • nameset = language-related names returned (default: 'us')

  • gender = specify whether random, male, or female values returned (default: random)

 
               # File lib/fake_name_generator.rb, line 86
def initialize(options={})
  options[:api_key] || options[:json_data] or raise ArgumentError, "No API key or JSON data provided"
  @api_key = options[:api_key]
  @country = options[:country] || DEFAULT_COUNTRY
  @nameset = options[:nameset] || DEFAULT_NAMESET
  @gender = options[:gender] || DEFAULT_GENDER

  raise ArgumentError, "Specified country parameter is not valid. Please see FakeNameGenerator::VALID_COUNTRY_CODES" unless VALID_COUNTRY_CODES.include?(@country)
  raise ArgumentError, "Specified nameset parameter is not valid. Please see FakeNameGenerator::VALID_NAMESET_CODES" unless VALID_NAMESET_CODES.include?(@nameset)
  raise ArgumentError, "Specified gender parameter is not valid. Please see FakeNameGenerator::VALID_GENDER_CODES" unless VALID_GENDER_CODES.include?(@gender)

  if options[:json_data]
    @data = JSON.parse(options[:json_data])
  else
    url = [API_URL, build_params].join('?')
    response = Net::HTTP.get_response(URI.parse(url))

    case response.code
    when '500' || 500
      raise APIConnectionError, "FakeNameGenerator API not working (500 Error)"
    when '403' || 403
      raise APIKeyInvalidError, "Provided API key is not valid (403 Error)"
    when '200' || 200
      @data = JSON.parse(response.body)
    else
      raise StandardError, "Unexpected response from FakeNameGenerator.com API"
    end
  end
end
            

Public Instance Methods

to_json() click to toggle source

Return the current fake name attributes as JSON

 
               # File lib/fake_name_generator.rb, line 118
def to_json
  @data.to_json
end