Module: Prettyusers

Defined in:
lib/prettyusers.rb,
lib/prettyusers/version.rb

Defined Under Namespace

Classes: User

Constant Summary collapse

VERSION =
"0.0.3.1"
@@api_ul =
'http://api.randomuser.me/'

Class Method Summary collapse

Class Method Details

.count_uri(count) ⇒ Object

Build Count of results Uri (Max 100)



43
44
45
# 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) ⇒ Object

Build Gender Uri



38
39
40
# File 'lib/prettyusers.rb', line 38

def self.gender_uri(gender)
    (!gender.nil? and ['male','female'].include? gender.downcase) ? '&gender='+gender : '' 
end

.generate(options = {}) ⇒ Object



93
94
95
# File 'lib/prettyusers.rb', line 93

def self.generate(options = {})
    self.run(options[:gender],options[:count])
end

.random(options = {}) ⇒ Object



89
90
91
# File 'lib/prettyusers.rb', line 89

def self.random(options = {})
    self.run(options[:gender],1)
end

.run(gender, count) ⇒ Object

Exec the request



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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