polling_request 0.0.1
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.
- data/README.md +100 -0
- data/lib/polling_request.rb +9 -0
- data/src/polling_request.coffee +63 -0
- data/src/polling_request.js +72 -0
- metadata +65 -0
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# Polling Request [](http://travis-ci.org/jch/polling_request)
|
2
|
+
|
3
|
+
jQuery helper object for AJAX polling a [progress-aware endpoint](#progress-aware-endpoint).
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```coffeescript
|
8
|
+
req = new PollingRequest
|
9
|
+
url: "/states.csv"
|
10
|
+
interval: 1000 # polling interval in milliseconds
|
11
|
+
progress: (n)->
|
12
|
+
console.log "Progress: #{n} percent"
|
13
|
+
success: (res)->
|
14
|
+
console.log "Response body: #{res}"
|
15
|
+
error: (status, response)->
|
16
|
+
console.log("Status: #{status}, Body: #{response}")
|
17
|
+
complete: (status, response)->
|
18
|
+
console.log("This is called after everything else")
|
19
|
+
|
20
|
+
req.status # 'pending'
|
21
|
+
req.start()
|
22
|
+
req.status # 'running'
|
23
|
+
req.progress # 0
|
24
|
+
|
25
|
+
# sometime later
|
26
|
+
req.status # 'running'
|
27
|
+
req.progress # 50
|
28
|
+
|
29
|
+
# much later
|
30
|
+
req.status # 'success'
|
31
|
+
req.progress # 100
|
32
|
+
|
33
|
+
# example of setting a hard timeout
|
34
|
+
setTimeout ->
|
35
|
+
req.stop()
|
36
|
+
, 5000
|
37
|
+
```
|
38
|
+
|
39
|
+
## Progress Aware Endpoint
|
40
|
+
|
41
|
+
PollingRequests will have a header `X-POLLING-REQUEST` set. It expects an HTTP
|
42
|
+
endpoint to respond with a [202 Accepted][202] status code when there is more
|
43
|
+
processing to be done. The JSON response body can optionally include a
|
44
|
+
`progress` property whose value is an integer 0..100 inclusive to indicate the
|
45
|
+
percentage of processing completed.
|
46
|
+
|
47
|
+
```bash
|
48
|
+
$ curl -i http://localhost/endpoint
|
49
|
+
HTTP/1.1 202 Accepted
|
50
|
+
{ progress: 0 }
|
51
|
+
|
52
|
+
$ curl -i http://localhost/endpoint
|
53
|
+
HTTP/1.1 202 Accepted
|
54
|
+
{ progress: 50 }
|
55
|
+
|
56
|
+
$ curl -i http://localhost/endpoint
|
57
|
+
HTTP/1.1 200 Success
|
58
|
+
Content-Type: text/plain
|
59
|
+
Successful responses can be any content type
|
60
|
+
```
|
61
|
+
|
62
|
+
[202]: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.3
|
63
|
+
|
64
|
+
## Rails
|
65
|
+
|
66
|
+
To access PollingRequest from the asset pipeline, add the following to your
|
67
|
+
Gemfile:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
gem 'polling_request'
|
71
|
+
```
|
72
|
+
|
73
|
+
And this to your asset manifest:
|
74
|
+
|
75
|
+
```javascript
|
76
|
+
//= require polling_request
|
77
|
+
```
|
78
|
+
|
79
|
+
## Development
|
80
|
+
|
81
|
+
Tests are written with QUnit and can be run in a browser by opening
|
82
|
+
`test/test.html`. Continuous integration is run with phantomjs:
|
83
|
+
|
84
|
+
```bash
|
85
|
+
$ phantomjs test/runner.js test/test.html
|
86
|
+
```
|
87
|
+
|
88
|
+
## Wish List
|
89
|
+
|
90
|
+
* Better docs for building and installing project
|
91
|
+
* Bower compatibility
|
92
|
+
* Travis CI
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
1. [Fork it](https://help.github.com/articles/fork-a-repo)
|
97
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
98
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
99
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
100
|
+
5. Create new [Pull Request](https://help.github.com/articles/using-pull-requests)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# Options:
|
2
|
+
#
|
3
|
+
# url: progress aware HTTP endpoint. See README.md
|
4
|
+
# interval: to poll in milliseconds
|
5
|
+
# success: callback for response body
|
6
|
+
# progress: callback for integer percentage
|
7
|
+
# error: callback with status code and response body
|
8
|
+
# stopped: callback after request stops
|
9
|
+
class PollingRequest
|
10
|
+
constructor: (@options) ->
|
11
|
+
@progress = 0
|
12
|
+
@status = 'pending'
|
13
|
+
@options.interval or= 1000
|
14
|
+
@options.success or= ->
|
15
|
+
@options.progress or= ->
|
16
|
+
@options.error or= ->
|
17
|
+
@options.stopped or= ->
|
18
|
+
|
19
|
+
# Public: schedule polling by `options.interval`
|
20
|
+
start: ->
|
21
|
+
return if @timer
|
22
|
+
@timer = setInterval =>
|
23
|
+
@_run()
|
24
|
+
, @options.interval
|
25
|
+
|
26
|
+
# Public: stop polling. Status becomes 'stopped'
|
27
|
+
stop: ->
|
28
|
+
@status = 'stopped'
|
29
|
+
clearInterval(@timer)
|
30
|
+
@timer = null
|
31
|
+
@options.stopped()
|
32
|
+
|
33
|
+
# Public: one of 'pending', 'running', 'stopped'
|
34
|
+
status: ->
|
35
|
+
@status
|
36
|
+
|
37
|
+
# Public: integer 0..100 inclusive indicating percentage complete
|
38
|
+
progress: ->
|
39
|
+
@progress
|
40
|
+
|
41
|
+
# Private: runs the actual ajax request
|
42
|
+
_run: ->
|
43
|
+
$.ajax
|
44
|
+
url: @options.url
|
45
|
+
beforeSend: (jqXHR, settings) =>
|
46
|
+
@status = 'running'
|
47
|
+
statusCode:
|
48
|
+
# The request has succeeded.
|
49
|
+
200: (data, textStatus, jqXHR) =>
|
50
|
+
@progress = 100
|
51
|
+
@options.success(data)
|
52
|
+
@stop()
|
53
|
+
|
54
|
+
# The request has been accepted for processing, but the processing has
|
55
|
+
# not been completed.
|
56
|
+
202: (data, textStatus, jqXHR) =>
|
57
|
+
@progress = data.progress or 0
|
58
|
+
@options.progress(@progress)
|
59
|
+
error: (jqXHR, textStatus, errorThrown) =>
|
60
|
+
@options.error(jqXHR.status, jqXHR.responseText)
|
61
|
+
@stop()
|
62
|
+
|
63
|
+
(exports ? this).PollingRequest = PollingRequest
|
@@ -0,0 +1,72 @@
|
|
1
|
+
// Generated by CoffeeScript 1.6.3
|
2
|
+
(function() {
|
3
|
+
var PollingRequest;
|
4
|
+
|
5
|
+
PollingRequest = (function() {
|
6
|
+
function PollingRequest(options) {
|
7
|
+
var _base, _base1, _base2, _base3;
|
8
|
+
this.options = options;
|
9
|
+
this.progress = 0;
|
10
|
+
this.status = 'pending';
|
11
|
+
(_base = this.options).interval || (_base.interval = 1000);
|
12
|
+
(_base1 = this.options).success || (_base1.success = function() {});
|
13
|
+
(_base2 = this.options).progress || (_base2.progress = function() {});
|
14
|
+
(_base3 = this.options).error || (_base3.error = function() {});
|
15
|
+
}
|
16
|
+
|
17
|
+
PollingRequest.prototype.start = function() {
|
18
|
+
var _this = this;
|
19
|
+
if (this.timer) {
|
20
|
+
return;
|
21
|
+
}
|
22
|
+
return this.timer = setInterval(function() {
|
23
|
+
return _this._run();
|
24
|
+
}, this.options.interval);
|
25
|
+
};
|
26
|
+
|
27
|
+
PollingRequest.prototype.stop = function() {
|
28
|
+
this.status = 'stopped';
|
29
|
+
clearInterval(this.timer);
|
30
|
+
return this.timer = null;
|
31
|
+
};
|
32
|
+
|
33
|
+
PollingRequest.prototype.status = function() {
|
34
|
+
return this.status;
|
35
|
+
};
|
36
|
+
|
37
|
+
PollingRequest.prototype.progress = function() {
|
38
|
+
return this.progress;
|
39
|
+
};
|
40
|
+
|
41
|
+
PollingRequest.prototype._run = function() {
|
42
|
+
var _this = this;
|
43
|
+
return $.ajax({
|
44
|
+
url: this.options.url,
|
45
|
+
beforeSend: function(jqXHR, settings) {
|
46
|
+
return _this.status = 'running';
|
47
|
+
},
|
48
|
+
statusCode: {
|
49
|
+
200: function(data, textStatus, jqXHR) {
|
50
|
+
_this.progress = 100;
|
51
|
+
_this.options.success(data);
|
52
|
+
return _this.stop();
|
53
|
+
},
|
54
|
+
202: function(data, textStatus, jqXHR) {
|
55
|
+
_this.progress = data.progress || 0;
|
56
|
+
return _this.options.progress(data.progress);
|
57
|
+
}
|
58
|
+
},
|
59
|
+
error: function(jqXHR, textStatus, errorThrown) {
|
60
|
+
_this.options.error(jqXHR.status, jqXHR.responseText);
|
61
|
+
return _this.stop();
|
62
|
+
}
|
63
|
+
});
|
64
|
+
};
|
65
|
+
|
66
|
+
return PollingRequest;
|
67
|
+
|
68
|
+
})();
|
69
|
+
|
70
|
+
(typeof exports !== "undefined" && exports !== null ? exports : this).PollingRequest = PollingRequest;
|
71
|
+
|
72
|
+
}).call(this);
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polling_request
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jerry Cheung
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
description: This gem adds polling_request Javascript assets to Rail's asset pipeline
|
31
|
+
email:
|
32
|
+
- jch@whatcodecraves.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- lib/polling_request.rb
|
38
|
+
- src/polling_request.coffee
|
39
|
+
- src/polling_request.js
|
40
|
+
- README.md
|
41
|
+
homepage: https://github.com/jch/polling_request
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.23
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Railtie for progress aware AJAX polling
|
65
|
+
test_files: []
|