Blanket
A dead simple API wrapper.
Installation
Add this line to your application's Gemfile:
gem 'blanket'
And then execute:
$ bundle
Or install it yourself as:
$ gem install blanket
Usage
Quick demo
github = Blanket.wrap("https://api.github.com")
# Get some user's info
user = github.users('inf0rmer').get
user.login
# => "inf0rmer"
# Get a user's repos
github.users('inf0rmer').repos.get
# => [{
# "id": 20000073,
# "name": "BAPersistentOperationQueue",
# ...
# }]
How it works
Blanket uses some metaprogramming black magic to wrap an API. Everytime you call a method on a wrapped API, Blanket appends it as a part of the final URL:
github = Blanket.wrap("https://api.github.com")
github.users('inf0rmer').repos.get
Here's how the final URL is built, the step by step:
github = Blanket.wrap("https://api.github.com")
# => "https://api.github.com"
github.users
# => "https://api.github.com/users"
github.users('inf0rmer')
# => "https://api.github.com/users/inf0rmer"
github.users('inf0rmer').repos
# => "https://api.github.com/users/inf0rmer/repos"
The final get method performs a GET HTTP request. You can also use it to append a final part to your request, so you can write something like:
github = Blanket.wrap("https://api.github.com")
github.users.get('inf0rmer')
# => "https://api.github.com/users/inf0rmer"
Responses
At the moment Blanket only accepts JSON responses. Every request returns a Blanket::Response instance, which parses the JSON internally and lets you access keys using dot syntax:
user = github.users('inf0rmer').get
user.login
# => "inf0rmer"
user.url
# => "https://api.github.com/users/inf0rmer"
# It even works on nested keys
repo = github.repos('inf0rmer').get('blanket')
repo.owner.login
# => "inf0rmer"
If the response is an array, all Enumerable methods work as expected:
repos = github.users('inf0rmer').repos.get
repos.map(&:name)
# => ["analytics-ios", "aztec", "fusebox", ...]
Request Parameters
Blanket supports appending parameters to your requests:
api.users(55).get(params: {foo: 'bar'})
# => "http://api.example.org/users/55?foo=bar"
Headers
HTTP Headers are always useful when accessing an API, so Blanket makes it easy for you to specify them, either globally or on a per-request basis:
# All requests will carry the `token` header
api = Blanket::wrap("http://api.example.org", headers: {token: 'my secret token'})
# This single request will carry the `foo` header
api.users(55).get(headers: {foo: 'bar'})
Extensions
Some APIs require you to append an extension to your requests, such as .json or .xml. Blanket supports this use case, letting you define an extension for all your requests or override it for a single one:
# All request URLs are suffixed with ".json"
api = Blanket::wrap("http://api.example.org", extension: :json)
# Requests to "users_endpoint" are suffixed with ".xml" instead
users_endpoint = api.users(55)
users_endpoint.extension = :xml
Contributing
- Fork it ( https://github.com/[my-github-username]/blanket/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request