How To Query A Basic API In Ruby (RubyShorts)

 - Webdesign Antwerpen


Querying API's

API stands for application programming interface. Which is basically a fancy way of saying that the creators of the web application you're trying to query have given you partial/limited access to their database. 

This allows you to send get, post, patch, put or delete requests (whatever is allowed) with your code in order to retrieve or alter data. (Note that not every application is publicly accessible and most even require you to send some type of "key/string" with the request to authenticate yourself). The most basic one I could found was the pokemon api! 

Follow the link to try it out;

Most api endpoints return a JSON response but could also serve CSV, XLS, XML or other types of data back. JSON is just the most-widely used. So - let's get started!

require "open-uri" # Allows us to send GET requests and receive the response
require "json" # Allows us to parse the reponse into a JSON object/hash

So now we can do

response = open("").read
=> {"forms":[{"url":"http:\/\/\/api\/v2\/pokemon-form\/65\/","name":"alakazam"}],"abilities":[{"slot":3,"is_hidden":true,"ability":{"url":"http:\/\/\/api\/v2\/ability\/98\/","name":"magic-guard"}},{"slot":2,"is_hidden":false,"ability":{"url":"http:\/\/\/api\/v2\/ability\/39\/","name":"inner-focus"}},{"slot":1,"is_hidden":false,"ability"...}

So now we have a JSON string we can parse into a JSON object;

json = JSON.parse(response)
=> {"forms"=>
  [{"url"=>"", "name"=>"alakazam"}],
     {"url"=>"", "name"=>"magic-guard"}},
     {"url"=>"", "name"=>"inner-focus"}},
     {"url"=>"", "name"=>"synchronize"}}],
  [{"stat"=>{"url"=>"", "name"=>"speed"},
     {"url"=>"", "name"=>"special-defense"},

=> Hash

And now we can basically treat it like any other ruby object, let's say I wanted to retrieve all moves that alakazam can learn as he levels up, I can do;

json["moves"].collect {|move| move["move"]["name"]}
=> ["mega-punch",
 "psychic", # THIS is probably the strongest move of alakazam. ALWAYS use your pp-ups on this!

Pretty cool huh!


Related Articles

 - Webdesign Antwerpen

How to Upload Subscribers to Mailchimp Using CSV File (RubyShorts)

Ever wanted to bulk upload users to your mailchimp account but were hindered because of the omnivore alert? Well with some magical ruby code and an API-key you won't have any problems :)

 - Webdesign Antwerpen

How To Install OneNote On Ubuntu (2017)

Do you love keeping your notes in OneNote, made by Microsoft and can't really live without it? Well in this video I'm going to show you how you can set it up on your linux ubuntu device.

 - Webdesign Antwerpen

How To Create An Automatic Sitemap For Your Rails App On Heroku (RailsShorts)

Wish your sitemap was automatically updated once a week or faster without having to manually update it and push the changes to your server? Combine sitemap generator & fog to fix this!

 - Webdesign Antwerpen

How To Do Basic CSV Manipulations In Ruby (RubyShorts)

Need some basic stuff done on your CSV like creating, reading, writing or appending? Here's a short overview!

 - Webdesign Antwerpen

How To Handle Errors In Ruby With Begin, Rescue & Ensure (RubyShorts)

Are your trying to catch some errors in your ruby application but can't really wrap your head around the begin, rescue and ensure blocks in ruby? Here are some pointers!

 - Webdesign Antwerpen

23 VSCode Extensions Every Web Developer Needs

Looking for the best code editor extensions to optimize your workflow? I got you covered. Here are all the ones you should be using in vscode!