What is API?
API or Application Programming Interface is a way for developers to use features or collect data from other companies, so that we can use the data or functionality to enhance our own app. Without API we would have to generate our own data to test our application, but with API we can use real world data that is already there. Many API follow the REST ful framework, meaning they follow the conventions of the seven restful routes.
Finding endpoint : Endpoint is the url we follow to get our desired results. It will be different for every api, we need to check the documentation of the api we are using to find the endpoint. For example, to get a list of books on harry potter using google books api, the endpoint would be https://www.googleapis.com/books/v1/volumes?q=harry+potter
If we open the link we will find that the result is in a big nested hash format which we can manipulate to get the specific attribute. This is actually a JSON object that acts like a ruby hash.
Require : We need the following requires at the top of our file, or defined in environment.rb.
The code above, has a class called GetRequester that can be initialized with a url. The url is passed onto an instance variable. The get_response_body method uses the NET::HTTP library to make an HTML request to the url. URI (Universal Resource Identifier) is also a library that we are using to parse the URL into a class object and make use of some URI methods. This method gets the information from the API. Finally the parse_json method parses the data into a readable format. Many API’s will require a unique key that they provide, for those it would be necessary to setup the key in our method as well. Checking documentation for the API should help us in the process.
Having defined this class to get information from API we can go into IRB to experiment with a joke API. This API doesn’t require any key. This api returns a joke based on category or search term, with the options of applying filters.
Once we go to irb, we would need to require_relative ‘lib/get_requester.rb’, the path of my program. I made a new instance of my GetRequester class and I passed in the endpoint url as argument. It searches for jokes containing Harry Potter with certain filters, to keep the joke pg-rated. I am applying my method in the class described above, to get a joke from the api and parse it. I then use the attribute [“setup”] and [“delivery”] to print out the joke on the console.
The joke reads
setup: How did Harry Potter get down the hill?
delivery: “Walking…\nJK, Rolling.”
Using Rest Client instead
Alternatively you could use rest-client to get the data from the api. You would need to install the gem ‘rest-client’, ‘~> 2.1.0’ and require ‘rest-client’ at the top of the file. Here is the same code but by using rest-client
In this code I am instantiating a new object in my ruby file with another endpoint, https://sv443.net/jokeapi/v2/joke/Any?contains=hate and I am running the file directly in my console. You may have noticed, I have added binding.pry to better show what I would get.
This time it is a one-liner
Hopefully this was a good refresher on using API’s and hope you find it useful to integrate api’s to your own project. A list of free public api is in the links.
start(address, port, p_addr, p_port, p_user, p_pass, &block) click to toggle source start(address, port=nil…
OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP. It is possible to open an http, https or ftp…
JokeAPI is a RESTful API that serves jokes from many categories while also offering a lot of filtering methods