Code Monkey home page Code Monkey logo

api_monkey's Introduction

ApiMonkey is undergoing a major overhaul for version 1.0.0

ApiMonkey

Makes building data-driven APIs in Rails a breeze by providing an easy to use DSL for declaring API resources.

Codeship Status for thebadmonkeydev/api_monkey Code Climate Test Coverage Issue Count

Installation

Add this line to your application's Gemfile:

gem 'api_monkey'

And then execute:

$ bundle

Usage

Once you have ApiMonkey installed, declare your model as an ApiMonkey model:

# app/models/product.rb

class Product < ActiveRecord::Base
  include ApiMonkey
end

Filter Params

Filter params provide a clean and easy way to add request-based filtering of your active record models. To use filter params, ApiMonkey expects the parameters send to rails to resolve like this:

# http://my.domain/products?filter[price]=200

{
  filter: {
    price: 200
  }
}

This would filter our Product query to only those with price = 200

More complex filters can be applied using an operator shorthand that allows for equality and comparison operators. The list of possible operators are:

SQL Operator Operator Shorthand
= eq
> gt
< lt
<= leq
>= geq

To use these, just change the concrete value for price in the above hash to it's own hash using the operators as keys:

# http://my.domain/products?filter[price[gt]]=200

{
  filter: {
    price: {
      'gt' => 200
    }
  }
}

Or:

# http://my.domain/products?filter[price[gt]]=200&filter[price[leq]]=350

{
  filter: {
    price: {
      'gt' => 200,
      'leq' => 350
    }
  }
}

To use ApiMonkey in an exisitng Rails app, you have to include the ApiMonkey module in your model like this:

class Product < ActiveRecord::Base
  include ApiMonkey
end

This will create all the necessary filtering methods that will be used in your controller. Next we want to add the filter to our controller. Use a methodology similar to strong parameters. We recommend using a controller method like this:

# app/controllers/products_controller.rb

class ProductsController < ApplicationController
  def index
    @products = if params[:filter]
                  Product.filter(filter_params)
                else
                  Product.all
  end

  protected

  def filter_params
    params.require(:filter).permit(Product.filter_params)
  end
end

Model.filter_params is a simple hash so it supports methods like include and except to help control the fields that can be filtered against. filter is designed so that passing either nil or {} to it will produce an empty set (where(nil)). Therefore, you should ensure that filter exists as a key in params. Passing nil to one of the filter_xxx will result in a where(xxx: nil).

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/michaelkelly322/api_resource. Guidelines and suggestion can be found in our CONTRIBUTE.md file. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

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.