Code Monkey home page Code Monkey logo

heroics's Introduction

Build Status

Heroics

Ruby HTTP client generator for APIs represented with JSON schema.

Installation

Add this line to your application's Gemfile:

gem 'heroics'

And then execute:

$ bundle

Or install it yourself as:

$ gem install heroics

Usage

Generating a client

Heroics generates an HTTP client from a JSON schema that describes your API. Look at prmd for tooling to help write a JSON schema. When you have a JSON schema prepared you can generate a client for your API:

bin/heroics-generate MyApp schema.json https://api.myapp.com > client.rb

Passing custom headers

If your client needs to pass custom headers with each request these can be specified using -H:

heroics-generate \
  -H "Accept: application/vnd.myapp+json; version=3" \
  MyApp \
  schema.json \
  https://api.myapp.com > client.rb

Pass multiple -H options if you need more than one custom header.

Client-side caching

The generated client sends and caches ETags received from the server. By default, this data is cached in memory and is only used during the lifetime of a single instance. You can specify a directory for cache data:

heroics-generate \
  -c "~/.heroics/myapp" \
  MyApp \
  schema.json \
  https://api.myapp.com > client.rb

~ will automatically be expanded to the user's home directory. Be sure to wrap such paths in quotes to avoid the shell expanding it to the directory you built the client in.

Generating API documentation

The generated client has Yard-compatible docstrings. You can generate documentation using yardoc:

yard doc -m markdown client.rb

This will generate HTML in the docs directory. Note that Yard creates an _index.html page won't be served by Jekyll on GitHub Pages. Add a .nojekyll file to your project to prevent GitHub from passing the content through Jekyll.

Handling failures

The client uses Excon under the hood and raises Excon errors when failures occur.

begin
  client.app.create({'name' => 'example'})
rescue Excon::Errors::Forbidden => error
  puts error
end

Contributing

  1. Fork the repository
  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 new pull request

heroics's People

Contributors

alindeman avatar brandur avatar dane avatar danp avatar geemus avatar jkakar avatar kch avatar kenchan avatar kjc-stripe avatar knightq avatar mfine avatar mmcgrana avatar rramsden avatar strika avatar tmaier avatar

Watchers

 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.