Class: Webmachine::DescribeRoutes
- Inherits:
-
Object
- Object
- Webmachine::DescribeRoutes
- Defined in:
- lib/webmachine/describe_routes.rb
Defined Under Namespace
Classes: Route
Class Method Summary collapse
- .call(webmachine_applications, search_term: nil) ⇒ Object
- .info_from_resource_instance(route) ⇒ Object
- .paths_to_resource_class_mappings(webmachine_application) ⇒ Object
- .source_location_for(clazz) ⇒ Object
-
.with_no_logging ⇒ Object
If we don’t turn off the logging, we get metrics logging due to the instantiation of the Webmachine::RackRequest class.
Class Method Details
.call(webmachine_applications, search_term: nil) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/webmachine/describe_routes.rb', line 28 def self.call(webmachine_applications, search_term: nil) path_mappings = webmachine_applications.flat_map { | webmachine_application | paths_to_resource_class_mappings(webmachine_application) } if search_term path_mappings = path_mappings.select{ |(route, _)| route[:path].include?(search_term) } end path_mappings.sort_by{ | mapping | mapping[:path] } end |
.info_from_resource_instance(route) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/webmachine/describe_routes.rb', line 50 def self.info_from_resource_instance(route) with_no_logging do path_info = { application_context: OpenStruct.new, pacticipant_name: "foo", pacticipant_version_number: "1", resource_name: "foo" } path_info.default = "1" dummy_request = Webmachine::Adapters::Rack::RackRequest.new("GET", "/", Webmachine::Headers["host" => "example.org"], nil, {}, {}, { "REQUEST_METHOD" => "GET" }) dummy_request.path_info = path_info dummy_resource = route.resource.new(dummy_request, Webmachine::Response.new) if dummy_resource { allowed_methods: dummy_resource.allowed_methods, } else {} end end rescue StandardError => e puts "Could not determine instance info for #{route.resource}. #{e.class} - #{e.}" {} end |
.paths_to_resource_class_mappings(webmachine_application) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/webmachine/describe_routes.rb', line 38 def self.paths_to_resource_class_mappings(webmachine_application) webmachine_application.routes.collect do | route | resource_path_absolute = Pathname.new(source_location_for(route.resource)) Route.new({ path: "/" + route.path_spec.collect{ |part| part.is_a?(Symbol) ? ":#{part}" : part }.join("/"), resource_class: route.resource, resource_name: route.instance_variable_get(:@bindings)[:resource_name], resource_class_location: resource_path_absolute.relative_path_from(Pathname.pwd).to_s }.merge(info_from_resource_instance(route))) end end |
.source_location_for(clazz) ⇒ Object
70 71 72 73 |
# File 'lib/webmachine/describe_routes.rb', line 70 def self.source_location_for(clazz) first_instance_method_name = (clazz.instance_methods(false) + clazz.private_instance_methods(false)).first clazz.instance_method(first_instance_method_name).source_location.first end |
.with_no_logging ⇒ Object
If we don’t turn off the logging, we get metrics logging due to the instantiation of the Webmachine::RackRequest class
76 77 78 79 80 81 82 |
# File 'lib/webmachine/describe_routes.rb', line 76 def self.with_no_logging original_default_level = SemanticLogger.default_level SemanticLogger.default_level = :fatal yield ensure SemanticLogger.default_level = original_default_level end |