Class: TRMNLPreview::Context
- Inherits:
-
Object
- Object
- TRMNLPreview::Context
- Defined in:
- lib/trmnl_preview/context.rb
Defined Under Namespace
Classes: ERBBinding
Instance Attribute Summary collapse
-
#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.
- #poll_data ⇒ Object
- #render_full_page(view) ⇒ Object
- #render_template(view) ⇒ Object
- #set_data(payload) ⇒ Object
- #user_data ⇒ Object
- #view_path(view) ⇒ Object
Constructor Details
#initialize(root) ⇒ Context
Returns a new instance of Context.
14 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 |
# File 'lib/trmnl_preview/context.rb', line 14 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'] || {} unless ['polling', 'webhook'].include?(@strategy) raise "Invalid strategy: #{strategy} (must be 'polling' or 'webhook')" end FileUtils.mkdir_p(@temp_dir) end |
Instance Attribute Details
#strategy ⇒ Object (readonly)
Returns the value of attribute strategy.
12 13 14 |
# File 'lib/trmnl_preview/context.rb', line 12 def strategy @strategy end |
#temp_dir ⇒ Object (readonly)
Returns the value of attribute temp_dir.
12 13 14 |
# File 'lib/trmnl_preview/context.rb', line 12 def temp_dir @temp_dir end |
Instance Method Details
#poll_data ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/trmnl_preview/context.rb', line 50 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
87 88 89 90 91 92 93 |
# File 'lib/trmnl_preview/context.rb', line 87 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
77 78 79 80 81 82 83 84 85 |
# File 'lib/trmnl_preview/context.rb', line 77 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) end |
#set_data(payload) ⇒ Object
69 70 71 |
# File 'lib/trmnl_preview/context.rb', line 69 def set_data(payload) File.write(@data_json_path, payload) end |
#user_data ⇒ Object
44 45 46 47 48 |
# File 'lib/trmnl_preview/context.rb', line 44 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
73 74 75 |
# File 'lib/trmnl_preview/context.rb', line 73 def view_path(view) File.join(@user_views_dir, "#{view}.liquid") end |