rack-reloader 0.0.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rack/reloader.rb +62 -0
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29f408d7f20016facb2d4dc3ebcc9a3d064aa22b
4
- data.tar.gz: 0e07479e6cb9fb526d7a9424aa5c4a8f13b27714
3
+ metadata.gz: 58e0698cc207984a6bd5fefa58ac5232b6466f05
4
+ data.tar.gz: 58b1a2849d4bb8e72278c496e23bd8298b88343d
5
5
  SHA512:
6
- metadata.gz: 1a34803a7bb3920654573a16c2726887dfe1b2ae7e71c9dc6687d9b6074892a2f1e25d37e5db0a833f93a8492411b8b2ad19e3c2fa18ad1c331b9f357270509a
7
- data.tar.gz: d662d8bc7c9919cbda535b80f458f972de00e24094fe2fec61a6372032e5c6e8993169a5e15f672aa46d7c3d8f13c3b9f5d45e53ea376ca6d6cf8ef115662446
6
+ metadata.gz: 62cec5ffdc63f4465b2c110fa2ddb901c2854371949bac6689b7ea417b05c163f0c07314d44c12b3eddc7614eb1a86f619784876ad3a387959db443ad86b44bb
7
+ data.tar.gz: 380137e43009d5cb77fc0b0bd0af72566c0e0defbb3fd30803571e5fcc7a395f6a042f302fbdb79d69bc74efb3fb7d51b7a031c0b827dbcabb1082be7332c1f9
@@ -1,5 +1,67 @@
1
1
  module Rack
2
2
  class Reloader
3
+ class Config
4
+ attr_accessor :interval, :ajax
3
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_script(body)]
22
+ end
23
+
24
+ private
25
+
26
+ def inject_script(body)
27
+ interval = @config.interval
28
+ body = body.join("\n")
29
+ template_name = @config.ajax ? :ajax_template : :template
30
+ injection = send(template_name, interval)
31
+ [body.gsub!(/<\/body>/, injection)]
32
+ end
33
+
34
+ def template(interval)
35
+ <<-HTML
36
+ </body>
37
+ <script>
38
+ var reloader_#{@mark} = setTimeout(() => { location.reload(); }, #{interval});
39
+ </script>
40
+ HTML
41
+ end
42
+
43
+ def ajax_template(interval)
44
+ <<-HTML
45
+ </body>
46
+ <script>
47
+ var reloadBody_#{@mark} = function() {
48
+ var url = window.location.href;
49
+ var xhttp = new XMLHttpRequest();
50
+ xhttp.onreadystatechange = function() {
51
+ if (this.readyState == 4 && this.status == 200) {
52
+ let bodyTag = document.getElementsByTagName("body")[0];
53
+ var parser = new DOMParser();
54
+ var remoteDocument = parser.parseFromString(this.responseText, 'text/html');
55
+ let remoteBodyTag = remoteDocument.getElementsByTagName("body")[0];
56
+ bodyTag.innerHTML = remoteBodyTag.innerHTML;
57
+ }
58
+ };
59
+ xhttp.open("GET", url, true);
60
+ xhttp.send();
61
+ }
62
+ var reloader_#{@mark} = setInterval(() => { reloadBody_#{@mark}(); }, #{interval});
63
+ </script>
64
+ HTML
65
+ end
4
66
  end
5
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-reloader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Zinovyev