Class: Praxis::RoutingConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/routing_config.rb

Constant Summary collapse

ABSOLUTE_PATH_REGEX =
%r|^//|

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version: 'n/a'.freeze, base: '', prefix: [], &block) ⇒ RoutingConfig

Returns a new instance of RoutingConfig.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/praxis/routing_config.rb', line 8

def initialize(version:'n/a'.freeze, base: '', prefix:[], &block)
  @version = version
  @base = base
  @prefix_segments = Array(prefix)

  @routes = []

  if block_given?
    instance_eval(&block)
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



6
7
8
# File 'lib/praxis/routing_config.rb', line 6

def base
  @base
end

#routesObject (readonly)

Returns the value of attribute routes.



4
5
6
# File 'lib/praxis/routing_config.rb', line 4

def routes
  @routes
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/praxis/routing_config.rb', line 5

def version
  @version
end

Instance Method Details

#add_route(verb, path, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/praxis/routing_config.rb', line 50

def add_route(verb, path, options={})
  unless path =~ ABSOLUTE_PATH_REGEX
    path = prefix + path
  end
  prefixed_path = path.gsub('//','/')
  path = (base + path).gsub('//','/')
  # Reject our own options
  route_name = options.delete(:name);
  pattern = Mustermann.new(path, {ignore_unknown_options: true}.merge( options ))
  route = Route.new(verb, pattern, version, name: route_name, prefixed_path: prefixed_path, **options)
  @routes << route
  route
end

#any(path, opts = {}) ⇒ Object



46
# File 'lib/praxis/routing_config.rb', line 46

def any(path, opts={})     add_route 'ANY',     path, opts end

#clear!Object



20
21
22
# File 'lib/praxis/routing_config.rb', line 20

def clear!
  @prefix_segments = []
end

#connect(path, opts = {}) ⇒ Object



44
# File 'lib/praxis/routing_config.rb', line 44

def connect(path, opts={}) add_route 'CONNECT', path, opts end

#delete(path, opts = {}) ⇒ Object



42
# File 'lib/praxis/routing_config.rb', line 42

def delete(path, opts={})  add_route 'DELETE',  path, opts end

#get(path, opts = {}) ⇒ Object



38
# File 'lib/praxis/routing_config.rb', line 38

def get(path, opts={})     add_route 'GET',     path, opts end

#head(path, opts = {}) ⇒ Object



39
# File 'lib/praxis/routing_config.rb', line 39

def head(path, opts={})    add_route 'HEAD',    path, opts end

#options(path, opts = {}) ⇒ Object



37
# File 'lib/praxis/routing_config.rb', line 37

def options(path, opts={}) add_route 'OPTIONS', path, opts end

#patch(path, opts = {}) ⇒ Object



45
# File 'lib/praxis/routing_config.rb', line 45

def patch(path, opts={})   add_route 'PATCH',   path, opts end

#post(path, opts = {}) ⇒ Object



40
# File 'lib/praxis/routing_config.rb', line 40

def post(path, opts={})    add_route 'POST',    path, opts end

#prefix(prefix = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/praxis/routing_config.rb', line 24

def prefix(prefix=nil)
  return @prefix_segments.join.gsub('//','/') if prefix.nil?

  case prefix
  when ''
    @prefix_segments = []
  when ABSOLUTE_PATH_REGEX
    @prefix_segments = Array(prefix[1..-1])
  else
    @prefix_segments << prefix
  end
end

#put(path, opts = {}) ⇒ Object



41
# File 'lib/praxis/routing_config.rb', line 41

def put(path, opts={})     add_route 'PUT',     path, opts end

#trace(path, opts = {}) ⇒ Object



43
# File 'lib/praxis/routing_config.rb', line 43

def trace(path, opts={})   add_route 'TRACE',   path, opts end