• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 15 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

make your objects REST-friendly

Roy

Roy is a tiny module that aims to make any Ruby object Rack-friendly and provide it with a REST-like interface.

Roy tries to be as less invasive as possible. It provides your objects with a #call method that takes a Rack environment and dispatches to a regular method named after the HTTP method you want to catch.

Tests

You can execute the tests by running rake test. They are written with MiniTest.

Example

class MessageQueue
  include Roy

  roy allow: [:get, :post, :delete]

  def initialize
    @stack = []
  end

  def get(_)
    @stack.inspect
  end

  def post(_)
    roy.halt 403 unless roy.params[:item]
    @stack << roy.params[:item].strip
    get
  end

  def delete(_)
    @stack.shift.inspect
  end
end

Docs

Configuration

The roy class method is mainly used to define access control and method prefix. You can also define your own options. The following example should be self-explanatory enough:

class Example
  include Roy
  roy allow: [:get], prefix: :http_, foo: "bar"

  def http_get(path)
    "foo is #{roy.conf.foo}"
  end
end

Environment

Inside your handler methods, you have access to a roy readable attribute which is an OpenStruct containing at least the following fields:

  • env: the Rack environment
  • response: a Rack::Response object that will be returned by call
  • request: a Rack::Request build from the environment
  • headers: a hash of headers that is part of response
  • params: parameters extracted from the query string and the request body
  • conf: the configuration set via ::roy

The keys for params can be accessed either via a String or a Symbol

Control flow

Your handler methods are run inside a catch block which will catch the :halt symbol. You can then use throw to abort a method but you must return an array composed of a status code and a message.

Roy provides a roy.halt method that takes a status code and an optional message. If there is no message it uses the default message from Rack::Utils::HTTP_STATUS_CODES

Plugins

Various plugins are shipped with Roy, here is the full list:

  • after: modify the response after the app has been called
  • before: modify the environment before calling the app
  • render: integration with Tilt
  • plugins: a simple plugin loader

Each plugin is designed to do only one thing. Thus it is very easy to take a look at the code and see how the plugin works.

More Repositories

1

git-identity

Manage your identity in Git
Shell
113
star
2

moreutils

my clone of the UNIX moreutils
C
107
star
3

haze

A minimalistic blogging engine, the successor of Honk
Ruby
18
star
4

elfi

An elegant state container for your JavaScript applications
JavaScript
17
star
5

sharedrop

Easily share files using your own server
Shell
11
star
6

honk

my blogging engine - sinatra + yaml
Ruby
9
star
7

obsidian-dv-paginate

A Dataview view script to render a paginated collection
JavaScript
8
star
8

veil

Make-based static website builder using Jade and Stylus
CSS
6
star
9

windsock

Generate React components from Tailwind classes
JavaScript
6
star
10

shumblr

A Tumblr client written in Shoes
Ruby
5
star
11

targetz

URL shortening/bookmarking using Sinatra & CouchDB
Ruby
5
star
12

ouiche

a small wiki-like system in Sinatra w/ text files
Ruby
4
star
13

gifcast

Linux GIF screencasting made easy:
Shell
4
star
14

prezto

A fast and simple JavaScript slideshow system
HTML
4
star
15

mdwn.in

[DOWN & DEPRECATED] A Markdown rendering service.
Ruby
4
star
16

propane

My setup scripts
Lua
4
star
17

obsidian-budget-tracker

My Obsidian budget tracker view
JavaScript
4
star
18

brume

a lightweight static website manager on top of git
Shell
3
star
19

solylace

A Shoes text editor
Ruby
3
star
20

shrrred

A Ruby regular expression tester in Shoes, greatly inspired from Rubular
Ruby
3
star
21

shitstorm-old

A minimalist bug tracker written in Ruby with Sinatra and Sequel
Ruby
3
star
22

jumpr

like autojump and friends, in ruby + yaml
Shell
3
star
23

computational_geometry

Source code for my computational geometry course
C
3
star
24

termki

A text-based and REST-oriented Wiki with Sinatra
Ruby
3
star
25

merb-todolist

A small todo list written with Merb
Ruby
3
star
26

dotfiles

My configuration files, managed with chezmoi.
Lua
3
star
27

todo

My command-line todolist manager
3
star
28

jspell

A Java spellchecker (Uni project)
Java
2
star
29

gp2sass

A little script to convert color palettes to Sass constant sets
2
star
30

metash

A Hash that allows method calls to get elements
Ruby
2
star
31

140.ru

A Rack-compliant Ruby framework that fits in a tweet
Ruby
2
star
32

patches

Various patches I made
2
star
33

listr

A directory browser in PHP
2
star
34

xup

A lightweight and modular markup language in pure Ruby
Ruby
2
star
35

ruby-lugdunum

Ruby Conf Lyon 2011
JavaScript
2
star
36

ubiquity-commands

Some homemade Ubiquity commands
JavaScript
2
star
37

rzen

A Zenity frontend in Ruby
Ruby
2
star
38

dicelib

A dice library in Ruby
Ruby
2
star
39

recettes

Mes recettes de cuisine
2
star
40

vim-plugins

My personnal vim plugins and helpers
Vim Script
2
star
41

madx.github.com

My GitHub Page
2
star
42

mtest

A micro test "framework"
Ruby
2
star
43

bm

Quickly jump to bookmarked places in your filesystem.
Shell
2
star
44

rwh

Study exercises from the Real World Haskell book
Haskell
1
star
45

calico

A lightweight markup language
Ruby
1
star
46

compilation_classes

my work for my compilation classes
C
1
star
47

trigger

Launch a command when files are modified
Shell
1
star
48

madx.me

The source and scripts for my personal website
CSS
1
star
49

therm

My terminal emulator (VTE-based)
Vala
1
star
50

wex

A wofi-based text expander and generic executor
Shell
1
star
51

kopay

My own Discord bot/assistant!
JavaScript
1
star
52

koleirs-toolbox

My custom Obsidian utilities
JavaScript
1
star