DEV Community

Cover image for 49 Days of Ruby: Day 37 -- APIs: Using SDKs
Ben Greenberg
Ben Greenberg

Posted on

49 Days of Ruby: Day 37 -- APIs: Using SDKs

Welcome to day 37 of the 49 Days of Ruby! 🎉

During the past couple of days, we discussed how to get data off the web programmatically. First, we looked at web scraping. Then, we explored making calls to APIs.

The API route is very common, and it is very powerful for all the ways that it lets you customize your criteria.

That customizability though can come at a cost of complexity. This is why for oft-used APIs there are often SDKs that are created.

An SDK, a software development kit, is a library that wraps the various functionality of the API into reusable methods for users to utilize in their applications. This can make building robust applications with APIs a lot easier than custom building your own methods.

Today, we'll take a look at one example from the Vonage Ruby SDK.

Making an API call with an SDK

Perhaps, you wish to send an SMS message using the SMS API. If you want to build the API request by hand it would look like this:

require 'net/http'

url = URI("https://rest.nexmo.com/sms/json")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
req = Net::HTTP::Post.new(url)
req["Accept"] = "application/x-www-form-urlencoded"
req["Content-Type"] = "application/x-www-form-urlencoded"

form_data = URI.encode_www_form(
  {
     :api_key => 'apiKey', 
     :api_secret => 'apiSecret',
     :from => '12222222',
     :to => '1221222222,
     :text => 'Send a message!'
  }
)

req.body = form_data

response = http.request(req)

JSON.parse(response.body)
Enter fullscreen mode Exit fullscreen mode

Now, the same request using the Ruby SDK:

require "vonage"

client = Vonage::Client.new

client.sms.send(from: '1222222', to: '12122222', text: 'Send a message!')
Enter fullscreen mode Exit fullscreen mode

As you can see from that one example, an SDK can save you a lot of time writing code!

Did you know most of the popular services you may have heard of have Ruby SDKs? Do some research today and find out about the Google Cloud, Twilio, IBM Watson, and other Ruby SDKs!

Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.

Top comments (0)