module FetcheableOnApi

FetcheableOnApi provides standardized sorting, filtering and pagination for you API controllers.

Constants

ArgumentError

Supports

NotImplementedError
VERSION

Public Class Methods

configuration() click to toggle source

Global configuration settings for FetcheableOnApi

@example Set default pagination size

FetcheableOnApi.configuration.pagination_default_size = 25

@return [Configuration] The global configuration instance

# File lib/fetcheable_on_api.rb, line 22
def self.configuration
  @configuration ||= Configuration.new
end
configure() { |configuration| ... } click to toggle source

Configure FetcheableOnApi using a block.

@example Set default pagination size

FetcheableOnApi.configure do |config|
  config.pagination_default_size = 25
end

@yield [Configuration] Gives the global instance to the block.

# File lib/fetcheable_on_api.rb, line 34
def self.configure
  yield(configuration)
end
included(klass) click to toggle source

Public class methods

# File lib/fetcheable_on_api.rb, line 47
def self.included(klass)
  klass.class_eval do
    include Filterable
    include Sortable
    include Pageable
  end
end

Protected Instance Methods

apply_fetcheable(collection) click to toggle source

Apply filters, sort and page on a collection.

# File lib/fetcheable_on_api.rb, line 65
def apply_fetcheable(collection)
  collection = apply_filters(collection)
  collection = apply_sort(collection)

  apply_pagination(collection)
end
foa_default_permitted_types() click to toggle source

Types allowed by default.

# File lib/fetcheable_on_api.rb, line 99
def foa_default_permitted_types
  [ActionController::Parameters, Hash]
end
foa_string_to_datetime(string) click to toggle source

Convert string to datetime.

# File lib/fetcheable_on_api.rb, line 104
def foa_string_to_datetime(string)
  DateTime.strptime(string, "%s")
end
foa_valid_parameters!( *keys, foa_permitted_types: foa_default_permitted_types) click to toggle source

Checks if the type of arguments is included in the permitted types

# File lib/fetcheable_on_api.rb, line 73
def foa_valid_parameters!(
                          *keys, foa_permitted_types: foa_default_permitted_types)
  return if foa_valid_params_types(
    *keys,
  foa_permitted_types: foa_permitted_types,
  )

  raise FetcheableOnApi::ArgumentError,
        "Incorrect type #{params.dig(*keys).class} for params #{keys}"
end
foa_valid_params_type(value, type) click to toggle source

Returns true if class is the class of value, or if class is one of the superclasses of value or modules included in value.

# File lib/fetcheable_on_api.rb, line 94
def foa_valid_params_type(value, type)
  value.is_a?(type)
end
foa_valid_params_types( *keys, foa_permitted_types: foa_default_permitted_types) click to toggle source
# File lib/fetcheable_on_api.rb, line 84
def foa_valid_params_types(
                           *keys, foa_permitted_types: foa_default_permitted_types)
  foa_permitted_types.inject(false) do |res, type|
    res || foa_valid_params_type(params.dig(*keys), type)
  end
end