Coder Social home page Coder Social logo

api_logic's Introduction

API Logic

A lightweight mixin for making API controllers

Usage

Extend a controller with ApiLogic and call exposes_model to generate the default REST actions.

class MyApiController < ApplicationController
  extend ApiLogic
  exposes_model Widget
  respond_to :json, :xml
end

or if the controller's name looks like #{resource}ApiController then exposes_model will automatically be called with the resource:

class CalendarApiController < ApplicationController
  extend ApiLogic
  respond_to :json, :xml
end

Customization

Responder

ApiLogic calls respond_with for each REST action, so you can change how it renders by replacing the Responder:

class CalendarApiController < ApplicationController
  extend ApiLogic
  responder = MyResponder
  respond_to :json, :xml
end

Templates

The recommended way of customizing how a model is rendered to JSON or XML is to supply a RABL template for the action.

Overriding methods

You can always override one of ApiLogic's actions by defining your own, but ApiLogic gives a few finer-grained ways of controlling its default behavior.

You can override the method find_models to change how the index action fetches models. By default find_models looks like this:

def find_models
  model_class.all
end

You can override the method find_model to change how the show, update, and destroy actions look up a model for the given URL. By default find_model looks like this:

def find_model
  @model = model_class.find(params[:id])
  instance_variable_set "@#{model_singular}", @model
end

create_attributes and update_attributes return the attributes supplied to model_class.create and @model.update_attributes in the create and update actions. Their default implementations are:

def create_attributes
  model_params
end

def update_attributes
  model_params
end

And, finally, you can change how a model's attributes are pulled out of params by overriding model_params. Its default implementation looks like this:

def model_params
  params[model_singular]
end

api_logic's People

Contributors

boblail avatar kobsy avatar

Stargazers

 avatar

Watchers

James Cloos avatar Luke Booth avatar Nicholas Weber avatar  avatar Rob 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.