Class: Senro::QueryParamsFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/senro/query_params_formatter.rb

Class Method Summary collapse

Class Method Details

.sorting(param) ⇒ String

Format RESTful API’s query params to SQL order clause. +: asc -: desc none(default): asc

And convert camel case attributes to snake case one.

Parameters:

  • param (String)

    format string. e.g. ‘+id,-name`

Returns:

  • (String)

    formated stirng



14
15
16
17
18
19
20
21
22
23
# File 'lib/senro/query_params_formatter.rb', line 14

def self.sorting(param)
  attributes = param.split(',')
  attributes.map do |attr|
    if /^\-/.match(attr).nil?
      "#{attr.strip.gsub(/^\+/, '').underscore} ASC"
    else
      "#{attr.strip.gsub(/^\-/, '').underscore} DESC"
    end
  end.join(', ')
end