Project

repia

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
It is a collection of basic features required to build RESTful API in Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 5.0.0
~> 2.1.5
 Project Readme

repia

Build Status Coverage Status Code Climate Gem Version

Rails Essential Plug-in for API (or repia) is a Rails plugin that serves as a collection of features that are useful for RESTful API development.

Install

Add gem 'repia' to your project Gemfile.

How to Use Repia

Edit application_controller.rb as the following:

class ApplicationController < Repia::Controller::Base
end

This will allow all controllers in the project to inherit from Repia::BaseController, which gracefully handles all code errors using pre-defined HTTP errors.

Next, update all models that need UUID as a primary identifier:

class Something < ActiveRecord::Base
  include Repia::Support::UUIDModel
end

This will trigger UUID generation before a record object is created. Note that migration must look similar to this:

class CreateSomethings < ActiveRecord::Migration[5.0]
  def change
    create_table :somethings, id: false do |t|
      t.string :uuid, primary_key: true, null: false
      t.string :name
      t.timestamps null: false
    end
  end
end

Middleware

When developing a JSON API, it is annoying to see non-JSON error responses from a middleware. There are two prominent cases: 405 Method Not Allowed and 404 Not Found. The former occurs when the request method is invalid and the latter happens when the route does not match any controller or action. Since these are caught within middleware, rescue_from does not really help. So repia provides two useful components for this.

Method Not Allowed

This class is a middleware that can be inserted (preferrably after ActionDispatch::RequestId) to catch 405 errors. Instead of using a view template, it will return a simple JSON response with a status of 405.

Routing Error

Routing errors can be caught using Repia::Controller::Base#exceptions_app. To configure it, add this to config/application.rb:

config.exceptions_app = lambda {|env| ApplicationController.action(:exceptions_app).call(env)}

NOTE: To enable this feature in development and in test, config/environments/development.rb and config/environments/test.rb must have

config.consider_all_requests_local = false