Class: Bluepine::Endpoints::Method
- Inherits:
-
Object
- Object
- Bluepine::Endpoints::Method
- Defined in:
- lib/bluepine/endpoints/method.rb
Overview
Represents HTTP method
Constant Summary collapse
- DEFAULT_OPTIONS =
{ as: nil, params: [], exclude: false, schema: nil, status: 200, title: nil, description: nil, validators: [], }.freeze
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#description ⇒ Object
Returns the value of attribute description.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#title ⇒ Object
Returns the value of attribute title.
-
#verb ⇒ Object
readonly
Returns the value of attribute verb.
Instance Method Summary collapse
-
#body? ⇒ Boolean
Does it have request body? (only non GET verb can have request body).
- #build_params(default = {}, resolver = nil) ⇒ Object
- #errors ⇒ Object
-
#initialize(verb, action:, path: "/", **options) ⇒ Method
constructor
A new instance of Method.
- #permit_params(params = {}, target = nil) ⇒ Object
- #valid?(*args) ⇒ Boolean
- #validate(params = {}, resolver = nil) ⇒ Object
Constructor Details
#initialize(verb, action:, path: "/", **options) ⇒ Method
Returns a new instance of Method.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/bluepine/endpoints/method.rb', line 29 def initialize(verb, action:, path: "/", **) @options = DEFAULT_OPTIONS.merge() @verb = verb @path = path @action = action # Create ParamsAttribute instance @params = create_params(action, .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 |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
26 27 28 |
# File 'lib/bluepine/endpoints/method.rb', line 26 def action @action end |
#as ⇒ Object (readonly)
Returns the value of attribute as.
26 27 28 |
# File 'lib/bluepine/endpoints/method.rb', line 26 def as @as end |
#description ⇒ Object
Returns the value of attribute description.
27 28 29 |
# File 'lib/bluepine/endpoints/method.rb', line 27 def description @description end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
26 27 28 |
# File 'lib/bluepine/endpoints/method.rb', line 26 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
26 27 28 |
# File 'lib/bluepine/endpoints/method.rb', line 26 def path @path end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
26 27 28 |
# File 'lib/bluepine/endpoints/method.rb', line 26 def schema @schema end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
26 27 28 |
# File 'lib/bluepine/endpoints/method.rb', line 26 def status @status end |
#title ⇒ Object
Returns the value of attribute title.
27 28 29 |
# File 'lib/bluepine/endpoints/method.rb', line 27 def title @title end |
#verb ⇒ Object (readonly)
Returns the value of attribute verb.
26 27 28 |
# File 'lib/bluepine/endpoints/method.rb', line 26 def verb @verb end |
Instance Method Details
#body? ⇒ Boolean
Does it have request body? (only non GET verb can have request body)
64 65 66 |
# 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) ⇒ Object
68 69 70 71 |
# 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 ⇒ Object
59 60 61 |
# File 'lib/bluepine/endpoints/method.rb', line 59 def errors @result&.errors end |
#permit_params(params = {}, target = nil) ⇒ Object
73 74 75 76 77 |
# 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) ⇒ Boolean
53 54 55 56 57 |
# File 'lib/bluepine/endpoints/method.rb', line 53 def valid?(*args) validate(*args) @result&.errors&.empty? end |
#validate(params = {}, resolver = nil) ⇒ Object
48 49 50 51 |
# 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 |