Git Product home page Git Product logo

hyperclient's Introduction

Hyperclient

Build Status Dependency Status Code Climate

Hyperclient is a Ruby Hypermedia API client written in Ruby.

Documentation

Hyperclient API documentation on rdoc.info

Usage

Example API client:

api = Hyperclient.new('http://myapp.com/api').tap do |api|
  api.digest_auth('user', 'password')
  api.headers.merge({'accept-encoding' => 'deflate, gzip'})
end

By default, Hyperclient adds application/json as Content-Type and Accept headers. It will also sent requests as JSON and parse JSON responses.

More examples

HAL

Hyperclient only works with JSON HAL friendly APIs. Learn about JSON HAL.

Resources

Hyperclient will try to fetch and discover the resources from your API.

Links

Accessing the links for a given resource is quite straightforward:

api.links.posts_categories
# => #<Resource ...>

You can also iterate between all the links:

api.links.each do |name, link|
  puts name, link.url
end

Actually, 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']

Embedded resources

Accessing embedded resources is similar to accessing links:

api.embedded.posts

And you can also iterate between them:

api.embedded.each do |name, resource|
  puts name, resource.attributes
end

You can even chain different calls (this also applies for links):

api.embedded.posts.first.links.author

Attributes

Not 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.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'

HTTP

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 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_cache

Other

There's also a PHP library named HyperClient, if that's what you were looking for :)

TODO

Contributing

  • List of hyperclient contributors

  • 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.

License

MIT License. Copyright 2012 Codegram Technologies

hyperclient's People

Contributors

oriolgual avatar josepjaume avatar langalex avatar divins avatar bkeepers avatar col avatar txus avatar lorenzoplanas avatar mrcasals avatar karlin avatar

Watchers

Ismael Celis avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.