Using Unirest to build a terminal dictionary app

Unirest is a Ruby gem that can easily manage API requests of a site’s data. I recommend using it with the Chrome extension JSON Formatter to clearly parse the results. Once this is in place, setting up a GET request with Unirest is a simple process, allowing you access to data all over the Web.

Step 1: Download JSON Formatter. This will allow you to collapse and expand JSON hashes for easier sorting while you build your request.

Step 2: Install Unirest:

gem install unirest

Step 3: Build your app. For this example we’ll create a terminal dictionary app using the API available at!/word.

We’ll start by selecting one of the example queries — how to grab a word’s definition:

Click “Get” and you’ll see the template laid out in JSON format. Enter a word in the “Parameters” section…

…and click “Try it Out!” to get your request URL.

In your Ruby code, set up your Unirest query, replacing the test word with an input variable:

print “Enter a word:”
word = gets.chomp
response = Unirest.get(“{word}/definitions?limit=200&includeRelated=true&useCanonical=false&includeTags=false&api_key=a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5").body

This will return an array of all of the available data; now iterate through the array and return just the definition text (use JSON Formatter to pinpoint the location of the data you need):

response.length.times do |i|
definition = response[i][“text”]
puts “#{i + 1}. #{response[i][“text”]}”
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.