Quickly and easily access any REST or REST-like API.
Here is a quick example:
GET /your/api/{param}/call
require 'ruby_http_client'
global_headers = {'Authorization' => 'Basic XXXXXXX' }
client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
client.your.api._(param).call.get
puts response.status_code
puts response.response_body
puts response.response_headersPOST /your/api/{param}/call with headers, query parameters and a request body with versioning.
import 'ruby_http_client'
global_headers = {'Authorization' => 'Basic XXXXXXX' }
client = SendGrid::Client(host: 'base_url', request_headers: global_headers)
query_params = { 'hello' => 0, 'world' => 1 }
request_headers = { 'X-Test' => 'test' }
data = { 'some' => 1, 'awesome' => 2, 'data' => 3}
response = client.your.api._(param).call.post(request_body: data,
query_params: query_params,
request_headers: request_headers)
puts response.status_code
puts response.response_body
puts response.response_headersgem install ruby_http_client
Following is an example using SendGrid. You can get your free account here.
First, update your .env with your SENDGRID_API_KEY and HOST. For this example HOST=https://api.sendgrid.com.
Following is an abridged example, here is the full working code.
require 'ruby_http_client'
SendGrid::Config.new
headers = JSON.parse('
{
"Authorization": "Bearer ' + ENV['SENDGRID_API_KEY'] + '",
"Content-Type": "application/json"
}
')
host = ENV['LOCAL_HOST']
client = SendGrid::Client.new(host: host, request_headers: headers)
# GET Collection
query_params = { 'limit' => 100, 'offset' => 0 }
response = client.version('v3').api_keys.get(query_params: query_params)
# POST
request_body = JSON.parse('
{
"name": "My API Key Ruby Test",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}
')
response = client.version('v3').api_keys.post(request_body: request_body)
api_key_id = JSON.parse(response.response_body)['api_key_id']
# GET Single
response = client.version('v3').api_keys._(api_key_id).get
# PATCH
request_body = JSON.parse('
{
"name": "A New Hope"
}
')
response = client.api_keys._(api_key_id).patch(request_body: request_body)
# PUT
request_body = JSON.parse('
{
"name": "A New Hope",
"scopes": [
"user.profile.read",
"user.profile.update"
]
}
')
response = client.api_keys._(api_key_id).put(request_body: request_body)
# DELETE
response = client.api_keys._(api_key_id).delete[2016.03.17] - We hit version 1!
We encourage contribution to our libraries, please see our CONTRIBUTING guide for details.
We were inspired by the work done on birdy and universalclient.
![SendGrid Logo] (https://assets3.sendgrid.com/mkt/assets/logos_brands/small/sglogo_2015_blue-9c87423c2ff2ff393ebce1ab3bd018a4.png)
ruby-http-client is guided and supported by the SendGrid Developer Experience Team.
ruby-http-client is maintained and funded by SendGrid, Inc. The names and logos for ruby-http-client are trademarks of SendGrid, Inc.