Class: TRMNLP::Context
- Inherits:
-
Object
- Object
- TRMNLP::Context
- Defined in:
- lib/trmnlp/context.rb
Defined Under Namespace
Classes: TemplateBinding
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
-
#initialize(root_dir) ⇒ Context
constructor
A new instance of Context.
- #on_view_change(&block) ⇒ Object
- #poll_data ⇒ Object
- #put_webhook(payload) ⇒ Object
- #render_full_page(view) ⇒ Object
- #render_template(view) ⇒ Object
- #screen_classes ⇒ Object
- #start_filewatcher ⇒ Object
- #user_data ⇒ Object
- #validate! ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/trmnlp/context.rb', line 14 def config @config end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
14 15 16 |
# File 'lib/trmnlp/context.rb', line 14 def paths @paths end |
Instance Method Details
#on_view_change(&block) ⇒ Object
46 47 48 |
# File 'lib/trmnlp/context.rb', line 46 def on_view_change(&block) @view_change_callback = block end |
#poll_data ⇒ Object
63 64 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 |
# File 'lib/trmnlp/context.rb', line 63 def poll_data return unless config.plugin.polling? data = {} if config.plugin.polling_urls.empty? raise Error, "config must specify polling_url or polling_urls" end config.plugin.polling_urls.each.with_index do |url, i| verb = config.plugin.polling_verb.upcase print "#{verb} #{url}... " conn = Faraday.new(url:, headers: config.plugin.polling_headers) case verb when 'GET' response = conn.get when 'POST' response = conn.post do |req| req.body = config.plugin.polling_body end end puts "received #{response.body.length} bytes (#{response.status} status)" if response.status == 200 json = wrap_array(JSON.parse(response.body)) else json = {} puts response.body end if config.plugin.polling_urls.count == 1 # For a single polling URL, we just return the JSON directly data = json break else # Multiple URLs are namespaced by index data["IDX_#{i}"] = json end end write_user_data(data) data rescue StandardError => e puts "error: #{e.}" {} end |
#put_webhook(payload) ⇒ Object
114 115 116 117 118 119 |
# File 'lib/trmnlp/context.rb', line 114 def put_webhook(payload) data = wrap_array(JSON.parse(payload)) write_user_data(data) rescue puts "webhook error: #{e.}" end |
#render_full_page(view) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/trmnlp/context.rb', line 138 def render_full_page(view) template = paths.render_template.read ERB.new(template).result(TemplateBinding.new(self, view).get_binding do render_template(view) end) end |
#render_template(view) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/trmnlp/context.rb', line 121 def render_template(view) template_path = paths.template(view) return "Missing template: #{template_path}" unless template_path.exist? shared_template_path = paths.shared_template if shared_template_path.exist? full_markup = shared_template_path.read + template_path.read else full_markup = template_path.read end user_template = Markup::Template.parse(full_markup, environment: liquid_environment) user_template.render(user_data) rescue StandardError => e e. end |
#screen_classes ⇒ Object
146 147 148 149 150 151 |
# File 'lib/trmnlp/context.rb', line 146 def screen_classes classes = 'screen' classes << ' screen--no-bleed' if config.plugin.no_screen_padding == 'yes' classes << ' dark-mode' if config.plugin.dark_mode == 'yes' classes end |
#start_filewatcher ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/trmnlp/context.rb', line 25 def start_filewatcher @filewatcher_thread ||= Thread.new do loop do begin Filewatcher.new(config.project.watch_paths).watch do |changes| config.project.reload! config.plugin.reload! new_user_data = user_data views = changes.map { |path, _change| File.basename(path, '.liquid') } views.each do |view| @view_change_callback.call(view, new_user_data) if @view_change_callback end end rescue => e puts "Error during live render: #{e}" end end end end |
#user_data ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/trmnlp/context.rb', line 50 def user_data merged_data = base_trmnl_data if config.plugin.static? merged_data.merge!(config.plugin.static_data) elsif paths.user_data.exist? merged_data.merge!(JSON.parse(paths.user_data.read)) end # Praise be to ActiveSupport merged_data.deep_merge!(config.project.user_data_overrides) end |
#validate! ⇒ Object
21 22 23 |
# File 'lib/trmnlp/context.rb', line 21 def validate! raise Error, "not a plugin directory (did not find #{paths.trmnlp_config})" unless paths.valid? end |