Giter Club home page Giter Club logo

api-sandbox's Introduction

API Sandbox jQuery Plugin

API Sandbox is a jQuery plugin written in CoffeeScript that allows web apps to easily implement sandbox environments for an API explorer. The plugin includes two parts: apiSandbox, which aids in the creation of inline sandboxes for individual API paths, and APIExplorer, which is a full API explorer solution, much like Facebook's.

Dependencies

API Explorer's dependencies:

API Sandbox's dependencies:

I'd be happy to accept pull requests for versions of API Sandbox without these dependencies.

Usage

###API Explorer

The plugin takes one argument, which is a relative URL to a server path that returns a hash describing the API for a web app. API Explorer accepts a hash with two keys, one with a string for the current version of the API, e.g. "v1", and one with an array of Grape route objects.

$.APIExplorer("describe_api")

For example, a Rails controller could be configured as follows:

# GET /api/:version/describe_api
def describe_api
  api_hash = {}
  api_hash['version'] = params[:version]
  api_hash['routes'] = Api::routes
  render json: api_hash
end

API Explorer adds functionality to a template, which has been provided. As long as the skeleton of the template remains with the proper div elements still intact, you can modify this template and the accompanying CSS as much as you like.

###API Sandbox

The plugin takes two arguments, one for an HTTP method, and another for the API path. The plugin parses the URL and creates editable fields on the page for each URL parameter so the API path can be tested. Note that if the page has multiple sandboxes, each sandbox must be in its own unique div element. For example:

$("#user").sandbox_for("get","/api/v1/users?user_id=")

This would place an API sandbox with one field for user_id in a div element with an id of user on the page.

API Sandbox recognizes URL parameters in three forms: :symbols, ?first_param=, and &subsequent_params=. Here's an example with multiple types of params in one URL string:

$("#user").sandbox_for("get","/api/v1/:model/search?term=&results_per_page=&page=")

This would result in a form with four fields, one for model, one for term, one for results_per_page, and one for page.

API Sandbox can be used to easily place interactive sandboxes inline with documentation. See this blog post for information on how to integrate Markdown, Redcarpet 2.0.0, and API Sandbox together to create dynamic docs.

Inspiration

The API Sandbox plugin was inspired by Wordnik's excellent API explorer. To easily create an API for your Ruby on Rails web app, use Grape. This plugin was tested on an API using Grape.

TODO

  • Add support for the entry of multiple values for array params (e.g. ?names[]=). Currently, API Sandbox correctly sends array params as arrays, but there isn't a clean interface to add multiple values to an array param all in the same box. If you need to enter multiple values, then just put multiple array params in the path, e.g. api/v1/users?names[]=names[]=names[]=.
  • Add more configuration options to APIExplorer

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.