easy_poller 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +110 -0
- data/Rakefile +2 -0
- data/app/assets/javascripts/easy_poller.js +68 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/easy_poller.gemspec +34 -0
- data/lib/easy_poller.rb +7 -0
- data/lib/easy_poller/engine.rb +20 -0
- data/lib/easy_poller/helper.rb +27 -0
- data/lib/easy_poller/rails.rb +5 -0
- data/lib/easy_poller/sinatra.rb +5 -0
- data/lib/easy_poller/version.rb +3 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b58ca12b6b121ba52fb30fefd1f2042879227e32
|
4
|
+
data.tar.gz: 33b7d9df8e84520423e24b7492b5f68c97059e33
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e063212b07d888b27a5c9fc1756b586ac427d4685034610fc3c5818433bcc38519d26f6f80a8ced2339aa5eabd2bf093fe08e60943cea8833b99b5ee9b244d8b
|
7
|
+
data.tar.gz: cad63c705ce9016d025885b80929c4a41fd4cd922e3b0a4c34018f4619489c65d1e9568b8c209306ae017e34676196175d63e0865da1ce4540719522d568a8d4
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 fabrouy
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Easy Poller
|
2
|
+
|
3
|
+
Very simple helper to poll for changes. Just give it an URL and voila.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'easy_poller'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install easy_poller
|
20
|
+
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
In `application.js`, add:
|
25
|
+
|
26
|
+
```js
|
27
|
+
//= require easy_poller
|
28
|
+
```
|
29
|
+
|
30
|
+
Add a polling container.
|
31
|
+
|
32
|
+
```erb
|
33
|
+
<%= easy_poller count_users_path %>
|
34
|
+
```
|
35
|
+
|
36
|
+
This will create something like this and start polling for changes to the given URL. Container's inner html will be replaced with your endpoint's response.
|
37
|
+
|
38
|
+
```html
|
39
|
+
<div class="easy_poller" data-poll="true" data-interval="3000" data-url="/users/count"></div>
|
40
|
+
```
|
41
|
+
|
42
|
+
Now make your controller return something useful.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
class UsersController < ApplicationController
|
46
|
+
def count
|
47
|
+
render text: User.count
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
That's it!
|
53
|
+
|
54
|
+
### Options
|
55
|
+
|
56
|
+
Poll every 10 seconds. _Default: 3_
|
57
|
+
|
58
|
+
```erb
|
59
|
+
<%= easy_poller count_users_path, interval: 10_000 %>
|
60
|
+
```
|
61
|
+
|
62
|
+
Do not start polling automatically. You can later start polling [interacting directly with the plugin](#plugin-interaction). _Default: true_
|
63
|
+
|
64
|
+
```erb
|
65
|
+
<%= easy_poller count_users_path, poll: false %>
|
66
|
+
```
|
67
|
+
|
68
|
+
Add HTML properties to the container
|
69
|
+
|
70
|
+
```erb
|
71
|
+
<%= easy_poller count_users_path, id: 'users-count-poller', class: 'user poller' %>
|
72
|
+
```
|
73
|
+
|
74
|
+
### Plugin interaction
|
75
|
+
|
76
|
+
Stop polling
|
77
|
+
|
78
|
+
```js
|
79
|
+
$("#users-count-poller").easyPoller("stop");
|
80
|
+
```
|
81
|
+
|
82
|
+
Start polling
|
83
|
+
|
84
|
+
```js
|
85
|
+
$("#users-count-poller").easyPoller("start");
|
86
|
+
```
|
87
|
+
|
88
|
+
Start all pollers
|
89
|
+
|
90
|
+
```js
|
91
|
+
$(".easy_poller").easyPoller("start");
|
92
|
+
```
|
93
|
+
|
94
|
+
Set options
|
95
|
+
|
96
|
+
```js
|
97
|
+
$("#users-count-poller").easyPoller("options", "loadingMessage", "Please wait...");
|
98
|
+
```
|
99
|
+
|
100
|
+
## Development
|
101
|
+
|
102
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
103
|
+
|
104
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fabrouy/easy_poller.
|
109
|
+
Feel free to suggest/add new features.
|
110
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
$.widget("nmk.easyPoller", {
|
2
|
+
|
3
|
+
options: {
|
4
|
+
loadingMessage: 'Loading...'
|
5
|
+
},
|
6
|
+
|
7
|
+
_create: function() {
|
8
|
+
if (this.element.data('poll') === true) {
|
9
|
+
this.start();
|
10
|
+
}
|
11
|
+
},
|
12
|
+
|
13
|
+
start: function() {
|
14
|
+
this.element.html(this.options.loadingMessage);
|
15
|
+
var $wid = this;
|
16
|
+
|
17
|
+
var $element = this.element;
|
18
|
+
|
19
|
+
$wid._request($element);
|
20
|
+
|
21
|
+
intId = setInterval(function() {
|
22
|
+
$wid._request($element);
|
23
|
+
}, this._interval());
|
24
|
+
|
25
|
+
this.intervalId = intId;
|
26
|
+
},
|
27
|
+
|
28
|
+
stop: function() {
|
29
|
+
clearInterval(this.intervalId);
|
30
|
+
},
|
31
|
+
|
32
|
+
_url: function() {
|
33
|
+
return this.element.data("url");
|
34
|
+
},
|
35
|
+
|
36
|
+
_interval: function() {
|
37
|
+
return this.element.data("interval");
|
38
|
+
},
|
39
|
+
|
40
|
+
_replace: function(content) {
|
41
|
+
return this.element.html(content);
|
42
|
+
},
|
43
|
+
|
44
|
+
_request: function(element) {
|
45
|
+
var $widget = this;
|
46
|
+
|
47
|
+
$.ajax({
|
48
|
+
url: this._url()
|
49
|
+
}).always((function(_this) {
|
50
|
+
return function(data, textStatus, jqXHR) {
|
51
|
+
if (jqXHR.status === 200) {
|
52
|
+
return $widget._replace(data);
|
53
|
+
}
|
54
|
+
};
|
55
|
+
})(this));
|
56
|
+
},
|
57
|
+
|
58
|
+
_setOption: function(key, value) {
|
59
|
+
this.options[key] = value;
|
60
|
+
}
|
61
|
+
|
62
|
+
});
|
63
|
+
|
64
|
+
$(function() {
|
65
|
+
return $(".easy_poller").each(function(i, div) {
|
66
|
+
return $(div).easyPoller();
|
67
|
+
});
|
68
|
+
});
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "easy_poller"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/easy_poller.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'easy_poller/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "easy_poller"
|
8
|
+
spec.version = EasyPoller::VERSION
|
9
|
+
spec.authors = ["Fabricio Solanes"]
|
10
|
+
spec.email = ["fabrouy@gmail.com"]
|
11
|
+
spec.summary = %q{Very easy to use helper to poll for changes.}
|
12
|
+
spec.description = %q{Very easy to use helper to poll for changes. Just give it an URL and voila.}
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
# if spec.respond_to?(:metadata)
|
18
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
19
|
+
# else
|
20
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
21
|
+
# "public gem pushes."
|
22
|
+
# end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
32
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
33
|
+
spec.add_development_dependency "minitest"
|
34
|
+
end
|
data/lib/easy_poller.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module EasyPoller
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
initializer "precompile", group: :all do |app|
|
4
|
+
if app.config.respond_to?(:assets)
|
5
|
+
if defined?(Sprockets) && Gem::Version.new(Sprockets::VERSION) >= Gem::Version.new("4.0.0.beta1")
|
6
|
+
app.config.assets.precompile << "easy_poller.js"
|
7
|
+
else
|
8
|
+
# use a proc instead of a string
|
9
|
+
app.config.assets.precompile << proc { |path| path == "easy_poller.js" }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
initializer "helper" do
|
15
|
+
ActiveSupport.on_load(:action_view) do
|
16
|
+
include Helper
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module EasyPoller
|
2
|
+
module Helper
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
def easy_poller(url, poll: true, interval: 3000, **html_options)
|
6
|
+
klasses = (html_options.delete(:class) || '').split.unshift('easy_poller').join(' ')
|
7
|
+
|
8
|
+
parsed_html_opts = ''
|
9
|
+
html_options.each do |key, array|
|
10
|
+
parsed_html_opts << " #{key}=\"#{array}\""
|
11
|
+
end
|
12
|
+
parsed_html_opts.rstrip!
|
13
|
+
|
14
|
+
replace = {
|
15
|
+
html_options: parsed_html_opts,
|
16
|
+
klasses: ERB::Util.html_escape(klasses),
|
17
|
+
poll: ERB::Util.html_escape(poll),
|
18
|
+
url: ERB::Util.html_escape(url),
|
19
|
+
interval: ERB::Util.html_escape(interval)
|
20
|
+
}
|
21
|
+
|
22
|
+
html = %(<div%{html_options} class="%{klasses}" data-poll="%{poll}" data-interval="%{interval}" data-url="%{url}"></div>) % replace
|
23
|
+
|
24
|
+
html.respond_to?(:html_safe) ? html.html_safe : html
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_poller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fabricio Solanes
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Very easy to use helper to poll for changes. Just give it an URL and
|
56
|
+
voila.
|
57
|
+
email:
|
58
|
+
- fabrouy@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- app/assets/javascripts/easy_poller.js
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- easy_poller.gemspec
|
72
|
+
- lib/easy_poller.rb
|
73
|
+
- lib/easy_poller/engine.rb
|
74
|
+
- lib/easy_poller/helper.rb
|
75
|
+
- lib/easy_poller/rails.rb
|
76
|
+
- lib/easy_poller/sinatra.rb
|
77
|
+
- lib/easy_poller/version.rb
|
78
|
+
homepage:
|
79
|
+
licenses:
|
80
|
+
- MIT
|
81
|
+
metadata: {}
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.4.8
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Very easy to use helper to poll for changes.
|
102
|
+
test_files: []
|