How To Query A Basic API In Ruby (RubyShorts)

 - Webdesign Mechelen

Video

https://www.youtube.com/watch?v=yUkv1pinlmg

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;

http://pokeapi.co/api/v2/pokemon/alakazam/

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("http://pokeapi.co/api/v2/pokemon/alakazam/").read
=> {"forms":[{"url":"http:\/\/pokeapi.co\/api\/v2\/pokemon-form\/65\/","name":"alakazam"}],"abilities":[{"slot":3,"is_hidden":true,"ability":{"url":"http:\/\/pokeapi.co\/api\/v2\/ability\/98\/","name":"magic-guard"}},{"slot":2,"is_hidden":false,"ability":{"url":"http:\/\/pokeapi.co\/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"=>"http://pokeapi.co/api/v2/pokemon-form/65/", "name"=>"alakazam"}],
 "abilities"=>
  [{"slot"=>3,
    "is_hidden"=>true,
    "ability"=>
     {"url"=>"http://pokeapi.co/api/v2/ability/98/", "name"=>"magic-guard"}},
   {"slot"=>2,
    "is_hidden"=>false,
    "ability"=>
     {"url"=>"http://pokeapi.co/api/v2/ability/39/", "name"=>"inner-focus"}},
   {"slot"=>1,
    "is_hidden"=>false,
    "ability"=>
     {"url"=>"http://pokeapi.co/api/v2/ability/28/", "name"=>"synchronize"}}],
 "stats"=>
  [{"stat"=>{"url"=>"http://pokeapi.co/api/v2/stat/6/", "name"=>"speed"},
    "effort"=>0,
    "base_stat"=>120},
   {"stat"=>
     {"url"=>"http://pokeapi.co/api/v2/stat/5/", "name"=>"special-defense"},
    "effort"=>0,
    "base_stat"=>95},...

json.class
=> 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",
 "fire-punch",
 "ice-punch",
 "thunder-punch",
 "mega-kick",
 "headbutt",
 "body-slam",
 "take-down",
 "double-edge",
 "disable",
 "psybeam",
 "hyper-beam",
 "submission",
 "counter",
 "seismic-toss",
 "thunder-wave",
 "dig",
 "toxic",
 "confusion",
 "psychic", # THIS is probably the strongest move of alakazam. ALWAYS use your pp-ups on this!

Pretty cool huh!


Questions/Suggestions?

Related Articles

 - Webdesign Mechelen

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 Mechelen

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 Mechelen

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 Mechelen

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 Mechelen

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!

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 :)

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.

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!

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!

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!