Class: Otto::RouteDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/otto/route_definition.rb

Overview

Immutable data class representing a complete route definition This encapsulates all aspects of a route: path, target, and options

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verb, path, definition, pattern: nil, keys: nil) ⇒ RouteDefinition

Returns a new instance of RouteDefinition.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/otto/route_definition.rb', line 39

def initialize(verb, path, definition, pattern: nil, keys: nil)
  @verb       = verb.to_s.upcase.to_sym
  @path       = path
  @definition = definition
  @pattern    = pattern
  @keys       = keys || []

  # Parse the definition into target and options
  parsed   = parse_definition(definition)
  @target  = parsed[:target]
  @options = parsed[:options].freeze

  # Parse the target into class, method, and kind
  target_parsed = parse_target(@target)
  @klass_name   = target_parsed[:klass_name]
  @method_name  = target_parsed[:method_name]
  @kind         = target_parsed[:kind]

  # Freeze for immutability
  freeze
end

Instance Attribute Details

#definitionString (readonly)

Returns The original definition string.

Returns:

  • (String)

    The original definition string



16
17
18
# File 'lib/otto/route_definition.rb', line 16

def definition
  @definition
end

#keysArray<String> (readonly)

Returns The parameter keys extracted from the path.

Returns:

  • (Array<String>)

    The parameter keys extracted from the path



37
38
39
# File 'lib/otto/route_definition.rb', line 37

def keys
  @keys
end

#kindSymbol (readonly)

Returns The invocation kind (:class, :instance, or :logic).

Returns:

  • (Symbol)

    The invocation kind (:class, :instance, or :logic)



28
29
30
# File 'lib/otto/route_definition.rb', line 28

def kind
  @kind
end

#klass_nameString (readonly)

Returns The class name portion.

Returns:

  • (String)

    The class name portion



22
23
24
# File 'lib/otto/route_definition.rb', line 22

def klass_name
  @klass_name
end

#method_nameString (readonly)

Returns The method name portion.

Returns:

  • (String)

    The method name portion



25
26
27
# File 'lib/otto/route_definition.rb', line 25

def method_name
  @method_name
end

#optionsHash (readonly)

Returns The route options (auth, response, csrf, etc.).

Returns:

  • (Hash)

    The route options (auth, response, csrf, etc.)



31
32
33
# File 'lib/otto/route_definition.rb', line 31

def options
  @options
end

#pathString (readonly)

Returns The URL path pattern.

Returns:

  • (String)

    The URL path pattern



13
14
15
# File 'lib/otto/route_definition.rb', line 13

def path
  @path
end

#patternRegexp (readonly)

Returns The compiled path pattern for matching.

Returns:

  • (Regexp)

    The compiled path pattern for matching



34
35
36
# File 'lib/otto/route_definition.rb', line 34

def pattern
  @pattern
end

#targetString (readonly)

Returns The target class and method (e.g., “TestApp.index”).

Returns:

  • (String)

    The target class and method (e.g., “TestApp.index”)



19
20
21
# File 'lib/otto/route_definition.rb', line 19

def target
  @target
end

#verbString (readonly)

Returns The HTTP verb (GET, POST, etc.).

Returns:

  • (String)

    The HTTP verb (GET, POST, etc.)



10
11
12
# File 'lib/otto/route_definition.rb', line 10

def verb
  @verb
end

Instance Method Details

#auth_requirementString?

Get authentication requirement (backward compatibility - returns first requirement)

Returns:

  • (String, nil)

    The auth requirement or nil



78
79
80
# File 'lib/otto/route_definition.rb', line 78

def auth_requirement
  auth_requirements.first
end

#auth_requirementsArray<String>

Get all authentication requirements as an array Supports multiple strategies: auth=session,apikey,oauth

Returns:

  • (Array<String>)

    Array of auth requirement strings



85
86
87
88
89
90
# File 'lib/otto/route_definition.rb', line 85

def auth_requirements
  auth = option(:auth)
  return [] unless auth

  auth.split(',').map(&:strip).reject(&:empty?)
end

#csrf_exempt?Boolean

Check if CSRF is exempt for this route

Returns:

  • (Boolean)


117
118
119
# File 'lib/otto/route_definition.rb', line 117

def csrf_exempt?
  option(:csrf) == 'exempt'
end

#has_option?(key) ⇒ Boolean

Check if route has specific option

Parameters:

  • key (Symbol, String)

    Option key to check

Returns:

  • (Boolean)


64
65
66
# File 'lib/otto/route_definition.rb', line 64

def has_option?(key)
  @options.key?(key.to_sym)
end

#inspectString

Detailed inspection

Returns:

  • (String)


162
163
164
# File 'lib/otto/route_definition.rb', line 162

def inspect
  "#<Otto::RouteDefinition #{self} options=#{@options.inspect}>"
end

#logic_route?Boolean

Check if this is a Logic class route (no . or # in target)

Returns:

  • (Boolean)


123
124
125
# File 'lib/otto/route_definition.rb', line 123

def logic_route?
  kind == :logic
end

#option(key, default = nil) ⇒ Object

Get option value with optional default

Parameters:

  • key (Symbol, String)

    Option key

  • default (Object) (defaults to: nil)

    Default value if option not present

Returns:

  • (Object)


72
73
74
# File 'lib/otto/route_definition.rb', line 72

def option(key, default = nil)
  @options.fetch(key.to_sym, default)
end

#response_typeString

Get response type

Returns:

  • (String)

    The response type (defaults to ‘default’)



111
112
113
# File 'lib/otto/route_definition.rb', line 111

def response_type
  option(:response, 'default')
end

#role_requirementString?

Get role requirement for route-level authorization Supports single role or comma-separated roles (OR logic): role=admin,editor

Returns:

  • (String, nil)

    The role requirement or nil



95
96
97
# File 'lib/otto/route_definition.rb', line 95

def role_requirement
  option(:role)
end

#role_requirementsArray<String>

Get all role requirements as an array Supports multiple roles with OR logic: role=admin,editor

Returns:

  • (Array<String>)

    Array of role requirement strings



102
103
104
105
106
107
# File 'lib/otto/route_definition.rb', line 102

def role_requirements
  role = option(:role)
  return [] unless role

  role.split(',').map(&:strip).reject(&:empty?)
end

#to_hHash

Convert to hash representation

Returns:

  • (Hash)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/otto/route_definition.rb', line 139

def to_h
  {
           verb: @verb,
           path: @path,
     definition: @definition,
         target: @target,
     klass_name: @klass_name,
    method_name: @method_name,
           kind: @kind,
        options: @options,
        pattern: @pattern,
           keys: @keys,
  }
end

#to_sString

String representation for debugging

Returns:

  • (String)


156
157
158
# File 'lib/otto/route_definition.rb', line 156

def to_s
  "#{@verb} #{@path} #{@definition}"
end

#with_options(new_options) ⇒ RouteDefinition

Create a new RouteDefinition with modified options

Parameters:

  • new_options (Hash)

    Options to merge/override

Returns:



130
131
132
133
134
135
# File 'lib/otto/route_definition.rb', line 130

def with_options(new_options)
  merged_options = @options.merge(new_options)
  new_definition = [@target, *merged_options.map { |k, v| "#{k}=#{v}" }].join(' ')

  self.class.new(@verb, @path, new_definition, pattern: @pattern, keys: @keys)
end