Skip to content
Simple, efficient RESTful framework in Ruby with Redis
Ruby Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
LICENSE
README.md
makefile
squad.gemspec

README.md

Squad

Simple, efficient RESTful framework in Ruby with Redis.

Squad uses Redis to store resources inspired by Ohm, and provides a simple DSL to easily develop APIs.

Getting started

Installation

gem install Squad

Usage

Squad assumes Redis was started with address localhost and port 6379. If you need to connect to a remote server or different port, try:

Squad.settings[:redis] = Redic.new 'redis://10.1.0.1:6379'

Hear's an example that has two resources users and products.

# cat hello_squad.rb
require "squad"

Squad.application do 
  resources :users do
    attribute :name
    attribute :email
   
    collection :posts
  end

  resources :posts do
    attribute :title
    attribute :content

    reference :users
  end
end

All resources have the id attribute built in, you don't need to declare it.

To run it, you can create a config.ru file

# cat config.ru
require 'hello_squad.rb'

run Squad 

Then run rackup.

Now, you already get basic CURD and relation functionality as RESTful API.

GET /users
POST /users
GET /users/:id
PUT /users/:id
DELETE /users/:id
GET /users/:id/posts

Custom action

You can operate the single element in custom action.

require "squad"

Squad.application do
  resources :users do
    attribute :name
    attribute :email

    element :showcase do
      # GET /users/:id/showcase
      show do |params|
        self.email[1..3] = 'xxx' 
      end

      # PUT /users/:id/showcase
      update do |params|
        self.email = params["email"]
        save
      end

      # DELETE /users/:id/showcase
      destory do |params|
        delete if self.email == params["email"]
      end
    end

    bulk :signup do
      # POST /users/signup
      create do |params|
        if params["email"].include?("@gmail.com")
          update_attributes(params)
          save
          created
        else
          bad_request 
        end
      end
    end
  end
end

Index

Index helps you quick lookup elements.

require "squad"

Squad.application do
  resources :users do
    attribute :name
    attribute :email

    # GET /users/?name=travis
    index :name
  end
end

You can have a customize query as well.

require "squad"

Squad.application do
  resources :users do
    attribute :name
    attribute :email

    index :name

    bulk :gmail do
      # There is `all` method can be used here to get all user elements.
      # GET /users/gmail
      show do |params|
        query("name", params["name"]).select |e|
          e.email.include?("@gmail.com") 
        end
      end
    end
  end
end

Processing request

If you need to do something with your request you can (for example, using the request body)

require "squad"

Squad.application do
  resources :users do
    attribute :extra_data

    process_request do |request|
        send(:extra_data=, request.body.read)
        save
    end
  end
end
You can’t perform that action at this time.