Class: Webmachine::DescribeRoutes

Inherits:
Object
  • Object
show all
Defined in:
lib/webmachine/describe_routes.rb

Defined Under Namespace

Classes: Route

Class Method Summary collapse

Class Method Details

.build_request(http_method: "GET", path_info:) ⇒ Object



136
137
138
139
140
# 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) ⇒ Object



129
130
131
132
133
134
# 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) ⇒ Array<Webmachine::DescribeRoutes::Route>

Build a Route object to describe every Webmachine route defined in the app.routes block



73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
# 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) ⇒ Hash

Return the properties of the resource that can only be determined by instantiating the resource

Returns:

  • (Hash)


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 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) ⇒ Object



142
143
144
145
146
147
148
149
150
# 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) ⇒ Object



86
87
88
# 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_loggingObject

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



153
154
155
156
157
158
159
# 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