Class: Bluepine::Endpoint

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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, options = {}, &block)
  options  = DEFAULT_OPTIONS.merge(options)
  @schema  = options[:schema]
  @path    = path
  @name    = normalize_name(options[:name])
  @methods = {}
  @params  = nil
  @block   = block
  @loaded  = false
  @title   = options[:title]
  @description = options[:description]
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



27
28
29
# File 'lib/bluepine/endpoint.rb', line 27

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/bluepine/endpoint.rb', line 26

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/bluepine/endpoint.rb', line 26

def path
  @path
end

#schemaObject (readonly)

Returns the value of attribute schema.



26
27
28
# File 'lib/bluepine/endpoint.rb', line 26

def schema
  @schema
end

#titleObject

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: "/", **options)
  # Automatically adds it self as schema value
  options[:schema] = options.fetch(:schema, schema)

  @methods[action.to_sym] = Bluepine::Endpoints::Method.new(verb, action: action, path: path, **options)
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