Module: Flon::DSL

Defined in:
lib/flon/dsl.rb

Overview

This module holds Flon’s DSL. You should extend it in your class.

Examples:

class BreadAPI
  extend Flon::DSL

  get '/bread'
  def bread
    'bread.'
  end
end

Routing collapse

Instance Method Summary collapse

Instance Method Details

#delete(path) ⇒ void

This method returns an undefined value.

Creates a DELETE route from path, bound to the next defined method.

Parameters:

  • path (String)

    the path to bind to



40
41
42
43
44
# File 'lib/flon/dsl.rb', line 40

%i[get post put delete patch].each do |http_method|
  define_method(http_method) do |path|
    @current_route = [http_method, namespaces.join + path]
  end
end

#get(path) ⇒ void

This method returns an undefined value.

Creates a GET route from path, bound to the next defined method.

Parameters:

  • path (String)

    the path to bind to



40
41
42
43
44
# File 'lib/flon/dsl.rb', line 40

%i[get post put delete patch].each do |http_method|
  define_method(http_method) do |path|
    @current_route = [http_method, namespaces.join + path]
  end
end

#method_added(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



81
82
83
84
85
86
87
88
# File 'lib/flon/dsl.rb', line 81

def method_added(name)
  return unless @current_route

  http_method, path = @current_route
  router.add_route(http_method, path, instance_method(name))

  @current_route = nil
end

#namespace(path) ⇒ void Also known as: version

This method returns an undefined value.

Creates a namespace of the given path. If a block is given, the namespace is local to the block. If not, it is applied to the rest of the API. You may not call this method without a block twice.

Parameters:

  • path (String)

    the path to use



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/flon/dsl.rb', line 52

def namespace(path)
  if block_given?
    namespaces.push(path)
    yield
    namespaces.pop
  else
    unless namespaces.empty?
      raise Flon::Error, 'cannot declare a nested global namespace or declare a global namespace twice'
    end

    namespaces.push(path)
  end
end

#namespacesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



76
77
78
# File 'lib/flon/dsl.rb', line 76

def namespaces
  @namespaces ||= []
end

#patch(path) ⇒ void

This method returns an undefined value.

Creates a PATCH route from path, bound to the next defined method.

Parameters:

  • path (String)

    the path to bind to



40
41
42
43
44
# File 'lib/flon/dsl.rb', line 40

%i[get post put delete patch].each do |http_method|
  define_method(http_method) do |path|
    @current_route = [http_method, namespaces.join + path]
  end
end

#post(path) ⇒ void

This method returns an undefined value.

Creates a POST route from path, bound to the next defined method.

Parameters:

  • path (String)

    the path to bind to



40
41
42
43
44
# File 'lib/flon/dsl.rb', line 40

%i[get post put delete patch].each do |http_method|
  define_method(http_method) do |path|
    @current_route = [http_method, namespaces.join + path]
  end
end

#put(path) ⇒ void

This method returns an undefined value.

Creates a PUT route from path, bound to the next defined method.

Parameters:

  • path (String)

    the path to bind to



40
41
42
43
44
# File 'lib/flon/dsl.rb', line 40

%i[get post put delete patch].each do |http_method|
  define_method(http_method) do |path|
    @current_route = [http_method, namespaces.join + path]
  end
end

#routerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



71
72
73
# File 'lib/flon/dsl.rb', line 71

def router
  @router ||= Flon::Router.new
end