Giter VIP home page Giter VIP logo

grape-client-generator's Introduction

grape-client-generator

Automatically generate clients for your Grape APIs.

Installation

Install the gem as you would any other:

gem install grape-client-generator

or put it in your Gemfile.

The grape-client-generator is probably best used in a Rakefile. Require it like so:

require 'grape/client_generator'

Usage

Let's say we have a little Grape API that looks like this:

class DummyEndpoints < Grape::API

  desc "I'm a little teapot."

  named "dummy"

  get :dummy do
    puts "hello"
  end

end

NOTE: Pay special attention to the call to named above. grape-client-generator expects all endpoints to give themselves a name that will be used as the method name in the generated class.

grape-client-generator lets you automatically generate a (Ruby) consumer for this API that could potentially be packaged up into a gem. The RubyGenerator class takes in an array of Grape::APIs to process as well as a number of additional options like :namespace, :class_name, :default_version, and :response_types. Here's an example:

generator = Grape::ClientGenerator::Ruby::RubyGenerator.new([DummyEndpoints], {
  :namespace => "MyClient",
  :class_name => "Api",
  :default_version => "v1",
  :response_types => [:xml, :json]
})

To generate the source code for your client, use the generate_each method:

generator.generate_each do |file_name, content|
  File.open(File.join("./output", file_name), "w+") do |f|
    f.write(content)
  end
end

Disclaimers

This gem makes quite a few assumptions about your Grape endpoints and is far from complete. For example, it expects each endpoint to have a simple path (i.e. no named arguments) and that you're placing the version in the url (eg. http://mysite.com/v1/...). Have a feature in mind you'd like to see added? Submit a pull request!

Requirements

You'll need to add grape-client-generator into an environment that has access to Grape, although it is not specifically listed as a dependency.

The mustache gem is used to assemble the generated Ruby source files.

If you'd like to use the JsonResponseParser that grape-client-generator can generate, you'll need to add one of the json gems into your environment, usually either json or json-pure.

Running Tests

Run bundle exec rake or bundle exec rspec.

Authors

Links

License

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

grape-client-generator's People

Contributors

camertron avatar

Stargazers

Yao avatar Tomas Jukin avatar  avatar Sho Kusano avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

jasonzhao3

grape-client-generator's Issues

Oi, Cameron

Since I'm not a contributor, I can't make code change :(
Can you update README doc for not suggesting to use set(:name, 'blah') ?
set method is deprecated since grape ver 0.10

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.