Code Monkey home page Code Monkey logo

resourcey's Introduction

Resourcey

Gem Version Master build status MIT License

A lightweight rails gem for building resource-based APIs. It gives you some conventions and convenient solutions for rendering resources in JSON format.

Table of contents

Requirements

Resourcey requires ruby 2.2.2 or higher.

Installation

Add Resourcey to your Gemfile:

gem 'resourcey'

And then:

bundle

Or install it manually:

gem install resourcey

Usage

Controller

For a resource called user, just create UsersController:

class Api::V1::UsersController < Resourcey::Controller
end

For further reading, click here.

Serialization

Now you need a serializer, let's assume that User has an email and an ID, go with this:

class UserSerializer < Resourcey::Serializer
  attributes :id, :email
end

For further reading, click here.

Fetch the data

Don't forget to create a correct route in your routes.rb file, and you're good to go! Now just visit /api/v1/users, and see how your resources are rendered.

/* example response */

[
  { "id": 1, "email": "[email protected]" },
  { "id": 2, "email": "[email protected]" }
]

Pagination

To paginate resources, simply invoke paginate method in controller, like this:

class ResourcesController < Resourcey::Controller
  paginate
end

This will use default :paged paginator. Now you need to pass page and per_page parameters in pagination parameter value, like this:

http://api.example.com/resources?page=3&per_page=10

This will fetch page 3, with 10 resources per single page. That's all! Pagination can be configured globally (see "Configuration" below). Also you can configure every controller's pagination.

For further reading, click here.

Filtering

You can filter your resources using filter objects defined per single resource model. See below for an example:

class UserFilter < Resourcey::Filter
  filter :older_than do |value, scope|
    scope.where('users.age > ?', value)
  end
end

For further reading, click here.

Configuration

Create configuration file in your config/initializers folder, and configure as usual:

Resourcey.configure do |config|
  config.some_config_variable = :some_value
end

Available config variables

config var description default
default_paginator Name of paginator that will be used in every controller, if not configured on controller-level. Click here for details. :paged
controller_parent Class or class name of controller, that Resourcey::Controller will inherit from. ActionController::Base

Contributing

If you want to take part in developing resourcey, fork this repository, commit your code, and create pull request.

Requirements

  • ruby 2.2.2 or higher
  • bundler gem

Running local tests

  • rspec

resourcey's People

Contributors

polakowski avatar

Stargazers

Trevor Owens avatar Druid avatar Tom avatar Dave avatar Maciej Kwaśniak avatar Tomasz Noworyta avatar Tomasz Bak avatar Dawid Pośliński avatar Błażej Kosmowski avatar Michał Staśkiewicz avatar Wojtek Wrona avatar Pawel Kuwik avatar Robert Harężlak avatar Krzysztof Golasik avatar  avatar Michał Kantor avatar Mateusz Wieczorek avatar Joanna Kolonko avatar Łukasz Lapczyk avatar Tomasz Czana avatar

Watchers

James Cloos avatar

resourcey's Issues

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.