Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Crude is a clever JavaScript library for working with RESTful services, inspired by the Rails routing engine.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 crude-min.js
Octocat-spinner-32 crude.js
Octocat-spinner-32 package.json
README.md

Crude

Crude is a clever JavaScript library from CloudMade that allows you to create human-readable APIs to RESTful services, inspired by the Rails routing engine.

Crude takes away all the cruft from REST-related code so that you can focus on the actual data instead of remembering what URL schemas, HTTP methods and parameters to use. It also makes your code more bulletproof, as you won't have to change the code throughout your whole project in case something on the server changes.

Initially a part of a closed-source client code for an upcoming CloudMade service, it's now released under an open source BSD-type license. It's designed to work both as a browser library and a CommonJS module for server-side platforms like Node.js. It also has a complete Jasmine-powered test coverage. Enjoy!

Basic usage example

Defining the API

var myApi = Crude.api('', 'js', function(url, method, data) {
    // do requests with jQuery as an example
    return $.ajax({url: url, type: method, data: data});
});

// this is where the magic happens
myApi.resources('post');
myApi.resources('comment').belongTo(MyApi.posts);
myApi.postComments.memberAction('add', {method: 'put'});

Using the API

myApi.posts.get(56).success(showPost);
// GET /posts/56.json, call showPost on response

myApi.posts.get({order: 'date'});
// GET /posts.json?order=date

myApi.comments.inPost(123).create({author: 'Vladimir', message: 'Hi!'});
// POST /posts/123/comments.json, comment[author]=Vladimir&comment[message]=Hi!

myApi.comments.inPost(123).add(345).error(handleError);
// PUT /posts/123/comments/345/add.json, call handleError if not successful

myApi.request('some/{foo}/url', 'get', {foo: 'custom', baz: 5});
// GET /some/custom/url.json?baz=5
Something went wrong with that request. Please try again.