class Rack::Refresher
Public Class Methods
new(app) { |config| ... }
click to toggle source
# File lib/rack/refresher.rb, line 17 def initialize(app) @app = app @config = Config.new @mark = rand(Time.now.to_i) yield(@config) if block_given? end
Public Instance Methods
call(env)
click to toggle source
# File lib/rack/refresher.rb, line 24 def call(env) status, headers, body = @app.call(env) Response.new(inject(body), status, headers) end
Private Instance Methods
ajax_template(interval)
click to toggle source
# File lib/rack/refresher.rb, line 58 def ajax_template(interval) <<-HTML </body> <script> var reloadBody_#{@mark} = function() { var url = window.location.href; var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { let bodyTag = document.getElementsByTagName("body")[0]; var parser = new DOMParser(); var remoteDocument = parser.parseFromString(this.responseText, 'text/html'); let remoteBodyTag = remoteDocument.getElementsByTagName("body")[0]; bodyTag.innerHTML = remoteBodyTag.innerHTML; } }; xhttp.open("GET", url, true); xhttp.send(); } var reloader_#{@mark} = setInterval(() => { reloadBody_#{@mark}(); }, #{interval}); </script> HTML end
inject(body)
click to toggle source
# File lib/rack/refresher.rb, line 31 def inject(body) [join(body).gsub(/<\/body>/i, render_template)] end
join(body)
click to toggle source
# File lib/rack/refresher.rb, line 35 def join(body) result = "" body.each do |line| result << line result << "\n" end result end
render_template()
click to toggle source
# File lib/rack/refresher.rb, line 44 def render_template template_name = @config.ajax ? :ajax_template : :template send(template_name, @config.interval) end
template(interval)
click to toggle source
# File lib/rack/refresher.rb, line 49 def template(interval) <<-HTML </body> <script> var reloader_#{@mark} = setTimeout(() => { location.reload(); }, #{interval}); </script> HTML end