Skip to content

Instantly share code, notes, and snippets.

@ar1a
Created January 24, 2018 08:39
Show Gist options
  • Save ar1a/a244b86c19df2b28e5ad84ba06ddddfa to your computer and use it in GitHub Desktop.
Save ar1a/a244b86c19df2b28e5ad84ba06ddddfa to your computer and use it in GitHub Desktop.
require 'faraday'
require 'json'
require 'stringio'
class String
def titleize
split(/ |\_/).map(&:capitalize).join(' ')
end
end
o
class RandomPerson
def initialize
data = JSON.parse(Faraday.get('https://randomuser.me/api/?nat=us,ca,au,nz').body)['results'][0]
@picture = data['picture']['large']
@gender = data['gender'].capitalize
name = StringIO.new
name.print data['name']['title'].capitalize
name.print ' '
name.print data['name']['first'].capitalize
name.print ' '
name.print data['name']['last'].capitalize
@name = name.string
location = StringIO.new
location.puts data['location']['street'].titleize
location.puts data['location']['city'].titleize
location.print data['location']['state'].titleize
location.print ', '
location.print data['location']['postcode']
@location = location.string
@dob = data['dob']
@username = data['login']['username']
@email = data['email']
end
attr_reader :picture
attr_reader :gender
attr_reader :name
attr_reader :location
attr_reader :dob
attr_reader :username
attr_reader :email
end
person = RandomPerson.new
p person
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment