Class: TRMNLPreview::Context
- Inherits:
-
Object
- Object
- TRMNLPreview::Context
- Defined in:
- lib/trmnl_preview/context.rb
Defined Under Namespace
Classes: ERBBinding
Instance Attribute Summary collapse
-
#live_render ⇒ Object
readonly
Returns the value of attribute live_render.
-
#strategy ⇒ Object
readonly
Returns the value of attribute strategy.
-
#temp_dir ⇒ Object
readonly
Returns the value of attribute temp_dir.
Instance Method Summary collapse
-
#initialize(root) ⇒ Context
constructor
A new instance of Context.
- #on_view_change(&block) ⇒ Object
- #poll_data ⇒ Object
- #render_full_page(view) ⇒ Object
- #render_template(view) ⇒ Object
- #set_data(payload) ⇒ Object
- #start_filewatcher_thread ⇒ Object
- #user_data ⇒ Object
- #view_path(view) ⇒ Object
Constructor Details
#initialize(root) ⇒ Context
Returns a new instance of Context.
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 |
# File 'lib/trmnl_preview/context.rb', line 15 def initialize(root) config_path = File.join(root, 'config.toml') @user_views_dir = File.join(root, 'views') @temp_dir = File.join(root, 'tmp') @data_json_path = File.join(@temp_dir, 'data.json') @liquid_environment = Liquid::Environment.build do |env| env.register_filter(LiquidFilters) end unless File.exist?(config_path) raise "No config.toml found in #{root}" end unless Dir.exist?(@user_views_dir) raise "No views found at #{@user_views_dir}" end config = TomlRB.load_file(config_path) @strategy = config['strategy'] @url = config['url'] @polling_headers = config['polling_headers'] || {} @live_render = config['live_render'] != false unless ['polling', 'webhook'].include?(@strategy) raise "Invalid strategy: #{strategy} (must be 'polling' or 'webhook')" end FileUtils.mkdir_p(@temp_dir) start_filewatcher_thread if @live_render end |
Instance Attribute Details
#live_render ⇒ Object (readonly)
Returns the value of attribute live_render.
13 14 15 |
# File 'lib/trmnl_preview/context.rb', line 13 def live_render @live_render end |
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
13 14 15 |
# File 'lib/trmnl_preview/context.rb', line 13 def strategy @strategy end |
#temp_dir ⇒ Object (readonly)
Returns the value of attribute temp_dir.
13 14 15 |
# File 'lib/trmnl_preview/context.rb', line 13 def temp_dir @temp_dir end |
Instance Method Details
#on_view_change(&block) ⇒ Object
65 66 67 |
# File 'lib/trmnl_preview/context.rb', line 65 def on_view_change(&block) @view_change_callback = block end |
#poll_data ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/trmnl_preview/context.rb', line 75 def poll_data if @url.nil? raise "URL is required for polling strategy" end print "Fetching #{@url}... " if @url.match?(/^https?:\/\//) payload = URI.open(@url, @polling_headers).read else payload = File.read(@url) end File.write(@data_json_path, payload) puts "got #{payload.size} bytes" user_data end |
#render_full_page(view) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/trmnl_preview/context.rb', line 114 def render_full_page(view) page_erb_template = File.read(File.join(__dir__, '..', '..', 'web', 'views', 'render_view.erb')) ERB.new(page_erb_template).result(ERBBinding.new(view).get_binding do render_template(view) end) end |
#render_template(view) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/trmnl_preview/context.rb', line 102 def render_template(view) path = view_path(view) unless File.exist?(path) return "Missing plugin template: views/#{view}.liquid" end user_template = Liquid::Template.parse(File.read(path), environment: @liquid_environment) user_template.render(user_data) rescue StandardError => e e. end |
#set_data(payload) ⇒ Object
94 95 96 |
# File 'lib/trmnl_preview/context.rb', line 94 def set_data(payload) File.write(@data_json_path, payload) end |
#start_filewatcher_thread ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/trmnl_preview/context.rb', line 48 def start_filewatcher_thread Thread.new do loop do begin Filewatcher.new(@user_views_dir).watch do |changes| views = changes.map { |path, _change| File.basename(path, '.liquid') } views.each do |view| @view_change_callback.call(view) if @view_change_callback end end rescue => e puts "Error during live render: #{e}" end end end end |
#user_data ⇒ Object
69 70 71 72 73 |
# File 'lib/trmnl_preview/context.rb', line 69 def user_data data = JSON.parse(File.read(@data_json_path)) data = { data: data } if data.is_a?(Array) # per TRMNL docs, bare array is wrapped in 'data' key data end |
#view_path(view) ⇒ Object
98 99 100 |
# File 'lib/trmnl_preview/context.rb', line 98 def view_path(view) File.join(@user_views_dir, "#{view}.liquid") end |