Sha256: d59ef8b80731ba449714a9d6a4bf781cc1e4de0008f95750769829bb7a2534b2

Contents?: true

Size: 809 Bytes

Versions: 3

Compression:

Stored size: 809 Bytes

Contents

module ServiceTemplate
  module GrapeHelpers
    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

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

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

    # extend all endpoints to include this
    Grape::Endpoint.send :include, self if defined?(Grape)
    # rails 4 controller concern
    extend ActiveSupport::Concern if defined?(Rails)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
service_template-0.5.2 lib/service_template/grape_extensions/grape_helpers.rb
service_template-0.5.1 lib/service_template/grape_extensions/grape_helpers.rb
service_template-0.5.0 lib/service_template/grape_extensions/grape_helpers.rb