class Bluepine::Endpoints::Method

Represents HTTP method

@example Create new POST {Method}

Method.new(:post, action: create, path: "/", schema: :user, as: :list)

@example

Method.new(:post, {
  action: :create,
  path: "/",
  validators: [CustomValidator]
})

Constants

DEFAULT_OPTIONS

Attributes

action[R]
as[R]
description[RW]
params[R]
path[R]
schema[R]
status[R]
title[RW]
verb[R]

Public Class Methods

new(verb, action:, path: "/", **options) click to toggle source
# File lib/bluepine/endpoints/method.rb, line 29
def initialize(verb, action:, path: "/", **options)
  @options = DEFAULT_OPTIONS.merge(options)
  @verb    = verb
  @path    = path
  @action  = action

  # Create ParamsAttribute instance
  @params  = create_params(action, options.slice(:params, :schema, :exclude))
  @schema  = @options[:schema]
  @as      = @options[:as]
  @status  = @options[:status]
  @title   = @options[:title]
  @description = @options[:description]
  @validator   = nil
  @validators  = @options[:validators]
  @resolver    = nil
  @result      = nil
end

Public Instance Methods

body?() click to toggle source

Does it have request body? (only non GET verb can have request body)

# File lib/bluepine/endpoints/method.rb, line 64
def body?
  Bluepine::Endpoint::HTTP_METHODS_WITH_BODY.include?(@verb) && @params.keys.any?
end
build_params(default = {}, resolver = nil) click to toggle source
# File lib/bluepine/endpoints/method.rb, line 68
def build_params(default = {}, resolver = nil)
  @resolver = resolver if resolver
  @params   = @params.build(default, resolver)
end
errors() click to toggle source
# File lib/bluepine/endpoints/method.rb, line 59
def errors
  @result&.errors
end
permit_params(params = {}, target = nil) click to toggle source
# File lib/bluepine/endpoints/method.rb, line 73
def permit_params(params = {}, target = nil)
  return params unless params.respond_to?(:permit)

  params.permit(*@params.permit(params))
end
valid?(*args) click to toggle source
# File lib/bluepine/endpoints/method.rb, line 53
def valid?(*args)
  validate(*args)

  @result&.errors&.empty?
end
validate(params = {}, resolver = nil) click to toggle source
# File lib/bluepine/endpoints/method.rb, line 48
def validate(params = {}, resolver = nil)
  @validator = create_validator(@resolver || resolver)
  @result    = @validator.validate(@params, params, validators: @validators)
end

Private Instance Methods

create_params(action, **options) click to toggle source
# File lib/bluepine/endpoints/method.rb, line 81
def create_params(action, **options)
  Bluepine::Endpoints::Params.new(action, **options)
end
create_validator(*args) click to toggle source
# File lib/bluepine/endpoints/method.rb, line 85
def create_validator(*args)
  Bluepine::Validator.new(*args)
end