class Webmachine::DescribeRoutes

Constants

Route

Public Class Methods

build_request(http_method: "GET", path_info: ) click to toggle source
# File lib/webmachine/describe_routes.rb, line 136
def self.build_request(http_method: "GET", path_info: )
  request = Webmachine::Adapters::Rack::RackRequest.new(http_method, "/", Webmachine::Headers["host" => "example.org"], nil, {}, {}, { "REQUEST_METHOD" => http_method })
  request.path_info = path_info
  request
end
build_resource(webmachine_route, http_method, application_context) click to toggle source
# File lib/webmachine/describe_routes.rb, line 129
def self.build_resource(webmachine_route, http_method, application_context)
  path_info = { application_context: application_context, pacticipant_name: "foo", pacticipant_version_number: "1", resource_name: "foo" }
  path_info.default = "1"
  request = build_request(http_method: http_method, path_info: path_info)
  webmachine_route.resource.new(request, Webmachine::Response.new)
end
build_routes(webmachine_application) click to toggle source

Build a Route object to describe every Webmachine route defined in the app.routes block @return [Array<Webmachine::DescribeRoutes::Route>]

# File lib/webmachine/describe_routes.rb, line 73
def self.build_routes(webmachine_application)
  webmachine_routes_to_describe(webmachine_application).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(properties_for_webmachine_route(webmachine_route, webmachine_application.application_context)))
  end
end
call(webmachine_applications, search_term: nil) click to toggle source
# File lib/webmachine/describe_routes.rb, line 61
def self.call(webmachine_applications, search_term: nil)
  path_mappings = webmachine_applications.flat_map { | webmachine_application | build_routes(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
properties_for_resource(allowed_methods, webmachine_route, application_context) click to toggle source

Return the properties of the resource that can only be determined by instantiating the resource @return [Hash]

# File lib/webmachine/describe_routes.rb, line 110
def self.properties_for_resource(allowed_methods, webmachine_route, application_context)
  schemas = []
  policy_names = []
  allowed_methods.each do | http_method |
    resource = build_resource(webmachine_route, http_method, application_context)
    if (schema_class = resource.respond_to?(:schema, true) && resource.send(:schema))
      schemas << { http_method: http_method, class: schema_class, location: source_location_for(schema_class)}
    end

    policy_names << resource.policy_name
  end

  {
    allowed_methods: allowed_methods,
    schemas: schemas,
    policy_names: policy_names.uniq
  }
end
properties_for_webmachine_route(webmachine_route, application_context) click to toggle source
# File lib/webmachine/describe_routes.rb, line 90
def self.properties_for_webmachine_route(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"
    request = build_request(http_method: "GET", path_info: path_info)

    resource = webmachine_route.resource.new(request, Webmachine::Response.new)
    if resource
      properties_for_resource(resource.allowed_methods - ["OPTIONS"], webmachine_route, application_context)
    else
      {}
    end
  end
rescue StandardError => e
  puts "Could not determine instance info for #{webmachine_route.resource}. #{e.class} - #{e.message}"
  {}
end
source_location_for(clazz) click to toggle source
# File lib/webmachine/describe_routes.rb, line 142
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
webmachine_routes_to_describe(webmachine_application) click to toggle source
# File lib/webmachine/describe_routes.rb, line 86
def self.webmachine_routes_to_describe(webmachine_application)
  webmachine_application.routes.reject{ | route | route.resource == Webmachine::Trace::TraceResource }.collect
end
with_no_logging() { || ... } click to toggle source

If we don’t turn off the logging, we get metrics logging due to the instantiation of the Webmachine::RackRequest class

# File lib/webmachine/describe_routes.rb, line 153
def self.with_no_logging
  original_default_level = SemanticLogger.default_level
  SemanticLogger.default_level = :fatal
  yield
ensure
  SemanticLogger.default_level = original_default_level
end