ki 0.4.10 → 0.4.11
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 +4 -4
- data/Gemfile.lock +16 -8
- data/MIDDLEWARE.md +27 -7
- data/README.md +45 -2
- data/REALTIME.md +48 -0
- data/TODO.md +19 -0
- data/ki.gemspec +1 -0
- data/lib/ki.rb +5 -0
- data/lib/ki/base_request.rb +7 -0
- data/lib/ki/channel_manager.rb +58 -0
- data/lib/ki/ki.rb +1 -0
- data/lib/ki/ki_cli.rb +1 -1
- data/lib/ki/ki_config.rb +18 -2
- data/lib/ki/middleware/api_handler.rb +11 -16
- data/lib/ki/middleware/helpers/redirect_to_helper.rb +13 -0
- data/lib/ki/middleware/realtime.rb +81 -0
- data/lib/ki/model.rb +6 -4
- data/lib/ki/modules/restrictions.rb +4 -7
- data/lib/ki/orm.rb +38 -2
- data/lib/ki/utils/redirect_to_helper.rb +0 -0
- data/lib/ki/version.rb +1 -1
- data/spec/examples/json.northpole.ro/.bowerrc +3 -0
- data/spec/examples/json.northpole.ro/Gemfile +1 -0
- data/spec/examples/json.northpole.ro/bower.json +17 -0
- data/spec/examples/json.northpole.ro/config.ru +3 -0
- data/spec/examples/json.northpole.ro/config.yml +4 -0
- data/spec/examples/json.northpole.ro/config.yml.backup +18 -0
- data/spec/examples/json.northpole.ro/config/deploy.rb +2 -1
- data/spec/examples/json.northpole.ro/config/deploy/production.rb +4 -38
- data/spec/examples/json.northpole.ro/public/app/.bowerrc +3 -0
- data/spec/examples/json.northpole.ro/public/app/.gitignore +9 -0
- data/spec/examples/json.northpole.ro/public/app/.jshintrc +13 -0
- data/spec/examples/json.northpole.ro/public/app/.travis.yml +14 -0
- data/spec/examples/json.northpole.ro/public/app/LICENSE +22 -0
- data/spec/examples/json.northpole.ro/public/app/README.md +82 -0
- data/spec/examples/json.northpole.ro/public/app/bower.json +20 -0
- data/spec/examples/json.northpole.ro/public/app/e2e/pages/ContactUser.js +22 -0
- data/spec/examples/json.northpole.ro/public/app/e2e/pages/UserDetails.js +11 -0
- data/spec/examples/json.northpole.ro/public/app/e2e/pages/UserList.js +12 -0
- data/spec/examples/json.northpole.ro/public/app/e2e/protractor.conf.js +26 -0
- data/spec/examples/json.northpole.ro/public/app/e2e/scenarios/users.js +34 -0
- data/spec/examples/json.northpole.ro/public/app/gulpfile.js +104 -0
- data/spec/examples/json.northpole.ro/public/app/karma.conf.js +35 -0
- data/spec/examples/json.northpole.ro/public/app/package.json +30 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/avatar-1.svg +11 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/avatar-4.svg +16 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/avatars.svg +244 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/google_plus.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/hangouts.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/ic_fullscreen_48px.svg +4 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/ic_fullscreen_exit_48px.svg +4 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/ic_music_note_48px.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/ic_note_add_48px.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/ic_view_list_48px.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/icon.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/mail.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/manggo.svg +1095 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/menu.svg +4 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/phone.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/share.svg +3 -0
- data/spec/examples/json.northpole.ro/public/app/src/assets/svg/twitter.svg +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/css/app.css +138 -0
- data/spec/examples/json.northpole.ro/public/app/src/css/app.css.map +7 -0
- data/spec/examples/json.northpole.ro/public/app/src/css/app.sass +145 -0
- data/spec/examples/json.northpole.ro/public/app/src/index.html +157 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/MainController.coffee +167 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/app.coffee +58 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/blobs/BlobsController.coffee +115 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/blobs/blobs.html +48 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/tutorial/tutorial.html +15 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/users/UserService.coffee +12 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/users/Users.coffee +1 -0
- data/spec/examples/json.northpole.ro/public/app/src/js/users/users.html +8 -0
- data/spec/examples/json.northpole.ro/public/javascripts/jnorthpole.coffee +40 -7
- data/spec/examples/json.northpole.ro/public/javascripts/realtime.coffee +28 -0
- data/spec/examples/json.northpole.ro/public/{javascripts/music → music}/angular-youtube-embed.js +0 -0
- data/spec/examples/json.northpole.ro/public/music/index.html +126 -0
- data/spec/examples/json.northpole.ro/public/music/music.coffee +99 -0
- data/spec/examples/json.northpole.ro/public/music/music.sass +63 -0
- data/spec/examples/json.northpole.ro/public/stylesheets/app.sass +3 -0
- data/spec/examples/json.northpole.ro/views/faq.haml +7 -0
- data/spec/examples/json.northpole.ro/views/layout.haml +1 -0
- data/spec/examples/json.northpole.ro/views/music.haml +2 -0
- data/spec/examples/json.northpole.ro/views/websocket.haml +7 -0
- data/spec/lib/ki/channel_manager_spec.rb +82 -0
- data/spec/lib/ki/helpers_spec.rb +11 -0
- data/spec/lib/ki/ki_config_spec.rb +28 -0
- data/spec/lib/ki/middleware/admin_generator_spec.rb +8 -0
- data/spec/lib/ki/middleware/init_middleware_spec.rb +21 -0
- data/spec/lib/ki/middleware/realtime_spec.rb +96 -0
- data/spec/lib/ki/model_spec.rb +28 -7
- data/spec/lib/ki/modules/model_helper_spec.rb +31 -0
- data/spec/lib/ki/orm_spec.rb +26 -0
- metadata +211 -9
- data/spec/examples/json.northpole.ro/public/javascripts/docs.min.js +0 -16
- data/spec/examples/json.northpole.ro/views/awsum.haml +0 -108
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 027faaedcab27f0fd3f3d65e878da0dfd98e74da
|
4
|
+
data.tar.gz: bc9e0f3ec9dd2c17fa4b238b829bd50afcdce5f3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa073bd56faec752bea84241acd3602b85c010628d10f393c006d96337ed9952d064af70991b69b6a40108df3794433ed9ecc8ac6f469eb7c6682b0b5e6d538c
|
7
|
+
data.tar.gz: 97eebe5a6b2e5b0e03d029e44dbe8bcdeacb9f16a7d721dd2644ff74be97fa67ab81dac38f19c3e9f057d442e6d57ca531281c136294d7744c394896fc243aed
|
data/Gemfile.lock
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ki (0.4.
|
4
|
+
ki (0.4.11)
|
5
5
|
bson_ext
|
6
6
|
bundler (~> 1.5)
|
7
7
|
coffee-script
|
8
|
+
faye-websocket
|
8
9
|
haml
|
9
10
|
mongo (= 1.9.2)
|
10
11
|
rack
|
@@ -33,10 +34,14 @@ GEM
|
|
33
34
|
coffee-script (2.4.1)
|
34
35
|
coffee-script-source
|
35
36
|
execjs
|
36
|
-
coffee-script-source (1.
|
37
|
+
coffee-script-source (1.10.0)
|
37
38
|
diff-lcs (1.2.5)
|
38
39
|
docile (1.1.5)
|
39
|
-
|
40
|
+
eventmachine (1.2.0.1)
|
41
|
+
execjs (2.6.0)
|
42
|
+
faye-websocket (0.10.3)
|
43
|
+
eventmachine (>= 0.12.0)
|
44
|
+
websocket-driver (>= 0.5.1)
|
40
45
|
ffi (1.9.3)
|
41
46
|
formatador (0.2.4)
|
42
47
|
guard (2.5.1)
|
@@ -51,7 +56,7 @@ GEM
|
|
51
56
|
guard-rubocop (1.2.0)
|
52
57
|
guard (~> 2.0)
|
53
58
|
rubocop (~> 0.20)
|
54
|
-
haml (4.0.
|
59
|
+
haml (4.0.7)
|
55
60
|
tilt
|
56
61
|
listen (2.7.1)
|
57
62
|
celluloid (>= 0.15.2)
|
@@ -73,7 +78,7 @@ GEM
|
|
73
78
|
slop (~> 3.4)
|
74
79
|
rack (1.5.2)
|
75
80
|
rack-cors (0.4.0)
|
76
|
-
rack-parser (0.
|
81
|
+
rack-parser (0.7.0)
|
77
82
|
rack
|
78
83
|
rack-test (0.6.2)
|
79
84
|
rack (>= 1.0)
|
@@ -97,7 +102,7 @@ GEM
|
|
97
102
|
rainbow (>= 1.99.1, < 3.0)
|
98
103
|
ruby-progressbar (~> 1.4)
|
99
104
|
ruby-progressbar (1.7.5)
|
100
|
-
sass (3.4.
|
105
|
+
sass (3.4.22)
|
101
106
|
simplecov (0.9.2)
|
102
107
|
docile (~> 1.1.0)
|
103
108
|
multi_json (~> 1.0)
|
@@ -105,8 +110,11 @@ GEM
|
|
105
110
|
simplecov-html (0.9.0)
|
106
111
|
slop (3.5.0)
|
107
112
|
thor (0.18.1)
|
108
|
-
tilt (2.0.
|
113
|
+
tilt (2.0.2)
|
109
114
|
timers (1.1.0)
|
115
|
+
websocket-driver (0.6.3)
|
116
|
+
websocket-extensions (>= 0.1.0)
|
117
|
+
websocket-extensions (0.1.2)
|
110
118
|
|
111
119
|
PLATFORMS
|
112
120
|
ruby
|
@@ -121,4 +129,4 @@ DEPENDENCIES
|
|
121
129
|
rspec
|
122
130
|
|
123
131
|
BUNDLED WITH
|
124
|
-
1.10.
|
132
|
+
1.10.6
|
data/MIDDLEWARE.md
CHANGED
@@ -5,6 +5,8 @@ Rack handles everything™
|
|
5
5
|
|
6
6
|
## Middleware
|
7
7
|
|
8
|
+
It is configured in the config.yml of the application.
|
9
|
+
|
8
10
|
The default enabled middleware list:
|
9
11
|
|
10
12
|
* ApiHandler
|
@@ -36,10 +38,28 @@ Similar to CoffeeCompiler, compiles haml to html.
|
|
36
38
|
|
37
39
|
Serves static files from the *public/* folder.
|
38
40
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
### example middleware configuration
|
42
|
+
|
43
|
+
```yml
|
44
|
+
development:
|
45
|
+
database:
|
46
|
+
name: base_development
|
47
|
+
host: 127.0.0.1
|
48
|
+
port: 27017
|
49
|
+
add_middleware: ['Realtime', 'DocGenerator', 'AdminInterfaceGenerator']
|
50
|
+
rm_middleware: ['SassCompiler']
|
51
|
+
|
52
|
+
test:
|
53
|
+
database:
|
54
|
+
name: np_test
|
55
|
+
host: 127.0.0.1
|
56
|
+
port: 27017
|
57
|
+
|
58
|
+
production:
|
59
|
+
middleware: ['ApiHandler', 'PublicFileServer']
|
60
|
+
add_middleware: ['Realtime', 'DocGenerator', 'AdminInterfaceGenerator']
|
61
|
+
database:
|
62
|
+
name: np
|
63
|
+
host: 127.0.0.1
|
64
|
+
port: 27017
|
65
|
+
```
|
data/README.md
CHANGED
@@ -15,6 +15,7 @@ and provides a fullblown REST api on top.
|
|
15
15
|
- [Install](#install)
|
16
16
|
- [Getting started](#getting-started)
|
17
17
|
- [Create a new app](#create-a-new-app)
|
18
|
+
- [Realtime](#realtime)
|
18
19
|
- [Adding a view](#adding-a-view)
|
19
20
|
- [Adding assets](#adding-assets)
|
20
21
|
- [View layout](#view-layout)
|
@@ -27,12 +28,14 @@ and provides a fullblown REST api on top.
|
|
27
28
|
- [Get advanced queries](#advanced-queries)
|
28
29
|
- [Update](#update)
|
29
30
|
- [Delete](#delete)
|
31
|
+
- [Headers](#headers)
|
30
32
|
- [Required attributes](#required-attributes)
|
31
33
|
- [Unique attributes](#unique-attributes)
|
32
34
|
- [Restricting resource requests](#restricting-resource-requests)
|
33
35
|
- [Before/after callbacks](#beforeafter-callbacks)
|
34
36
|
- [Accessing the json object within the callback](#accessing-the-json-object-within-the-callback)
|
35
37
|
- [Exceptions](#exceptions)
|
38
|
+
- [Magic](#magic)
|
36
39
|
- [Deploy](#deploy)
|
37
40
|
- [Heroku deployment](#heroku-deployment)
|
38
41
|
- [CORS enabled](#cors-enabled)
|
@@ -81,7 +84,7 @@ development: the dreaded TODO app.
|
|
81
84
|
View the [code](https://github.com/mess110/ki/tree/master/spec/examples/todo) for
|
82
85
|
the final webapp.
|
83
86
|
|
84
|
-
###
|
87
|
+
### Create a new app
|
85
88
|
|
86
89
|
```shell
|
87
90
|
ki new todo
|
@@ -119,6 +122,12 @@ irb session with your application context already loaded.
|
|
119
122
|
ki console
|
120
123
|
```
|
121
124
|
|
125
|
+
### Realtime
|
126
|
+
|
127
|
+
ki supports realtime communication though [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API)
|
128
|
+
|
129
|
+
To learn how to use the realtime api, [read this](REALTIME.md).
|
130
|
+
|
122
131
|
### Adding a view
|
123
132
|
|
124
133
|
A view is a html page. By default, a view called 'index.haml' is created for
|
@@ -252,6 +261,17 @@ OR Query
|
|
252
261
|
curl -k -X GET -d '{"$or": [{"category": "music"}, {"category": "band"}]}' "https://json.northpole.ro/storage.json?api_key=secret&secret=secret"
|
253
262
|
```
|
254
263
|
|
264
|
+
To limit the number of results use a param called *__limit* and the value desired.
|
265
|
+
For example, the request below will limit itself to 10 results.
|
266
|
+
|
267
|
+
```
|
268
|
+
curl -k -X GET -d '{"__limit": 10}' "https://json.northpole.ro/storage.json?api_key=guest&secret=guest"
|
269
|
+
```
|
270
|
+
|
271
|
+
To sort the results, use a param called *__sort* and the key value desired. Example
|
272
|
+
|
273
|
+
curl -k -X GET -d '{"__sort": { "name": "desc" }}' "https://json.northpole.ro/storage.json?api_key=guest&secret=guest"
|
274
|
+
|
255
275
|
#### Update
|
256
276
|
|
257
277
|
```shell
|
@@ -264,6 +284,18 @@ curl -X PATCH -d '{"id": "ITEM_ID", "title": "finish the todo tutorial"}' http:/
|
|
264
284
|
curl -X DELETE -d http://localhost:9292/todo.json?id=ITEM_ID
|
265
285
|
```
|
266
286
|
|
287
|
+
### Headers
|
288
|
+
|
289
|
+
To access headers, simply call `@req.headers`. In this example, the headers are returned by the API.
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
class Headers < Ki::Model
|
293
|
+
def after_find
|
294
|
+
@result = @req.headers
|
295
|
+
end
|
296
|
+
end
|
297
|
+
```
|
298
|
+
|
267
299
|
### Required attributes
|
268
300
|
|
269
301
|
You can add mandatory attributes on resources with the *requires* method. It
|
@@ -350,6 +382,17 @@ class Todo < Ki::Model
|
|
350
382
|
end
|
351
383
|
```
|
352
384
|
|
385
|
+
### Magic
|
386
|
+
|
387
|
+
#### redirect_to param
|
388
|
+
|
389
|
+
By setting the param `redirect_to`, you can set an endpoint on where to redirect
|
390
|
+
once the request is completed.
|
391
|
+
|
392
|
+
```
|
393
|
+
http://localhost:1337/storage.json?api_key=guest&secret=guest&redirect_to=/examples
|
394
|
+
```
|
395
|
+
|
353
396
|
## Deploy
|
354
397
|
|
355
398
|
It has a *config.ru* file. Ki is based on *rack*. You can deploy anywhere (ex:
|
@@ -389,4 +432,4 @@ and access */instadmin*.
|
|
389
432
|
|
390
433
|
## Tasks
|
391
434
|
|
392
|
-
Ki offers a simple task framework. [More info here](https://github.com/mess110/ki/tree/master/spec/examples/tasks-
|
435
|
+
Ki offers a simple task framework. [More info here](https://github.com/mess110/ki/tree/master/spec/examples/tasks-example/).
|
data/REALTIME.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Realtime
|
2
|
+
|
3
|
+
The following webservers are supported:
|
4
|
+
|
5
|
+
* Goliath
|
6
|
+
* Phusion Passenger >= 4.0 with nginx >= 1.4
|
7
|
+
* Puma >= 2.0
|
8
|
+
* Rainbows
|
9
|
+
* Thin
|
10
|
+
|
11
|
+
## Requrirements
|
12
|
+
|
13
|
+
* Load the adapter for your webserver in config.ru after 'require' (example: Faye::WebSocket.load_adapter('thin'))
|
14
|
+
* You need to run in production mode (example: rackup -p 1337 -E production)
|
15
|
+
* add the Realtime middlware in config.yml (example: add_middleware: 'Realtime')
|
16
|
+
|
17
|
+
## Documentation
|
18
|
+
|
19
|
+
Ki realtime offers channels to which a user subscribes to. Once a user is subscribed
|
20
|
+
to a channel he/she will receive messages from that channel. Anybody can publish
|
21
|
+
messages in a channel.
|
22
|
+
|
23
|
+
Upon connecting, the user will receive a unique id.
|
24
|
+
|
25
|
+
Using WebSockets, connect to the /realtime endpoint:
|
26
|
+
|
27
|
+
```javascript
|
28
|
+
// connect
|
29
|
+
var socket = new WebSocket("ws://localhost:1337/realtime");
|
30
|
+
socket.onclose = function() { console.log('socket closed'); }
|
31
|
+
socket.onmessage = function(m) {
|
32
|
+
json = JSON.parse(m)
|
33
|
+
console.log(json);
|
34
|
+
}
|
35
|
+
|
36
|
+
send = function (hash) {
|
37
|
+
socket.send(JSON.stringify(hash))
|
38
|
+
}
|
39
|
+
|
40
|
+
// Subscribe to channel events
|
41
|
+
send({type: 'subscribe', channel_name: 'my-channel'})
|
42
|
+
|
43
|
+
// Unsubscribe from channel events
|
44
|
+
send({type: 'unsubscribe', channel_name: 'my-channel'})
|
45
|
+
|
46
|
+
// Send a message to a channel
|
47
|
+
send({type: 'publish', channel_name: 'my-channel', text: 'asd', custom_attribute: [1, 'text']})
|
48
|
+
```
|
data/TODO.md
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# deploy new version to rubygems
|
2
|
+
# add logging
|
3
|
+
# respect RESTful routing /foo/:id is the only thing missing
|
4
|
+
|
5
|
+
if param is set to redirect_to, ki will redirect
|
6
|
+
|
7
|
+
class Redirector < Ki::Model
|
8
|
+
def after_find
|
9
|
+
@params['redirect_to'] = 'http://localhost:1337/'
|
10
|
+
end
|
11
|
+
end>
|
12
|
+
|
13
|
+
how to read headers
|
14
|
+
|
15
|
+
class Headers < Ki::Model
|
16
|
+
def after_find
|
17
|
+
@result = @req.headers
|
18
|
+
end
|
19
|
+
end
|
data/ki.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_runtime_dependency 'mongo', '1.9.2'
|
29
29
|
spec.add_runtime_dependency 'bson_ext'
|
30
30
|
spec.add_runtime_dependency 'rack-cors'
|
31
|
+
spec.add_runtime_dependency 'faye-websocket'
|
31
32
|
|
32
33
|
spec.add_development_dependency 'rake'
|
33
34
|
spec.add_development_dependency 'rack-test'
|
data/lib/ki.rb
CHANGED
@@ -10,6 +10,8 @@ require 'haml'
|
|
10
10
|
require 'sass'
|
11
11
|
require 'coffee-script'
|
12
12
|
require 'mongo'
|
13
|
+
require 'faye/websocket'
|
14
|
+
require 'eventmachine'
|
13
15
|
|
14
16
|
# code
|
15
17
|
require 'ki/utils/api_error'
|
@@ -24,6 +26,7 @@ require 'ki/modules/model_helper'
|
|
24
26
|
require 'ki/middleware/helpers/format_of_helper'
|
25
27
|
require 'ki/middleware/helpers/public_file_helper'
|
26
28
|
require 'ki/middleware/helpers/haml_compiler_helper'
|
29
|
+
require 'ki/middleware/helpers/redirect_to_helper'
|
27
30
|
require 'ki/middleware/helpers/view_helper'
|
28
31
|
|
29
32
|
require 'ki/middleware/base_middleware'
|
@@ -35,11 +38,13 @@ require 'ki/middleware/haml_compiler'
|
|
35
38
|
require 'ki/middleware/coffee_compiler'
|
36
39
|
require 'ki/middleware/doc_generator'
|
37
40
|
require 'ki/middleware/admin_interface_generator'
|
41
|
+
require 'ki/middleware/realtime'
|
38
42
|
|
39
43
|
require 'ki/ki_config'
|
40
44
|
require 'ki/helpers'
|
41
45
|
require 'ki/orm'
|
42
46
|
require 'ki/model'
|
47
|
+
require 'ki/channel_manager'
|
43
48
|
require 'ki/base_request'
|
44
49
|
require 'ki/ki_app'
|
45
50
|
|
data/lib/ki/base_request.rb
CHANGED
@@ -18,6 +18,13 @@ module Ki
|
|
18
18
|
content_type == 'application/json' || format_of(path) == 'json'
|
19
19
|
end
|
20
20
|
|
21
|
+
def headers
|
22
|
+
Hash[*env.select { |k, _v| k.start_with? 'HTTP_' }
|
23
|
+
.collect { |k, v| [k.sub(/^HTTP_/, ''), v] }
|
24
|
+
.sort
|
25
|
+
.flatten]
|
26
|
+
end
|
27
|
+
|
21
28
|
def to_ki_model_class
|
22
29
|
path.to_s.gsub('/', '').gsub(format_of(path), '').gsub('.', '').to_class
|
23
30
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Ki
|
2
|
+
class ChannelManager
|
3
|
+
def self.connect
|
4
|
+
db.insert 'realtime_channel_sockets', {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.disconnect(socket)
|
8
|
+
db.delete 'realtime_channel_sockets', { 'id' => socket['id'] }
|
9
|
+
db.delete 'realtime_channel_subscriptions', { 'socket_id' => socket['id'] }
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.sockets
|
13
|
+
db.find 'realtime_channel_sockets'
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.subscribe(json)
|
17
|
+
channels = db.find 'realtime_channel_subscriptions', json
|
18
|
+
if channels.empty?
|
19
|
+
item = db.insert 'realtime_channel_subscriptions', json
|
20
|
+
channels.push item
|
21
|
+
end
|
22
|
+
channels
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.unsubscribe(json)
|
26
|
+
json.delete('type')
|
27
|
+
db.delete 'realtime_channel_subscriptions', json
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.publish(json)
|
31
|
+
json['created_at'] = Time.now.to_i
|
32
|
+
db.insert 'realtime_channel_messages', json
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.tick(json)
|
36
|
+
subscribed_channels = db.find 'realtime_channel_subscriptions', json
|
37
|
+
channel_names = subscribed_channels.map { |sc| sc['channel_name'] }
|
38
|
+
|
39
|
+
t = Time.now.to_i
|
40
|
+
|
41
|
+
messages = db.find 'realtime_channel_messages', {
|
42
|
+
'channel_name' => { '$in' => channel_names },
|
43
|
+
'created_at' => { '$gt' => t - 5 }
|
44
|
+
}
|
45
|
+
messages
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.cleanup
|
49
|
+
db.delete 'realtime_channel_sockets', {}
|
50
|
+
db.delete 'realtime_channel_subscriptions', {}
|
51
|
+
db.delete 'realtime_channel_messages', {}
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.db
|
55
|
+
Orm::Db.instance
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/ki/ki.rb
CHANGED
data/lib/ki/ki_cli.rb
CHANGED
data/lib/ki/ki_config.rb
CHANGED
@@ -23,10 +23,13 @@ module Ki
|
|
23
23
|
def middleware
|
24
24
|
used_middleware = %w(ApiHandler CoffeeCompiler SassCompiler HamlCompiler
|
25
25
|
PublicFileServer)
|
26
|
-
used_middleware = @config['middleware'] if @config['middleware']
|
26
|
+
used_middleware = @config['middleware'] if @config['middleware']
|
27
|
+
|
28
|
+
used_middleware = add_rm_middleware used_middleware, 'add_middleware', 'push'
|
29
|
+
used_middleware = add_rm_middleware used_middleware, 'rm_middleware', 'delete'
|
27
30
|
|
28
31
|
# convert middleware to ruby object
|
29
|
-
used_middleware.map do |middleware|
|
32
|
+
used_middleware.uniq.map do |middleware|
|
30
33
|
Object.const_get('Ki').const_get('Middleware').const_get(middleware)
|
31
34
|
end
|
32
35
|
end
|
@@ -34,5 +37,18 @@ module Ki
|
|
34
37
|
def database
|
35
38
|
@config['database']
|
36
39
|
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def add_rm_middleware(used_middleware, key, action)
|
44
|
+
if @config.key?(key)
|
45
|
+
# TODO: concat should work here
|
46
|
+
@config[key] = [@config[key]] if @config[key].class != Array
|
47
|
+
@config[key].each do |mid|
|
48
|
+
used_middleware.send(action, mid)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
used_middleware
|
52
|
+
end
|
37
53
|
end
|
38
54
|
end
|