Module: RESTFramework::Utils
- Defined in:
- lib/rest_framework/utils.rb
Class Method Summary collapse
-
.get_route_pattern(application_routes, request) ⇒ Object
Helper to get the current route pattern, stripped of the ‘(:format)` segment.
-
.get_routes(application_routes, request) ⇒ Object
Helper for showing routes under a controller action, used for the browsable API.
-
.parse_extra_actions(extra_actions) ⇒ Object
Helper to take extra_actions hash and convert to a consistent format: ‘methods:, kwargs:`.
Class Method Details
.get_route_pattern(application_routes, request) ⇒ Object
Helper to get the current route pattern, stripped of the ‘(:format)` segment.
39 40 41 42 43 |
# File 'lib/rest_framework/utils.rb', line 39 def self.get_route_pattern(application_routes, request) application_routes.router.recognize(request) do |route, _, _| return route.path.spec.to_s.gsub(/\(\.:format\)$/, "") end end |
.get_routes(application_routes, request) ⇒ Object
Helper for showing routes under a controller action, used for the browsable API.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rest_framework/utils.rb', line 46 def self.get_routes(application_routes, request) current_pattern = self.get_route_pattern(application_routes, request) current_subdomain = request.subdomain.presence # Return routes that match our current route subdomain/pattern, grouped by controller. return application_routes.routes.map { |r| { verb: r.verb, path: r.path.spec.to_s, action: r.defaults[:action].presence, controller: r.defaults[:controller].presence, subdomain: r.defaults[:subdomain].presence, route_app: r.app&.app&.inspect&.presence, } }.select { |r| r[:subdomain] == current_subdomain && r[:path].start_with?(current_pattern) }.group_by { |r| r[:controller] } end |
.parse_extra_actions(extra_actions) ⇒ Object
Helper to take extra_actions hash and convert to a consistent format: ‘methods:, kwargs:`.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rest_framework/utils.rb', line 4 def self.parse_extra_actions(extra_actions) return (extra_actions || {}).map do |k, v| kwargs = {action: k} path = k # Convert structure to path/methods/kwargs. if v.is_a?(Hash) # allow kwargs v = v.symbolize_keys # Ensure methods is an array. if v[:methods].is_a?(String) || v[:methods].is_a?(Symbol) methods = [v.delete(:methods)] else methods = v.delete(:methods) end # Override path if it's provided. if v.key?(:path) path = v.delete(:path) end # Pass any further kwargs to the underlying Rails interface. kwargs = kwargs.merge(v) elsif v.is_a?(Symbol) || v.is_a?(String) methods = [v] else methods = v end # Return a hash with keys: :path, :methods, :kwargs. {path: path, methods: methods, kwargs: kwargs} end end |