Module: Webhookdb::Service::ViewApi
- Included in:
- API::Install
- Defined in:
- lib/webhookdb/service/view_api.rb
Overview
Mixin for Grape API endpoints that use HTML rendering. This isn’t tested well enough.
Defined Under Namespace
Classes: FormError
Class Method Summary collapse
Class Method Details
.included(mod) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/webhookdb/service/view_api.rb', line 15 def self.included(mod) mod.helpers do define_method(:render_liquid) do | data_rel_path, vars: {}, content_type: "text/html", # If true, store view params in a cookie so we can re-render this page easily, usually to show errors. serialize_view_params: false | tmpl_file = File.open(Webhookdb::DATA_DIR + data_rel_path) liquid_tmpl = Liquid::Template.parse(tmpl_file.read) rendered = liquid_tmpl.render!(vars.deep_stringify_keys, registers: {}) _endpoint.content_type content_type if serialize_view_params _endpoint.[:whdbviewparams] = {path: data_rel_path, vars:, content_type:}.to_json end env["api.format"] = :binary rendered end define_method(:_endpoint) do env["api.endpoint"] end end mod.rescue_from FormError do |e| if (params = _endpoint.[:whdbviewparams]).blank? merror!(e.status, e.) else begin h = JSON.parse(params) rescue StandardException # If there are any problems, use fallback error handling. merror!(e.status, e.) else vars = h["vars"].symbolize_keys vars[:error_message] = e. new_html = render_liquid( h.fetch("path"), vars:, content_type: h.fetch("content_type"), serialize_view_params: true, ) Rack::Response.new(new_html, e.status, {"Content-Type" => "text/html"}) end end end end |