garufa 1.1.0.rc.3 → 1.1.0.rc.4

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.
@@ -1 +1 @@
1
- 7939
1
+ 18609
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
 
28
28
  s.add_dependency "goliath", "1.0.4"
29
29
  s.add_dependency "faye-websocket", "0.7.4"
30
- s.add_dependency "cuba", "3.1.1"
30
+ s.add_dependency "cuba", "3.3.0"
31
31
  s.add_dependency "signature", "0.1.7"
32
32
  s.add_dependency "tilt", "2.0.1"
33
33
  s.add_dependency "yajl-ruby", "1.2.1"
@@ -0,0 +1,10 @@
1
+ module Garufa
2
+ module API
3
+ module BodyReader
4
+ def read_body
5
+ body = req.body.read; req.body.rewind
6
+ body
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,7 +1,7 @@
1
1
  module Garufa
2
2
  module API
3
3
  module ChannelFilter
4
- def channel_filter(params)
4
+ def filter(params)
5
5
  {
6
6
  info: params['info'].to_s.split(/\s*,\s*/),
7
7
  prefix: params['filter_by_prefix']
@@ -10,4 +10,3 @@ module Garufa
10
10
  end
11
11
  end
12
12
  end
13
-
@@ -0,0 +1,18 @@
1
+ require 'garufa/subscriptions'
2
+
3
+ module Garufa
4
+ module API
5
+ module ChannelStats
6
+ def channel_stats(channel)
7
+ Subscriptions.channel_stats(channel)
8
+ end
9
+
10
+ def channels_stats
11
+ Subscriptions.all.each_with_object({}) do |(channel, _), stats|
12
+ stats[channel] = channel_stats(channel)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -1,4 +1,3 @@
1
- require 'cuba'
2
1
  require 'cuba/render'
3
2
 
4
3
  require 'yajl'
@@ -6,39 +5,27 @@ require 'yajl/json_gem'
6
5
  require 'tilt/yajl'
7
6
 
8
7
  require 'garufa/api/channel_filter'
8
+ require 'garufa/api/channel_stats'
9
+ require 'garufa/api/response_writer'
10
+ require 'garufa/api/terminal_matcher'
9
11
  require 'garufa/api/settings_setter'
12
+ require 'garufa/api/routes/channels'
10
13
 
11
14
  module Garufa
12
15
  module API
16
+
13
17
  class Channels < Cuba
14
18
  plugin Cuba::Render
15
19
  plugin ChannelFilter
20
+ plugin ChannelStats
21
+ plugin ResponseWriter
22
+ plugin TerminalMatcher
16
23
  plugin SettingsSetter
17
24
 
18
- set :render, template_engine: 'yajl', views: File.expand_path("views", File.dirname(__FILE__))
19
- end
20
-
21
- Channels.define do
22
-
23
- on get, "channels/:channel/users" do |channel|
24
- stats = Subscriptions.channel_stats(channel)
25
- res.write partial('users', stats: stats)
26
- end
27
-
28
- on get, "channels/:channel" do |channel|
29
- filter = channel_filter(req.params)
30
- stats = Subscriptions.channel_stats(channel)
31
- res.write partial('channel', stats: stats, filter: filter)
32
- end
33
-
34
- on get, "channels" do
35
- filter = channel_filter(req.params)
36
- stats = Subscriptions.all.each_with_object({}) do |(channel, sub), obj|
37
- obj[channel] = Subscriptions.channel_stats(channel)
38
- end
25
+ set :render, template_engine: 'yajl'
26
+ set :render, views: File.expand_path("views", File.dirname(__FILE__))
39
27
 
40
- res.write partial('channels', stats: stats, filter: filter)
41
- end
28
+ include Routes::Channels
42
29
  end
43
30
  end
44
31
  end
@@ -6,7 +6,10 @@ require 'garufa/message'
6
6
  module Garufa
7
7
  module API
8
8
  module EventHandler
9
- def handle_events(body, params = {})
9
+ def handle_events(body, channel = nil, params = {})
10
+
11
+ params.merge!(channels: [channel]) if channel
12
+
10
13
  body_params = JSON.parse(body)
11
14
 
12
15
  # Some old api clients send channel and event in the url, while only data is
@@ -1,28 +1,15 @@
1
1
  require 'garufa/api/event_handler'
2
+ require 'garufa/api/body_reader'
3
+ require 'garufa/api/routes/events'
2
4
 
3
5
  module Garufa
4
6
  module API
5
- class Events < Cuba; end
6
7
 
7
- Events.plugin EventHandler
8
+ class Events < Cuba
9
+ plugin EventHandler
10
+ plugin BodyReader
8
11
 
9
- Events.define do
10
-
11
- res.status = 202
12
- body = req.body.read
13
-
14
- # Events
15
- on "events" do
16
- handle_events(body)
17
- res.write "{}"
18
- end
19
-
20
- # Legacy events
21
- on "channels/:channel_id/events" do |channel_id|
22
- params = req.GET.merge(channels: [channel_id])
23
- handle_events(body, params)
24
- res.write "{}"
25
- end
12
+ include Routes::Events
26
13
  end
27
14
  end
28
15
  end
@@ -0,0 +1,9 @@
1
+ module Garufa
2
+ module API
3
+ module ResponseWriter
4
+ def write(template, locals)
5
+ res.write partial(template, locals)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,36 @@
1
+ module Garufa
2
+ module API
3
+ module Routes
4
+ module Channels
5
+ def self.included(mod)
6
+ mod.define do
7
+ # GET channels/presence-channel
8
+ on terminalPrefix("presence-") do |channel|
9
+ write 'presence', stats: channel_stats(channel), filter: filter(req.params)
10
+ end
11
+
12
+ # GET channels/presence-channel/users
13
+ on "(presence-.*)/users" do |channel|
14
+ write 'presence_users', stats: channel_stats(channel)
15
+ end
16
+
17
+ # GET channels/non-presence-channel/users
18
+ on ":channel/users" do |channel|
19
+ res.status = 400
20
+ end
21
+
22
+ # GET channels/non-presence-channel
23
+ on terminalPrefix("(?!presence-)") do |channel|
24
+ write 'non_presence', stats: channel_stats(channel)
25
+ end
26
+
27
+ # GET channels
28
+ on root do
29
+ write 'channels', stats: channels_stats, filter: filter(req.params)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,26 @@
1
+ module Garufa
2
+ module API
3
+ module Routes
4
+ module Events
5
+ def self.included(mod)
6
+
7
+ mod.define do
8
+ # Events
9
+ on "events" do
10
+ handle_events(read_body)
11
+ res.status = 202
12
+ res.write '{}'
13
+ end
14
+
15
+ # Legacy events
16
+ on "channels/:channel/events" do |channel|
17
+ handle_events(read_body, channel, req.GET)
18
+ res.status = 202
19
+ res.write '{}'
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,13 +1,15 @@
1
1
  require 'cuba'
2
+
2
3
  require 'garufa/api/authentication'
3
4
  require 'garufa/api/events'
4
5
  require 'garufa/api/channels'
5
6
 
6
7
  module Garufa
7
8
  module API
8
- class Server < Cuba; end
9
9
 
10
- Server.plugin Authentication
10
+ class Server < Cuba
11
+ plugin Authentication
12
+ end
11
13
 
12
14
  Server.define do
13
15
  on "apps/:app_id" do |app_id|
@@ -18,7 +20,7 @@ module Garufa
18
20
  run Events
19
21
  end
20
22
 
21
- on get do
23
+ on get, 'channels' do
22
24
  run Channels
23
25
  end
24
26
  end
@@ -0,0 +1,9 @@
1
+ module Garufa
2
+ module API
3
+ module TerminalMatcher
4
+ def terminalPrefix(prefix)
5
+ /(#{prefix}((?!\/).)*)\z/
6
+ end
7
+ end
8
+ end
9
+ end
@@ -122,8 +122,8 @@ module Garufa
122
122
  data = {}
123
123
 
124
124
  if subscription.presence_channel?
125
- data[:presence] = Subscriptions.channels_data(channel)
126
- data[:presence][:count] = Subscriptions.channel_size(channel)
125
+ stats = Subscriptions.channel_stats(channel)
126
+ data[:presence] = stats[:presence]
127
127
  end
128
128
 
129
129
  send_message Message.subscription_succeeded(channel, data)
@@ -50,25 +50,29 @@ module Garufa
50
50
  subs ? subs.size : 0
51
51
  end
52
52
 
53
- def channels_data(channel)
54
- response = { ids: [], hash: {} }
53
+ def presence_stats(channel)
54
+ return unless channel.start_with?('presence-')
55
+
56
+ data = { ids: [], hash: {} }
55
57
 
56
58
  (subscriptions[channel] || []).each do |sub|
59
+
57
60
  channel_data = JSON.parse(sub.channel_data)
58
61
  id, info = channel_data.values_at('user_id', 'user_info')
59
62
 
60
- next if response[:ids].include? id
63
+ next if data[:ids].include? id
61
64
 
62
- response[:ids] << id
63
- response[:hash][id] = info
65
+ data[:ids] << id
66
+ data[:hash][id] = info
64
67
  end
65
- response
68
+
69
+ data.merge(count: data[:ids].count)
66
70
  end
67
71
 
68
72
  def channel_stats(channel)
69
73
  {
70
74
  size: channel_size(channel),
71
- presence: channels_data(channel)
75
+ presence: presence_stats(channel)
72
76
  }
73
77
  end
74
78
 
@@ -1,3 +1,3 @@
1
1
  module Garufa
2
- VERSION = '1.1.0.rc.3'
2
+ VERSION = '1.1.0.rc.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garufa
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.rc.3
4
+ version: 1.1.0.rc.4
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-09 00:00:00.000000000 Z
12
+ date: 2015-02-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: goliath
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - '='
52
52
  - !ruby/object:Gem::Version
53
- version: 3.1.1
53
+ version: 3.3.0
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 3.1.1
61
+ version: 3.3.0
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: signature
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -151,13 +151,19 @@ files:
151
151
  - README.md
152
152
  - Rakefile
153
153
  - lib/garufa/version.rb
154
+ - lib/garufa/api/body_reader.rb
155
+ - lib/garufa/api/routes/channels.rb
156
+ - lib/garufa/api/routes/events.rb
157
+ - lib/garufa/api/terminal_matcher.rb
154
158
  - lib/garufa/api/channels.rb
155
159
  - lib/garufa/api/event_handler.rb
156
160
  - lib/garufa/api/server.rb
157
161
  - lib/garufa/api/authentication.rb
158
162
  - lib/garufa/api/channel_filter.rb
159
163
  - lib/garufa/api/events.rb
164
+ - lib/garufa/api/response_writer.rb
160
165
  - lib/garufa/api/settings_setter.rb
166
+ - lib/garufa/api/channel_stats.rb
161
167
  - lib/garufa/subscription.rb
162
168
  - lib/garufa/message.rb
163
169
  - lib/garufa/config.rb