- Read Tutorial
- Watch Guide Video
In the last lesson we walked through a HTTParty RubyGem guide and we learned how to integrate built in classes from the gem to call an API. If you need something more specific, it's a good idea to create a separate class to manage the API processes. That's what we are going to do in this lesson, while still leveraging some of the key modules supplied by HTTParty.
require 'rubygems' require 'httparty' class StackExchange include HTTParty base_uri 'api.stackexchange.com' end
In this code, we are creating a class called StackExchance
. As the first step we are including the HTTParty
module so that this class can access the methods provided by the gem. Next, we are setting the base_uri
, which is the stackexchange API URI's path.
Since this is our custom class, let's create an initializer that takes in the arguments service
and page
. Inside of the initialize
method we will store our API query inside of the @options
instance variable. Inside of the query we're passing in the service and page that we want returned.
def initialize(service, page) @options = { query: {site: service, page: page}} end
Next, we are going to create some additional custom methods. If you remember the previous lesson, we got a large amount of information when we used the body
method. To be more specific, we are going to create a method called questions
that will simply return the questions from Stack Overflow.
def questions self.class.get('/2.2/questions', @options) end
In this method, the string that we passed is the API endpoint. Every web application's documentation provides this endpoint value for you, so you have to look through it to find out what it is. From there we're passing in the query from our @options
.
Next, we'll have a method called users
that's similar to the questions
method. This will allow us to query the list of users from Stack Overflow.
def users self.class.get('/2.2/users', @options) end
Now that we have all our methods, let's create an instance of our class.
stack_exchange = StackExchange.new('stackoverflow', 1) puts stack_exchange.questions
In this code, we are passing two arguments because our initialize
method takes two parameters. The first is a service called stackoverflow
and we can find this information about services in the API's documentation. The second parameter is the page
, and we want only results from the first page so we pass in 1
.
Next, we are calling the questions
method.
If you execute this code, the output is:
If you look through the results you'll see that the API contains a list of questions and their related URLs. You can copy the link
attribute and paste it in the browser to see if it is working, if you access the URL in the browser you'll see the question returned from the API:
If you go back to the output, we can see other attributes too such as: title
, tags
, owner
, display name
and just about everything else you need to access the stack overflow questions.
Let's test if the users
method is working by updating the call to the following:
stack_exchange = StackExchange.new('stackoverflow', 1) puts stack_exchange.users
If you run the program now the output will bring up all the users.
Note that it brings up the list of users on the first page and includes different attributes such as: gravatar
, account id
, last accessed
date and last modified
date.
So, that's how you create a custom API Connector in Ruby while using the HTTParty
gem.