Code Monkey home page Code Monkey logo

grape-3scale's Introduction

grape-3scale

What is grape-3scale?

grape-3scale provides an autogenerated documentation for your grape-API. The generated documentation is 3scale-compliant, meaning it can be used as the spec for 3scale Active Docs. This gem is a fork of grape-swagger (https://github.com/tim-vandecasteele/grape-swagger.git)

Related projects

Installation

grape-3scale is available as a gem, install it simply via the commandline:

gem install grape-3scale

or add to your Gemfile:

gem 'grape-3scale'

Usage

Once you have the gem installed, mount all your different APIs (with Grape::API superclass) on a root node. In the root class definition, also include add_3scale_documentation, this sets up the system and registers the documentation on '/3scale_doc.json'. Setup done, you can restart your local server now.

require 'grape-3scale'

module API
  class Root < Grape::API
    mount API::Cats
    mount API::Dogs
    mount API::Pirates
    add_3scale_documentation
  end
end

To use with Active Docs get the generated spec from http://localhost:3000/3scale_doc.json and upload to your 3scale admin portal either using the form on the api service edit page or via the API:

curl -v -X PUT -F "body=@YOUR_API_JSON_SPEC.json" "https://YOUR_ADMIN_DOMAIN/admin/api/active_docs/ID_OF_THE_SERVICE_SPEC.xml?provider_key=YOUR_PROVIDER_KEY"

Configure

You can pass a hash with some configuration possibilities to add_3scale_documentation, all of these are optional:

  • :mount_path The path were the API documentation is loaded, default '/3scale_doc'
  • :api_version Version of the API that's being exposed
  • :base_path Basepath of the API that's being exposed
  • :markdown Allow markdown in notes, default false
  • :hide_documentation_path Don't show the '/3scale_doc' path in the generated 3scale documentation
  • :default_params A hash of values for paramaters so save to repeatedly documenting the same parameters

3scale Header Parameters

3scale also supports the documentation of parameters passed in the header. Since grape's params[] doesn't return header parameters we can to specify header parameters seperately in a block after the description.

desc "Return super-secret information", {
  headers: {
    "XAuthToken" => {
      description: "Valdates your identity",
      required: true 
    },
    "XOptionalHeader" => {
      description: "Not reallly needed",
      required: false 
    }
  }
}

Passing in default values

We found that to supply good descriptions to function paramaters for 3scale, we were adding the same comments in again and again. To apply a more DRY approach, we allowed the generator to have passed in a hash of default values for parameters. i.e

  default_params = {
          "App-Id" => {
            description: "Identifies application", required: true, threescale_name: 'app_ids', type: "String"
          },
          "App-Key" => {
            description: "Authorizes application", required: true, threescale_name: 'app_keys', type: "String"
          },
          .....
        }
          
  add_3scale_documentation :markdown => true, :hide_documentation_path => true, 
                          :hide_format => true, :default_params=>default_params

This means that later you can simply specifiy a param name, and the description and other params are added from the hash. i.e.

    params do
      requires "App-Id"
      requires "App-Key"
    end

You can override any of the parameters locally if needed, and the defaults only apply if not set

3scale additions

grape-3scale allows you to add an explanation in markdown in the notes field. Which would result in proper formatted markdown in Active Docs.

We're using kramdown for parsing the markdown, specific syntax can be found here.

Be sure to enable markdown in the add_3scale_documentation call: ':markdown => true'

desc "Reserve a virgin in heaven", {
  :notes => <<-NOTE
    Virgins in heaven
    -----------------

    > A virgin doesn't come for free

    If you want to reserve a virgin in heaven, you have to do
    some crazy stuff on earth.

        def do_good
          puts 'help people'
        end

    * _Will go to Heaven:_ Probably
    * _Will go to Hell:_ Probably not
  NOTE
}

You can also make use of the extra 3scale params:

  • :threescale_name The options 3scame param name - useful for app-id and app-key
  • :default This passes in to 3scale as their param: :defaultValue
  • :allowedValues A list of allowed values for a parameter

Finally the gem also automatically uses the 3scale group variable to group opperations by their route name

Contributing to grape-3scale

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2012 Tim Vandecasteele. See LICENSE.txt for further details.

grape-3scale's People

Contributors

tim-vandecasteele avatar dblock avatar edclements avatar idyll avatar cutalion avatar glenn avatar jrhe avatar andyw8 avatar fknappe avatar justfalter avatar

Watchers

 avatar Greg Bock avatar Fawad avatar James Williamson avatar Chris Maximin avatar  avatar Niall Giggins avatar Richard Kettle avatar  avatar Daniel O Farrow avatar Damian Pudlo avatar James Cloos avatar DJ Patterson avatar Gwe Limpalaer avatar António Pacheco avatar Rizwan Haider avatar Antonia H. avatar Carlos avatar Alan Bennett avatar Alin CALINCIUC avatar Luke Ferguson avatar Jakub Adler avatar Terrance Mutizwa avatar  avatar James avatar Bank Lee avatar Nuno Costa avatar Flavio Tulino avatar GoodViber avatar Richard avatar  avatar Nicolas Klein avatar Piotr Siatkowski avatar Sebastian Macarescu avatar Dean Palmer avatar  avatar Adrian Onu avatar Creanga-Damean Alexandru avatar Petru Cioată avatar Andy Wingrave avatar  avatar  avatar  avatar jack fenton avatar rosubogdan avatar Sebastian Vîrlan avatar  avatar Florin Andriiciuc avatar  avatar Alexandru Tornea avatar Victor Jeman avatar Sergiu Merticariu avatar Daniel avatar  avatar Grigore George Mihai avatar Joel avatar Thao Vo 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.