Class: FakeNameGenerator
- Inherits:
-
Object
- Object
- FakeNameGenerator
- Defined in:
- lib/fake_name_generator.rb
Overview
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.
Defined Under Namespace
Classes: APIConnectionError, APIKeyInvalidError
Constant Summary collapse
- VERSION =
The current version of the gem
'0.0.3'
- API_URL =
The current FakeNameGenerator.com API endpoint
'http://svc.webservius.com/v1/CorbanWork/fakename'
- DEFAULT_OUTPUT =
'json'
- DEFAULT_COUNTRY =
'us'
- DEFAULT_NAMESET =
'us'
- DEFAULT_GENDER =
random
'0'
- VALID_COUNTRY_CODES =
The currently valid country codes you can use
['as', 'au', 'bg', 'ca', 'cyen', 'cygk', 'dk', 'fi', 'fr', 'gr', 'hu', 'is', 'it', 'nl', 'no', 'pl', 'sl', 'sp', 'sw', 'sz', 'uk', 'us']
- VALID_NAMESET_CODES =
The currently valid nameset codes you can use
[ 'ar', 'au', 'ch', 'dk', 'en', 'er', 'fa', 'fi', 'fr', 'gd', 'gr', 'hr', 'hu', 'ig', 'is', 'it', 'jp', 'jpja', 'nl', 'pl', 'sl', 'sp', 'sw', 'us', 'vn', 'zhtw']
- VALID_GENDER_CODES =
The currently valid gender codes you can use 0 = random 1 = male 2 = female
['0', '1', '2']
Instance Attribute Summary collapse
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#gender ⇒ Object
readonly
Returns the value of attribute gender.
-
#nameset ⇒ Object
readonly
Returns the value of attribute nameset.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ FakeNameGenerator
constructor
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).
-
#to_json ⇒ Object
Return the current fake name attributes as JSON.
Constructor Details
#initialize(options = {}) ⇒ FakeNameGenerator
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)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/fake_name_generator.rb', line 86 def initialize(={}) [:api_key] || [:json_data] or raise ArgumentError, "No API key or JSON data provided" @api_key = [:api_key] @country = [:country] || DEFAULT_COUNTRY @nameset = [:nameset] || DEFAULT_NAMESET @gender = [: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 [:json_data] @data = JSON.parse([: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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym) ⇒ Object (private)
134 135 136 137 138 139 140 141 142 |
# File 'lib/fake_name_generator.rb', line 134 def method_missing(sym) if data['identity'][sym.to_s] data['identity'][sym.to_s]['value'] elsif METHOD_ALIASES[sym] data['identity'][METHOD_ALIASES[sym].to_s]['value'] else raise NoMethodError, "undefined method \`#{sym}' for #{self}" end end |
Instance Attribute Details
#country ⇒ Object (readonly)
Returns the value of attribute country.
79 80 81 |
# File 'lib/fake_name_generator.rb', line 79 def country @country end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
79 80 81 |
# File 'lib/fake_name_generator.rb', line 79 def data @data end |
#gender ⇒ Object (readonly)
Returns the value of attribute gender.
79 80 81 |
# File 'lib/fake_name_generator.rb', line 79 def gender @gender end |
#nameset ⇒ Object (readonly)
Returns the value of attribute nameset.
79 80 81 |
# File 'lib/fake_name_generator.rb', line 79 def nameset @nameset end |
Instance Method Details
#to_json ⇒ Object
Return the current fake name attributes as JSON
118 119 120 |
# File 'lib/fake_name_generator.rb', line 118 def to_json @data.to_json end |