Hyperclient is a Ruby Hypermedia API client written in Ruby.
Hyperclient API documentation on rdoc.info
Example API client:
api = Hyperclient.new('http://myapp.com/api').tap do |api|
api.digest_auth('user', 'password')
api.headers.update('accept-encoding' => 'deflate, gzip')
endBy default, Hyperclient adds application/json as Content-Type and Accept headers. It will also sent requests as JSON and parse JSON responses.
Hyperclient only works with JSON HAL friendly APIs. Learn about JSON HAL.
Hyperclient will try to fetch and discover the resources from your API.
Accessing the links for a given resource is quite straightforward:
api._links.posts_categories
# => #<Resource ...>Or omit _links, which will look for a link called "posts_categories" by default:
api.posts_categories
# => #<Resource ...>You can also iterate between all the links:
api._links.each do |name, link|
puts name, link._url
endActually, you can call any Enumerable method :D
If a Resource doesn't have friendly name you can always access it as a Hash:
api._links['http://myapi.org/rels/post_categories']Accessing embedded resources is similar to accessing links:
api._embedded.postsOr omit the _embedded keyword. By default Hyperclient will look for a "posts" link, then for an embedded "posts" collection:
api.postsAnd you can also iterate between them:
api.embedded.each do |name, resource|
puts name, resource.attributes
endYou can even chain different calls (this also applies for links):
api.embedded.posts.first.links.authorOr omit the navigational structures:
api.posts.first.authorIf you have a named link that retrieves an embedded collection of the same name, you can collapse the nested reference. The following statements produce identical results:
api.links.posts.embedded.posts.firstapi.posts.firstNot only you might have links and embedded resources in a Resource, but also its attributes:
api.embedded.posts.first.attributes
# => {title: 'Linting the hell out of your Ruby classes with Pelusa',
teaser: 'Gain new insights about your code thanks to static analysis',
body: '...' }You can access the attribute values via attribute methods, or as a hash:
api.posts.first.title
# => 'Linting the hell out of your Ruby classes with Pelusa'
api.embedded.posts.first.attributes.title
# => 'Linting the hell out of your Ruby classes with Pelusa'
api.embedded.posts.first.attributes['title']
# => 'Linting the hell out of your Ruby classes with Pelusa'
api.embedded.posts.first.attributes.fetch('title')
# => 'Linting the hell out of your Ruby classes with Pelusa'OK, navigating an API is really cool, but you may want to actually do something with it, right?
Hyperclient uses Faraday under the hood to perform HTTP calls. You can call any valid HTTP method on any Resource:
post = api._embedded.posts.first
post._get
post._head
post._put(title: 'New title')
post._patch(title: 'New title')
post._delete
post._options
posts = api._links.posts
posts._post(title: "I'm a blogger!", body: 'Wohoo!!')If you have a templated link you can expand it like so:
api._links.post._expand(id: 3).first
# => #<Resource ...>You can omit the "_expand" keyword.
api.post(id: 3).first
# => #<Resource ...>You can access the Faraday connection (to add middlewares or do whatever you want) by calling connection on the entry point. As an example, you could use the faraday-http-cache-middleware:
api.connection.use :http_cacheThere's also a PHP library named HyperClient, if that's what you were looking for :)
- Resource permissions: Using the
Allowheader Hyperclient should be able to restrict the allowed method on a givenResource. - Curie syntax support for links (see http://tools.ietf.org/html/draft-kelly-json-hal-03#section-8.2)
- Profile support for links
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add specs for it. This is important so we don't break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. If you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull.
-
Send me a pull request. Bonus points for topic branches.
MIT License. Copyright 2012-2014 Codegram Technologies


