Project

hoc_utils

0.0
No commit activity in last 3 years
No release in over 3 years
Collection of utilities for the House of Code API project
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16.a
~> 10.0
 Project Readme

HocUtils

This is a collection of utilities for a HoC API application

Installation

Add this line to your application's Gemfile:

gem 'hoc_utils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hoc_utils

Error handling

In the rescue controller you'll need to add this

rescue_from HocUtils::ApiException, with: :handle_exception

# ...

def handle_exception(error)
    render json: { error: error.message }, status: error.http_code
end

Raise exception

No Parameters

When you want to raise an exception you'll need to raise inside a controller

class SessionsController < ApplicationController
    def login
        # Login logic
        raise HocUtils::ApiException::LoginWrongPassword unless user.authenticate(password)
    end
end

Messages and status codes

Messages and status code are placed inside the config/locals/errors.en.yml or just config/locals/en.yml and would look like the following:

en:
    error:
        login_wrong_password:
            code: 1000
            http_code: 401
            message: "Username or password is not correct"

With parameters

class SessionsController < ApplicationController
    def login
        # Parameter check
        unless params.include?([:username, :password])
        raise HocUtils::ApiException::MissingParameter(parameter: "username and password") unless user.authenticate(password)
    end
end

config/locals/en.yml:

en:
    error:
        missing_parameter:
            code: 1001
            http_code: 400
            message: "Parameter %{parameter} is missing from body"

TODO

  • Routes for nested resources
  • Generate client code
  • DSL for specifying models and layout of admin pages

License

The gem is available as open source under the terms of the MIT License.