Class: Grape::Router::Route
- Inherits:
-
Object
- Object
- Grape::Router::Route
- Extended by:
- Forwardable
- Defined in:
- lib/grape/router/route.rb
Constant Summary collapse
- ROUTE_ATTRIBUTE_REGEXP =
/route_([_a-zA-Z]\w*)/.freeze
- SOURCE_LOCATION_REGEXP =
/^(.*?):(\d+?)(?::in `.+?')?$/.freeze
- FIXED_NAMED_CAPTURES =
%w[format version].freeze
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#index ⇒ Object
Returns the value of attribute index.
-
#options ⇒ Object
Returns the value of attribute options.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
-
#regexp ⇒ Object
Returns the value of attribute regexp.
-
#translator ⇒ Object
(also: #attributes)
Returns the value of attribute translator.
Instance Method Summary collapse
- #apply(app) ⇒ Object
- #exec(env) ⇒ Object
-
#initialize(method, pattern, **options) ⇒ Route
constructor
A new instance of Route.
- #match?(input) ⇒ Boolean
- #method_missing(method_id, *arguments) ⇒ Object
- #params(input = nil) ⇒ Object
- #respond_to_missing?(method_id, _) ⇒ Boolean
- #route_method ⇒ Object
- #route_path ⇒ Object
Constructor Details
#initialize(method, pattern, **options) ⇒ Route
Returns a new instance of Route.
66 67 68 69 70 71 72 73 74 |
# File 'lib/grape/router/route.rb', line 66 def initialize(method, pattern, **) method_s = method.to_s method_upcase = Grape::Http::Headers.find_supported_method(method_s) || method_s.upcase @suffix = [:suffix] @options = .merge(method: method_upcase) @pattern = Pattern.new(pattern, **) @translator = AttributeTranslator.new(**, request_method: method_upcase) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/grape/router/route.rb', line 22 def method_missing(method_id, *arguments) match = ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s) if match method_name = match.captures.last.to_sym warn_route_methods(method_name, caller(1).shift) @options[method_name] else super end end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
15 16 17 |
# File 'lib/grape/router/route.rb', line 15 def app @app end |
#index ⇒ Object
Returns the value of attribute index.
15 16 17 |
# File 'lib/grape/router/route.rb', line 15 def index @index end |
#options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/grape/router/route.rb', line 15 def @options end |
#pattern ⇒ Object
Returns the value of attribute pattern.
15 16 17 |
# File 'lib/grape/router/route.rb', line 15 def pattern @pattern end |
#regexp ⇒ Object
Returns the value of attribute regexp.
15 16 17 |
# File 'lib/grape/router/route.rb', line 15 def regexp @regexp end |
#translator ⇒ Object Also known as: attributes
Returns the value of attribute translator.
15 16 17 |
# File 'lib/grape/router/route.rb', line 15 def translator @translator end |
Instance Method Details
#apply(app) ⇒ Object
80 81 82 83 |
# File 'lib/grape/router/route.rb', line 80 def apply(app) @app = app self end |
#exec(env) ⇒ Object
76 77 78 |
# File 'lib/grape/router/route.rb', line 76 def exec(env) @app.call(env) end |
#match?(input) ⇒ Boolean
85 86 87 |
# File 'lib/grape/router/route.rb', line 85 def match?(input) translator.respond_to?(:forward_match) && translator.forward_match ? input.start_with?(pattern.origin) : pattern.match?(input) end |
#params(input = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/grape/router/route.rb', line 89 def params(input = nil) if input.nil? pattern.named_captures.keys.each_with_object(translator.params) do |(key), defaults| defaults[key] ||= '' unless FIXED_NAMED_CAPTURES.include?(key) || defaults.key?(key) end else parsed = pattern.params(input) parsed ? parsed.delete_if { |_, value| value.nil? }.symbolize_keys : {} end end |
#respond_to_missing?(method_id, _) ⇒ Boolean
33 34 35 |
# File 'lib/grape/router/route.rb', line 33 def respond_to_missing?(method_id, _) ROUTE_ATTRIBUTE_REGEXP.match(method_id.to_s) end |
#route_method ⇒ Object
56 57 58 59 |
# File 'lib/grape/router/route.rb', line 56 def route_method warn_route_methods(:method, caller(1).shift, :request_method) request_method end |
#route_path ⇒ Object
61 62 63 64 |
# File 'lib/grape/router/route.rb', line 61 def route_path warn_route_methods(:path, caller(1).shift) pattern.path end |