Sha256: b1b5b9834d1b9f50684860149c4d16bfdf24d81a5335755c029b4a866e8b0153

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module TbCommerce::Api
  class BaseController < TbCommerce::ApplicationController
    layout false

    self.responder = TbCore::Responder
    respond_to :json

    # Handle errors if a required parameter is not passed to the action
    #
    rescue_from ActionController::ParameterMissing, :with => :handle_missing_param_error

    # Handle errors if a record is not found
    #
    rescue_from ActiveRecord::RecordNotFound, :with => :handle_record_not_found_error

private

    def get_limit_param(default:nil)
      return params[:limit].try(:to_i) || default
    end

    def get_page_param
      return params[:page].try(:to_i) || 1
    end

    def paginate_params
      return {
        :page => get_page_param(),
        :per_page => get_limit_param()
      }
    end

    def handle_missing_param_error(error)
      render :json => {:error => error.to_s}, :status => 400
    end

    def handle_record_not_found_error(error)
      error = Spud::NotFoundError.new(:item => 'record')
      handle_request_error(error)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tb_commerce-0.0.4 app/controllers/tb_commerce/api/base_controller.rb
tb_commerce-0.0.3 app/controllers/tb_commerce/api/base_controller.rb
tb_commerce-0.0.2 app/controllers/tb_commerce/api/base_controller.rb