module Webmachine
Code to describe the routes in a Webmachine
API, including path, resource class, allowed methods, schemas, policy class. Used in tests and in the pact_broker:routes task
Monkey patches the render_error
method so that it returns hal+json or problem+json instead of text/html
Public Class Methods
build_rack_api(application_context) { |app| ... }
click to toggle source
# File lib/pact_broker/webmachine.rb, line 9 def self.build_rack_api(application_context) api = Webmachine::Application.new do |app| yield app end api.application_context = application_context api.configure do |config| config.adapter = :RackMapped end api.adapter end
decorator_configuration(req)
click to toggle source
rubocop: enable Metrics/ParameterLists
# File lib/webmachine/render_error_monkey_patch.rb, line 76 def self.decorator_configuration(req) req.env["pactbroker.application_context"].decorator_configuration end
error_response_body(req, detail, title, type, status, request)
click to toggle source
rubocop: disable Metrics/ParameterLists
# File lib/webmachine/render_error_monkey_patch.rb, line 64 def self.error_response_body(req, detail, title, type, status, request) if problem_json_error_content_type?(request) decorator_configuration(req) .class_for(:custom_error_problem_json_decorator) .new(detail: detail, title: title, type: type, status: status) .to_json(user_options: { base_url: req.env["pactbroker.base_url"] }) else decorator_configuration(req).class_for(:error_decorator).new(detail).to_json end end
error_response_content_type(request)
click to toggle source
# File lib/webmachine/render_error_monkey_patch.rb, line 53 def self.error_response_content_type(request) if problem_json_error_content_type?(request) "application/problem+json;charset=utf-8" elsif text_html_error_content_type?(request) "text/html;charset=utf-8" else "application/json;charset=utf-8" end end
problem_json_error_content_type?(request)
click to toggle source
# File lib/webmachine/render_error_monkey_patch.rb, line 49 def self.problem_json_error_content_type?(request) request.headers["Accept"]&.include?("application/problem+json") end
render_error(code, req, res, options={})
click to toggle source
Renders a standard error message body for the response. The standard messages are defined in localization files. @param [Integer] code the response status code @param [Request] req the request object @param [Response] req the response object @param [Hash] options keys to override the defaults when rendering
the response body
# File lib/webmachine/render_error_monkey_patch.rb, line 20 def self.render_error(code, req, res, options={}) if text_html_error_content_type?(req) Webmachine.original_render_error(code, req, res, options) else render_error_for_api(code, req, res, options) end end
Also aliased as: original_render_error
render_error_for_api(code, req, res, options)
click to toggle source
# File lib/webmachine/render_error_monkey_patch.rb, line 28 def self.render_error_for_api(code, req, res, options) res.code = code unless res.body title, message = t(["errors.#{code}.title", "errors.#{code}.message"], **{ :method => req.method, :error => res.error}.merge(options)) title = options[:title] if options[:title] message = options[:message] if options[:message] res.body = error_response_body(req, message, title, title.dasherize.gsub(/^\d+\-/, ""), code, req) res.headers[CONTENT_TYPE] = error_response_content_type(req) end ensure_content_length(res) ensure_date_header(res) end
text_html_error_content_type?(request)
click to toggle source
# File lib/webmachine/render_error_monkey_patch.rb, line 45 def self.text_html_error_content_type?(request) request.headers["Accept"]&.include?("text/html") end