Class: Praxis::RoutingConfig

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

Constant Summary collapse

ABSOLUTE_PATH_REGEX =
%r{^//}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of RoutingConfig.



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

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

  @route = nil

  instance_eval(&block) if block_given?
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#routeObject (readonly)

Returns the value of attribute route.



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

def route
  @route
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



76
77
78
79
80
81
82
# File 'lib/praxis/routing_config.rb', line 76

def add_route(verb, path, options = {})
  path = prefix + path unless path =~ ABSOLUTE_PATH_REGEX
  prefixed_path = path.gsub('//', '/')
  path = (base + path).gsub('//', '/')
  pattern = Mustermann.new(path, **{ ignore_unknown_options: true }.merge(options))
  @route = Route.new(verb, pattern, version, prefixed_path: prefixed_path, **options)
end

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



70
71
72
# File 'lib/praxis/routing_config.rb', line 70

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

#clear!Object



17
18
19
# File 'lib/praxis/routing_config.rb', line 17

def clear!
  @prefix_segments = []
end

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



62
63
64
# File 'lib/praxis/routing_config.rb', line 62

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

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



54
55
56
# File 'lib/praxis/routing_config.rb', line 54

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

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



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

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

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



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

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

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



34
35
36
# File 'lib/praxis/routing_config.rb', line 34

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

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



66
67
68
# File 'lib/praxis/routing_config.rb', line 66

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

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



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

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

#prefix(prefix = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/praxis/routing_config.rb', line 21

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..])
  else
    @prefix_segments << prefix
  end
end

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



50
51
52
# File 'lib/praxis/routing_config.rb', line 50

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

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



58
59
60
# File 'lib/praxis/routing_config.rb', line 58

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