Project

wcc-api

0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
Holds common code used in our applications that host APIs and those that consume them.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.10.0
~> 0.9
~> 2.15
~> 1.0
~> 12.3
~> 3.3
= 0.69.0
~> 1.3
~> 3.0

Runtime

 Project Readme

WCC::API

This library holds common code used in our applications that host APIs.

Installation

Add this line to your application's Gemfile:

gem 'wcc-api'

And then execute:

$ bundle

Or install it yourself as:

$ gem install wcc-api

Usage

WCC::API::BaseQuery

This provides an abstraction for building queries within an API for an ActiveRecord backed model. You must override the default_scope method to return a scope for the model you are querying on. The query object has the following:

  • call - calls each of the subsequent methods in order
  • paged - adds the paging elements to the query (e.g. limit and offset)
  • ordered - hook to add ordering (default does nothing)
  • filtered - hook to add filtering (default does nothing)

The Base implementation mostly just handles the paging and provides hooks to do other useful things. Here is an example implementation of a query object for a generic Tag model.

class TagQuery < WCC::API::BaseQuery
  def default_scope
    Tag.all
  end

  def ordered(scope=self.scope)
    scope.ordered
  end

  def filtered(scope=self.scope)
    Array(filter[:name_like]).each do |query|
      scope = scope.name_like(query)
    end
    scope
  end
end

Pagination template

To use the common pagination object template include the view helper in the controllers that will need it:

class APIController < ApplicationController
  helper WCC::API::ViewHelpers
end

Now within your jbuilder templates you can do the following to include a Pagination object:

json.pagination api_pagination_for(query: @query)

Where @query conforms to the interface specified by WCC::API::BaseQuery.

Contributing

  1. Fork it ( https://github.com/[my-github-username]/wcc-api/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request