Hackernoon logoRuby on Rails and the Complexity of Fake User Profiles, Made Simple by@steviee

Ruby on Rails and the Complexity of Fake User Profiles, Made Simple

Stephan Eberle Hacker Noon profile picture

@stevieeStephan Eberle

Father, First basic code on a CBM 600, I also like hiking

When I was working on my last story I was first using our production database to show off some functions of a web application.

While the tool is used internally at New Work SE this would have worked for an internal blog or Slack post, however this is a totally different story for showing your workplace with actual people, where they sit, with whom to the public, without their consent or them even knowing.

So I needed to find a quick way to replace all profile images and names with fake data. Working with Rails I use factory_bot_rails (link) frequently to generate fake data, but I am missing a fitting avatar image when using this Ruby gem.

I started looking around and found Ramdomuser.me, which is a web app that profiles fake user profiles with logs of fake-data attributes and it even provides an API to pull multiple profiles at once.

The first thing I did was look at the actual response JSON from the service to orient myself:

Excited as I was, I really just needed to replace the avatar images and names with the ones from the fake profiles. After some further reading the API documentation I added some parameters to the query so the response got smaller.

The final result is this rake task:

As you can see there is no magic and the code however naive works flawlessly with our 2.6k users. I added the progress_bar Ruby gem for nicer output (it will also calculate when the job is done).

One little aspect here is the fact that I do not need to download the profile images into local temp files to again upload them to the database.

The User model that is used has a paperclip attribute for the user image and with a little extra method one can use the URI module to download and assign the avatar image file to the User.image like so:

Using URI to download and assign a profile image from a URL

This is in no way very sophisticated or elegant, but it gets the job done and I didn’t plan on build this feature in the first place and definitely not while writing a blog post. Again Ruby On Rails and its ecosystem made something complex easy to use so that I didn’t lose focus too much and was able to just like that create some “fake user” screenshots.

Thank you for reading and thank you, Randomuser.me, for providing an easy-to-use API service!

Also published on Medium's subdomain.


Join Hacker Noon

Create your free account to unlock your custom reading experience.