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
#initialize(root_dir) ⇒ Context
Returns a new instance of Context.
15 16 17 18 |
# File 'lib/trmnlp/context.rb', line 15 def initialize(root_dir) @paths = Paths.new(root_dir) @config = Config.new(paths) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/trmnlp/context.rb', line 13 def config @config end |
#paths ⇒ Object (readonly)
Returns the value of attribute paths.
13 14 15 |
# File 'lib/trmnlp/context.rb', line 13 def paths @paths end |
Instance Method Details
#on_view_change(&block) ⇒ Object
45 46 47 |
# File 'lib/trmnlp/context.rb', line 45 def on_view_change(&block) @view_change_callback = block end |
#poll_data ⇒ Object
62 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 |
# File 'lib/trmnlp/context.rb', line 62 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.message}" {} end |
#put_webhook(payload) ⇒ Object
113 114 115 116 117 118 |
# File 'lib/trmnlp/context.rb', line 113 def put_webhook(payload) data = wrap_array(JSON.parse(payload)) write_user_data(data) rescue puts "webhook error: #{e.message}" end |
#render_full_page(view) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/trmnlp/context.rb', line 137 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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/trmnlp/context.rb', line 120 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 = Liquid::Template.parse(full_markup, environment: liquid_environment) user_template.render(user_data) rescue StandardError => e e. end |
#screen_classes ⇒ Object
145 146 147 148 149 150 |
# File 'lib/trmnlp/context.rb', line 145 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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/trmnlp/context.rb', line 24 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
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/trmnlp/context.rb', line 49 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
20 21 22 |
# File 'lib/trmnlp/context.rb', line 20 def validate! raise Error, "not a plugin directory (did not find #{paths.trmnlp_config})" unless paths.valid? end |