Kawaii
I would like to share new web API framework based on Rack.
Installation
Add this line to your application's Gemfile:
gem 'kawaii-api'
And then execute:
$ bundle
Or install it yourself as:
$ gem install kawaii-api
Creating new project
To create new project structure execute:
$ kawaii-api new app_name
And then execute:
$ cd app_name
$ bundle install
$ rackup
Defining routes
You can define routes in many ways. All routes have to be defined in class which interhits from Kawaii::Routing::Routes
class. This is why you can easily split your routes into many files.
Support HTTP methods
class Routes < Kawaii::Routing::Routes
#you can specify mapping to controller class
get '/kawaii', "controller#method"
post '/kawaii', "controller#method"
put '/kawaii', "controller#method"
delete '/kawaii', "controller#method"
#or define block as a route handler
get '/kawaii' do |params, request|
{message: 'Hello'}
end
post '/kawaii' do |params, request|
{message: 'Hello'}
end
put '/kawaii' do |params, request|
{message: 'Hello'}
end
delete '/kawaii' do |params, request|
{message: 'Hello'}
end
end
REST Resources
class Routes < Kawaii::Routing::Routes
resource :car
resources :house
end
HTTP_METHOD | PATH | MAPPING |
---|---|---|
:GET | /car | car#index |
:GET | /car/new | car#new |
:POST | /car | car#create |
:GET | /car/edit | car#edit |
:PUT | /car | car#update |
:DELETE | /car | car#destroy |
class Routes < Kawaii::Routing::Routes
resources :car
end
HTTP_METHOD | PATH | MAPPING |
---|---|---|
:GET | /car | car#index |
:GET | /car/new | car#new |
:GET | /car/:id | car#show |
:POST | /car | car#create |
:GET | /car/:id/edit | car#edit |
:PUT | /car/:id | car#update |
:DELETE | /car/:id | car#destroy |
####Nested resources
class Routes < Kawaii::Routing::Routes
resources :cars do
resource :wheel
end
resource :human do
resources :legs
end
end
####Additional options You can specify which methods do you want to generate.
class Routes < Kawaii::Routing::Routes
resources :cars, [:index, :edit]
resource :wheel, [:index]
end
Namespaced routes
class Routes < Kawaii::Routing::Routes
namespace :car do
resources :wheel
get :wheel, "controller#method"
end
end
Creating Controllers
App Controllers should be placed in app/ directory. Every controller class must interhits from Kawaii:Controller
class.
Let create resource routes.
class Routes < Kawaii::Routing::Routes
resource :car
end
Then in app/car_controller.rb
:
class CarController < Kawaii::Controller
def index
end
def new
end
def create
end
def edit
end
def update
end
def destroy
end
end
Using Rack middlewares
class ModifyResponse
def initialize(app)
@app = app
end
attr_reader :app
def call(env)
status, headers, response = app.call(env)
response[0] = {message: 'Modified hello'}.to_json
[status, headers, response]
end
end
class Application < Kawaii::App
use ModifyResponse
end
Defining custom 404 handler
class Application < Kawaii::App
route_not_found do
[404, {Rack::CONTENT_TYPE => "application/json"}, [{ message: 'Route not exists'}.to_json]]
end
end
Contributing
- Fork it ( https://github.com/[my-github-username]/kawaii/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request