rack-refresher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c7b0c54d1a4a1d0c3dfdbdc0a77d6546ac1f9b4
4
+ data.tar.gz: '017769e685a8f654e21c95fcd0244bf34980fd3e'
5
+ SHA512:
6
+ metadata.gz: eb2d1dda17d368af7ff510177797dad133c48e34a69e91fb89708dae895e6f779667d4d0c6e3941d68c729d9a30e72fd75ac71bda966462b40ef0577958f4b4a
7
+ data.tar.gz: 66627756191b8b7926eff4cd2f2c79a81c99959e065147cad3ec9bceae7857290c7a983e52f8cac766e2a9b0b2a252f96f2b2cd3a9ba82704bc8bd786117e320
@@ -0,0 +1,69 @@
1
+ module Rack
2
+ class Refresher
3
+ class Config
4
+ attr_accessor :interval, :ajax
5
+
6
+ def initialize
7
+ @interval = 5000
8
+ @ajax = false
9
+ end
10
+ end
11
+
12
+ def initialize(app)
13
+ @app = app
14
+ @config = Config.new
15
+ @mark = rand(Time.now.to_i)
16
+ yield(@config) if block_given?
17
+ end
18
+
19
+ def call(env)
20
+ status, headers, body = @app.call(env)
21
+ [status, headers, inject(body)]
22
+ end
23
+
24
+ private
25
+
26
+ def inject(body)
27
+ body = body.join("\n")
28
+ [body.gsub!(/<\/body>/, render_template)]
29
+ end
30
+
31
+ def render_template
32
+ template_name = @config.ajax ? :ajax_template : :template
33
+ send(template_name, @config.interval)
34
+ end
35
+
36
+ def template(interval)
37
+ <<-HTML
38
+ </body>
39
+ <script>
40
+ var reloader_#{@mark} = setTimeout(() => { location.reload(); }, #{interval});
41
+ </script>
42
+ HTML
43
+ end
44
+
45
+ def ajax_template(interval)
46
+ <<-HTML
47
+ </body>
48
+ <script>
49
+ var reloadBody_#{@mark} = function() {
50
+ var url = window.location.href;
51
+ var xhttp = new XMLHttpRequest();
52
+ xhttp.onreadystatechange = function() {
53
+ if (this.readyState == 4 && this.status == 200) {
54
+ let bodyTag = document.getElementsByTagName("body")[0];
55
+ var parser = new DOMParser();
56
+ var remoteDocument = parser.parseFromString(this.responseText, 'text/html');
57
+ let remoteBodyTag = remoteDocument.getElementsByTagName("body")[0];
58
+ bodyTag.innerHTML = remoteBodyTag.innerHTML;
59
+ }
60
+ };
61
+ xhttp.open("GET", url, true);
62
+ xhttp.send();
63
+ }
64
+ var reloader_#{@mark} = setInterval(() => { reloadBody_#{@mark}(); }, #{interval});
65
+ </script>
66
+ HTML
67
+ end
68
+ end
69
+ end
@@ -0,0 +1 @@
1
+ require "rack/refresher"
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-refresher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Zinovyev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Refresh the content of your page with a given interval
14
+ email: ivan@zinovyev.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rack-refresher.rb
20
+ - lib/rack/refresher.rb
21
+ homepage: https://github.com/zinovyev/rack-refresher
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.6.11
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Rack middleware for authomatic page refresh
45
+ test_files: []