Giter VIP home page Giter VIP logo

classy_resources's Introduction

Classy Resources

With a simple, declarative syntax, you can create active_resource compatible REST APIs incredibly quickly.

Think resource_controller, except for Sinatra.

Installation

sudo gem install giraffesoft-classy_resources

Usage

require 'rubygems'
require 'sinatra'
require 'classy_resources'
require 'classy_resources/active_record'
# ... or require 'classy_resources/sequel'
# more ORMs coming (it's also easy to implement your own)...

define_resource :posts, :member     => [:get, :put, :delete],
                        :collection => [:get, :post],
                        :formats    => [:xml, :json, :yaml]

The above declaration will create the five actions specified, each responding to all of the formats listed.

- GET /resources.format      # => index
- POST /resources.format     # => create
- GET /resources/1.format    # => show
- PUT /resources/1.format    # => update
- DELETE /resources/1.format # => destroy

Since ClassyResources was designed to be active resource compatible, the params formats and return values are what AR expects.

Overrides

In the above example, :posts would map to a Post class. If your class is named differently, you just override class_for. For example, if your Post class was stored in a module:

def class_for(resource)
  MyModule.const_get(resource.to_s.singularize.classify.constantize)
end

Or, if you wanted to change how objects were being serialized:

def serialize(object, format)
  MySerializer.new(object, format).to_s
end

Other method signatures you might want to override:

- def load_collection(resource)
- def build_object(resource, params)
- def load_object(resource, id)
- def update_object(object, params) # Note that this doesn't save. It just changes the attributes.
- def destroy_object(object)

Copyright © 2008 James Golick, Daniel Haran, GiraffeSoft Inc.. See LICENSE for details.

classy_resources's People

Contributors

jamesgolick avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.