CleanQueryParams

A small, concise DSL for describing query parameters, specifically for sorting, paging and querying on index actions. I aim to make the specs as clear a demonstration as possible, but here's a quick test case as an example:

Given a /users endpoint, you can create a params object like this:

class UserParameters
  include CleanQueryParams

  defaults sort_by:     'last_name',
           sort_dir:    'ASC',
           page_number: 1,
           page_size:   20

  queryable_by :first_name, :last_name, :age
end

it "gives you a query params object with pagination, sorting and an extracted query hash" do

  raw_params = {
    page_number: 2,
    page_size: 15,
    first_name: 'Stu',
    role: 'admin'
  }

  user_params = UserParameters.new(raw_params)

  # specified above as UserParameters default
  expect(user_params.sort_by).to eq 'last_name'

  # sensible global default
  expect(user_params.sort_dir).to eq 'ASC'

  # defined in raw params
  expect(user_params.page_number).to eq 2

  # defined in raw params
  expect(user_params.page_size).to eq 15

  # only key/values specified above in queryable_by
  expect(user_params.query).to eq({ first_name: 'Stu' })
end

I'm planning to write a railtie to make integration with rails controllers seamless.

More examples to follow...

Installation

Add this line to your application's Gemfile:

gem 'clean_query_params'

And then execute:

$ bundle

Or install it yourself as:

$ gem install clean_query_params

Usage

TODO: Write usage instructions here

Contributing

  1. Fork it
  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 new Pull Request