module Prettyusers

Constants

VERSION

Public Class Methods

count_uri(count) click to toggle source

Build Count of results Uri (Max 100)

# File lib/prettyusers.rb, line 43
def self.count_uri(count)
    (!count.nil? and (1..100).include?(count)) ? '?results='+count.to_s : '?results=1'
end
gender_uri(gender) click to toggle source

Build Gender Uri

# File lib/prettyusers.rb, line 38
def self.gender_uri(gender)
    (!gender.nil? and ['male','female'].include? gender.downcase) ? '&gender='+gender : '' 
end
generate(options = {}) click to toggle source
# File lib/prettyusers.rb, line 93
def self.generate(options = {})
    self.run(options[:gender],options[:count])
end
random(options = {}) click to toggle source
# File lib/prettyusers.rb, line 89
def self.random(options = {})
    self.run(options[:gender],1)
end
run(gender,count) click to toggle source

Exec the request

# File lib/prettyusers.rb, line 48
def self.run(gender,count)
    gender  = self.gender_uri(gender)
    count   = self.count_uri(count)
    uri     = @@api_ul+count

    if (!gender.empty?)
        uri = @@api_ul+count+gender
    end
    
    uri  = URI.parse(uri)

    response = Net::HTTP.start(uri.host) do |http|
        http.get uri.request_uri, 'User-Agent' => 'PrettyUsers' 
    end

     case response
        when Net::HTTPRedirection
        when Net::HTTPSuccess
          response = JSON.parse(response.body, symbolize_names: true)
        
          if(response[:results].count>0)
              users = Array.new
              response[:results].each do |u|

                  r = u[:user] # minifying :)
                  name = {:title =>r[:name][:title], :firstname => r[:name][:first], :lastname => r[:name][:last]}
                  location = {:street => r[:location][:street], :city =>r[:location][:city], :state => r[:location][:state], :zip => r[:location][:zip]}
                 
                  u = User.new({:name => name,:username =>r[:username], :picture=>r[:picture],:gender => r[:gender],:location => location,:email => r[:email],:password =>r[:password], :md5_password =>r[:md5], :sha1_hash => r[:sha1], :phone => r[:phone], :cell => r[:cell], :SSN => r[:SSN]})
                  users.push(u)

              end
              users.count == 1 ? users.first : users
          end
        else
          response.error!
    end

    
end