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
- .dummy_request(http_method: "GET", path_info:) ⇒ Object
- .info_from_resource_instance(webmachine_route, application_context) ⇒ Object
- .paths_to_resource_class_mappings(webmachine_application) ⇒ Object
-
.schemas(allowed_methods, resource, path_info) ⇒ Object
This is not entirely accurate, because some GET requests have schemas too, but we can’t tell that statically at the moment.
- .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
64 65 66 67 68 69 70 71 72 |
# File 'lib/webmachine/describe_routes.rb', line 64 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 |
.dummy_request(http_method: "GET", path_info:) ⇒ Object
120 121 122 123 124 |
# File 'lib/webmachine/describe_routes.rb', line 120 def self.dummy_request(http_method: "GET", path_info: ) dummy_request = Webmachine::Adapters::Rack::RackRequest.new(http_method, "/", Webmachine::Headers["host" => "example.org"], nil, {}, {}, { "REQUEST_METHOD" => http_method }) dummy_request.path_info = path_info dummy_request end |
.info_from_resource_instance(webmachine_route, application_context) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/webmachine/describe_routes.rb', line 87 def self.info_from_resource_instance(webmachine_route, application_context) with_no_logging do path_info = { application_context: application_context, pacticipant_name: "foo", pacticipant_version_number: "1", resource_name: "foo" } path_info.default = "1" dummy_request = dummy_request(http_method: "GET", path_info: path_info) dummy_resource = webmachine_route.resource.new(dummy_request, Webmachine::Response.new) if dummy_resource { allowed_methods: dummy_resource.allowed_methods, schemas: dummy_resource.respond_to?(:schema, true) && dummy_resource.send(:schema) ? schemas(dummy_resource.allowed_methods, webmachine_route.resource, path_info) : nil }.compact else {} end end rescue StandardError => e puts "Could not determine instance info for #{webmachine_route.resource}. #{e.class} - #{e.}" {} end |
.paths_to_resource_class_mappings(webmachine_application) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/webmachine/describe_routes.rb', line 74 def self.paths_to_resource_class_mappings(webmachine_application) webmachine_application.routes.collect do | webmachine_route | resource_path_absolute = Pathname.new(source_location_for(webmachine_route.resource)) Route.new({ path: "/" + webmachine_route.path_spec.collect{ |part| part.is_a?(Symbol) ? ":#{part}" : part }.join("/"), path_spec: webmachine_route.path_spec, resource_class: webmachine_route.resource, resource_name: webmachine_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(webmachine_route, webmachine_application.application_context))) end.reject{ | route | route.resource_class == Webmachine::Trace::TraceResource } end |
.schemas(allowed_methods, resource, path_info) ⇒ Object
This is not entirely accurate, because some GET requests have schemas too, but we can’t tell that statically at the moment
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/webmachine/describe_routes.rb', line 109 def self.schemas(allowed_methods, resource, path_info) (allowed_methods - ["GET", "OPTIONS", "DELETE"]).collect do | http_method | resource.new(dummy_request(http_method: http_method, path_info: path_info), Webmachine::Response.new).send(:schema) end.uniq.collect do | schema_class | { class: schema_class, location: source_location_for(schema_class) } end end |
.source_location_for(clazz) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/webmachine/describe_routes.rb', line 126 def self.source_location_for(clazz) first_instance_method_name = (clazz.instance_methods(false) + clazz.private_instance_methods(false)).first if first_instance_method_name clazz.instance_method(first_instance_method_name).source_location.first else # have a guess! "lib/" + clazz.name.snakecase.gsub("::", "/") + ".rb" end 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
137 138 139 140 141 142 143 |
# File 'lib/webmachine/describe_routes.rb', line 137 def self.with_no_logging original_default_level = SemanticLogger.default_level SemanticLogger.default_level = :fatal yield ensure SemanticLogger.default_level = original_default_level end |