Class: Admission::VisualisationApp
- Inherits:
-
Object
- Object
- Admission::VisualisationApp
- Defined in:
- lib/admission/visualisation_app.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #default_404_response ⇒ Object
-
#initialize(**settings) ⇒ VisualisationApp
constructor
A new instance of VisualisationApp.
- #render_data(privileges:, rules:, arbitrator: Admission::ResourceArbitration) ⇒ Object
- #render_page ⇒ Object
Constructor Details
#initialize(**settings) ⇒ VisualisationApp
Returns a new instance of VisualisationApp.
6 7 8 9 10 11 12 13 14 |
# File 'lib/admission/visualisation_app.rb', line 6 def initialize **settings order = settings[:order] || (raise 'order not defined, cannot visualise data') raise 'order must be a Proc' unless Proc === order settings[:js_entry] ||= Pathname.new(__FILE__).join('..', '..', '..', 'visualisation', 'dist', 'app.js') @settings = settings end |
Instance Method Details
#call(env) ⇒ Object
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 |
# File 'lib/admission/visualisation_app.rb', line 16 def call env case env['PATH_INFO'].to_s when %r~^/(\.html)?$~ [ 200, {'Content-Type' => 'text/html; charset=utf-8'}, [render_page] ] when %r~/app\.js~ [ 200, {'Content-Type' => 'application/js; charset=utf-8'}, [File.read(@settings[:js_entry])] ] when %r~/data\.json~ [ 200, {'Content-Type' => 'application/json; charset=utf-8'}, [render_data(@settings[:order].call)] ] else default_404_response end end |
#default_404_response ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/admission/visualisation_app.rb', line 121 def default_404_response page = if @settings[:file_404] File.read @settings[:file_404] else 'Admission::VisualisationApp : page not found' end [ 404, {'Content-Type' => 'text/html; charset=utf-8'}, [page] ] end |
#render_data(privileges:, rules:, arbitrator: Admission::ResourceArbitration) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/admission/visualisation_app.rb', line 65 def render_data privileges:, rules:, arbitrator: Admission::ResourceArbitration js_data = {} top_levels = [] privileges = privileges.values.inject Array.new do |arr, levels| tops, others = levels.to_a.partition{|key, _| key == :'^'} tops.first[1].tap{|privilege| top_levels << privilege.text_key} others.each do |_, privilege| arr << {name: privilege.name, level: privilege.level, inherits: privilege.inherited && privilege.inherited.map(&:text_key)} end arr end js_data[:privileges] = privileges js_data[:top_levels] = top_levels js_data[:levels] = privileges.inject Hash.new do |hash, p| (hash[p[:name]] ||= []) << p[:level] hash end actions_reduce = -> (index) { index.to_a.map do |action, action_index| action_index = action_index.to_a.map do |privilege, rule| if rule.is_a? Proc rule = 'proc' end [privilege.text_key, rule] end [action, Hash[action_index]] end } rules = if arbitrator == Admission::Arbitration single_scope = actions_reduce[rules] [['-non-scoped-', Hash[single_scope]]] elsif arbitrator == Admission::ResourceArbitration rules.to_a.map do |scope, scope_index| scope_index = actions_reduce[scope_index] [scope, Hash[scope_index]] end else raise "not implemented for #{arbitrator.name}" end js_data[:rules] = Hash[rules] JSON.generate js_data end |
#render_page ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/admission/visualisation_app.rb', line 45 def render_page data_url = "#{@settings[:url_prefix]}/data.json" script_url = "#{@settings[:url_prefix]}/app.js" <<-HEREDOC <!DOCTYPE html> <html> <head> <title>Admission</title> </head> <body class='flex-column'> <div data-url="#{data_url}" id="admission-visualisation"></div> <script src="#{script_url}"></script> </body> </html> HEREDOC end |