Travis Badge

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_headers

POST /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_headers

Installation

gem install ruby_http_client

Usage

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

Announcements

[2016.03.17] - We hit version 1!

Roadmap

Milestones

How to Contribute

We encourage contribution to our libraries, please see our CONTRIBUTING guide for details.

Thanks

We were inspired by the work done on birdy and universalclient.

About

SendGrid Logo

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.