How to create a custom API connector in Ruby

In the How to use the httparty Ruby gem section, we walked through the httparty Ruby gem segment and we discussed 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 here 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 created a class called StackExchange. As the first step, we included the HTTParty module so that this class can access the methods provided by the gem. Next, we set base_uri, which is the Stack Exchange API URI's path.

Since this is a 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. Then, inside of query, we'll pass in service and page that we want to be 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 section, 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, we passed a string as 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 passed in the query from options.

Next, we 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 passed two arguments because our initialize method takes two parameters. The first is a service called stackoverflow, and we can find information about services in the API's documentation. The second parameter is the page, and we wanted results from only the first page, so we passed in 1.

Next, we called the questions method.

If you execute this code, the output will be as follows:

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 check whether 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, you will 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 whether 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 links, 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.65