Class: Bluepine::Endpoint
- Inherits:
-
Object
- Object
- Bluepine::Endpoint
- Includes:
- Assertions
- Defined in:
- lib/bluepine/endpoint.rb
Constant Summary collapse
- HTTP_METHODS_WITHOUT_BODY =
See ‘docs/api/endpoint-validations.md`.
i[get head trace]
- HTTP_METHODS_WITH_BODY =
i[post put patch delete]
- HTTP_METHODS =
HTTP_METHODS_WITHOUT_BODY + HTTP_METHODS_WITH_BODY
- DEFAULT_OPTIONS =
{ schema: nil, title: nil, description: nil, }.freeze
Constants included from Assertions
Assertions::Error, Assertions::KeyError, Assertions::SubsetError
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
-
.normalize_name(name) ⇒ Object
Converts ‘/users/:id/friends` to `users_id_friends`.
Instance Method Summary collapse
-
#create_method(verb, action, path: "/", **options) ⇒ Object
Registers http verb method.
-
#initialize(path, options = {}, &block) ⇒ Endpoint
constructor
A new instance of Endpoint.
-
#method(name, resolver: nil) ⇒ Object
Lazily builds params for speicified method.
-
#methods(resolver = nil) ⇒ Object
Lazily builds all params and return methods hash.
-
#params(&block) ⇒ Object
Returns default params.
Methods included from Assertions
#assert, #assert_in, #assert_kind_of, #assert_not, #assert_subset_of, included
Constructor Details
#initialize(path, options = {}, &block) ⇒ Endpoint
Returns a new instance of Endpoint.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/bluepine/endpoint.rb', line 29 def initialize(path, = {}, &block) = DEFAULT_OPTIONS.merge() @schema = [:schema] @path = path @name = normalize_name([:name]) @methods = {} @params = nil @block = block @loaded = false @title = [:title] @description = [:description] end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
27 28 29 |
# File 'lib/bluepine/endpoint.rb', line 27 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/bluepine/endpoint.rb', line 26 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
26 27 28 |
# File 'lib/bluepine/endpoint.rb', line 26 def path @path end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
26 27 28 |
# File 'lib/bluepine/endpoint.rb', line 26 def schema @schema end |
#title ⇒ Object
Returns the value of attribute title.
27 28 29 |
# File 'lib/bluepine/endpoint.rb', line 27 def title @title end |
Class Method Details
.normalize_name(name) ⇒ Object
Converts ‘/users/:id/friends` to `users_id_friends`
15 16 17 |
# File 'lib/bluepine/endpoint.rb', line 15 def normalize_name(name) name.to_s.delete(":").gsub(/(\A\/+|\/+\z)/, '').tr('/', '_').to_sym end |
Instance Method Details
#create_method(verb, action, path: "/", **options) ⇒ Object
Registers http verb method
create_method(:post, :create, path: "/")
78 79 80 81 82 83 |
# File 'lib/bluepine/endpoint.rb', line 78 def create_method(verb, action, path: "/", **) # Automatically adds it self as schema value [:schema] = .fetch(:schema, schema) @methods[action.to_sym] = Bluepine::Endpoints::Method.new(verb, action: action, path: path, **) end |
#method(name, resolver: nil) ⇒ Object
Lazily builds params for speicified method
61 62 63 64 65 66 |
# File 'lib/bluepine/endpoint.rb', line 61 def method(name, resolver: nil) ensure_loaded assert_in @methods, name.to_sym @methods[name.to_sym].tap { |method| method.build_params(params, resolver) } end |
#methods(resolver = nil) ⇒ Object
Lazily builds all params and return methods hash
54 55 56 57 58 |
# File 'lib/bluepine/endpoint.rb', line 54 def methods(resolver = nil) ensure_loaded @methods.each { |name, _| method(name, resolver: resolver) } end |
#params(&block) ⇒ Object
Returns default params
69 70 71 72 73 |
# File 'lib/bluepine/endpoint.rb', line 69 def params(&block) ensure_loaded @params ||= Bluepine::Endpoints::Params.new(:default, schema: schema, built: true, &block || -> {}) end |