ProductPromotion
Logo

Ruby

made by https://0x3d.site

GitHub - inf0rmer/blanket: A dead simple API wrapper
A dead simple API wrapper. Contribute to inf0rmer/blanket development by creating an account on GitHub.
Visit Site

GitHub - inf0rmer/blanket: A dead simple API wrapper

GitHub - inf0rmer/blanket: A dead simple API wrapper

Blanket

Build Status Coverage Status Code Climate Inline docs

A dead simple API wrapper.

Table of Contents

Installation

Add this line to your application's Gemfile:

gem 'blanket_wrapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install blanket_wrapper

Usage

Quick demo

require 'blanket'

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:

As this magic works using method_missing, you can send slashed uri parts to the wrapper and it will play nicely. This is especially usefull when APIs give you URLs:

github.get('users/inf0rmer/repos')
# or, if you don't wnat to perform the request yet, or have to append more parts to the uri
github.send('users/inf0rmer').repos#.get
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 Body You can make requests with body using the body option:

api = Blanket::wrap("http://api.example.org")
api.messages.post(body: 'Hello')

Request Parameters

Blanket supports appending parameters to your requests:

api.users(55).get(params: {foo: 'bar'})
# => "http://api.example.org/users/55?foo=bar"

You can also set default params for all your requests on initialization:

api = Blanket::wrap("http://api.example.org", params: {access_token: 'my secret token'})

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

Handling Exceptions

Blanket will raise exceptions for HTTP errors encountered while making requests. Exception subclasses are raised for well known errors (404, 500, etc.) but for other status codes a default Blanket::Exception will be raised instead.

begin
  api.thingamajig.get
rescue Blanket::ResourceNotFound => e
  e.code
  # => 404

  e.message
  # => "404: Resource Not Found"

  # The HTTP body, ie. the error message sent by the server
  e.body
  # => "Could not find this resource!"
end

Contributing

  1. Fork it ( https://github.com/inf0rmer/blanket/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory