Module: RUser

Defined in:
lib/ruser.rb,
lib/ruser/api.rb,
lib/ruser/person.rb,
lib/ruser/version.rb

Overview

RUser is a simple Ruby Gem to communicate with the randomuser.me API.

Defined Under Namespace

Modules: Api Classes: Person

Constant Summary collapse

VERSION =
'3.0.1'.freeze

Class Method Summary collapse

Class Method Details

.new(options = {}) ⇒ Person

Creates a new Ruser object

Examples:

Create a random user

Person.new()

Create a male user

Person.new({:gender => 'male'})

Create a female user

Person.new({:gender => 'female'})

Create a random user with a specific nationality

Person.new({:nat => 'US'})

Create a specific user from a seed

Person.new({:seed => 'lazyWolf'})

Create three users

Person.new({:results => 3})

Create three male users

Person.new({:results => 3. :gender => 'male'})

Parameters:

  • options (Hash) (defaults to: {})

    a set of options to pass to the template

Options Hash (options):

  • :gender (String) — default: nil

    the user's gender

  • :seed (String) — default: nil

    the user's unique seed value

  • :results (Integer) — default: 1

    the number of results to return

  • :nat (String) — default: nil

    the user's nationality

Returns:


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ruser.rb', line 34

def self.new(options = {})
  params = options.map { |k, v| "#{k}=#{v}" } || ''

  json = JSON.parse(Net::HTTP.get(URI([
    RUser::Api::URL,
    RUser::Api::VERSION,
    '',
    '?' + params.join('&')
  ].join('/'))))

  results(json)
end

.results(data) ⇒ Object

Handles results

Parameters:

  • data (Hash)

    the data to be processed.


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruser.rb', line 50

def self.results(data)
  results = []
  if data['results'].size > 1
    data['results'].each do |user|
      results << RUser::Person.new(user, data['nationality'], data['seed'])
    end
    results
  else
    RUser::Person.new(data['results'][0], data['nationality'], data['seed'])
  end
end