Module: Napa::GrapeHelpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/napa/grape_extensions/grape_helpers.rb

Instance Method Summary collapse

Instance Method Details

#permitted_params(options = {}) ⇒ Object



17
18
19
20
# File 'lib/napa/grape_extensions/grape_helpers.rb', line 17

def permitted_params(options = {})
  options = { include_missing: false }.merge(options)
  declared(params, options)
end

#present_error(code, message = '', reasons = {}) ⇒ Object



13
14
15
# File 'lib/napa/grape_extensions/grape_helpers.rb', line 13

def present_error(code, message = '', reasons={})
  Napa::JsonError.new(code, message, reasons)
end

#represent(data, with: nil, **args) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
# File 'lib/napa/grape_extensions/grape_helpers.rb', line 3

def represent(data, with: nil, **args)
  raise ArgumentError.new(":with option is required") if with.nil?

  if data.respond_to?(:to_a)
    return { data: data.map{ |item| with.new(item).to_hash(args) } }
  else
    return { data: with.new(data).to_hash(args)}
  end
end