gripcontrol 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7abc023fc0faffad3040b8ce12b0c4b1b4d20e58
4
+ data.tar.gz: 5388924b9251ce2610310825114cb8b631d64b0d
5
+ SHA512:
6
+ metadata.gz: 8d48568f8e78f118e6528c1f6c0e73a64f717b849f7a9985078275c5b30fb7b2a4c2fd2a2b1ab18659fc4011097a35136ec2a24cd654b94b0fc5f984421cd582
7
+ data.tar.gz: 0d19c9558e6b3c945d80f92b762f18a2e73307ff6fd02dd5c382a05063d2352426bfc3f7d70175a6c54d0f159c9bdc8bbd9738378bfcf879be4f5834d323fc27
data/lib/channel.rb ADDED
@@ -0,0 +1,17 @@
1
+ # channel.rb
2
+ # ~~~~~~~~~
3
+ # This module implements the Channel class.
4
+ # :authors: Konstantin Bokarius.
5
+ # :copyright: (c) 2015 by Fanout, Inc.
6
+ # :license: MIT, see LICENSE for more details.
7
+
8
+ class Channel
9
+ attr_accessor :name
10
+ attr_accessor :prev_id
11
+
12
+ def initialize(name, prev_id=nil)
13
+ @name = name
14
+ @prev_id = prev_id
15
+ end
16
+ end
17
+
@@ -0,0 +1,144 @@
1
+ # gripcontrol.rb
2
+ # ~~~~~~~~~
3
+ # This module implements the GripControl class.
4
+ # :authors: Konstantin Bokarius.
5
+ # :copyright: (c) 2015 by Fanout, Inc.
6
+ # :license: MIT, see LICENSE for more details.
7
+
8
+ require 'base64'
9
+ require 'jwt'
10
+ require_relative 'channel.rb'
11
+ require_relative 'httpresponseformat.rb'
12
+ require_relative 'httpstreamformat.rb'
13
+ require_relative 'websocketmessageformat.rb'
14
+ require_relative 'websocketevent.rb'
15
+ require_relative 'grippubcontrol.rb'
16
+
17
+ class GripControl
18
+ def self.create_hold(mode, channels, response)
19
+ hold = Hash.new
20
+ hold['mode'] = mode
21
+ if channels.is_a?(Channel)
22
+ channels = [channels]
23
+ elsif channels.is_a?(String)
24
+ channels = [Channel.new(channels)]
25
+ end
26
+ raise 'channels.length equal to 0' unless channels.length > 0
27
+ ichannels = []
28
+ channels.each do |channel|
29
+ if channel.is_a?(String)
30
+ channel = Channel(channel)
31
+ end
32
+ ichannel = Hash.new
33
+ ichannel['name'] = channel.name
34
+ if !channel.prev_id.nil?
35
+ ichannel['prev-id'] = channel.prev_id
36
+ end
37
+ ichannels.push(ichannel)
38
+ end
39
+ hold['channels'] = ichannels
40
+ iresponse = nil
41
+ if !response.nil?
42
+ if response.is_a?(String)
43
+ response = Response(nil, nil, nil, response)
44
+ end
45
+ iresponse = Hash.new
46
+ if !response.code.nil?
47
+ iresponse['code'] = response.code
48
+ end
49
+ if !response.reason.nil?
50
+ iresponse['reason'] = response.reason
51
+ end
52
+ if !response.headers.nil? and response.headers.length > 0
53
+ iresponse['headers'] = response.headers
54
+ end
55
+ if !response.body.nil?
56
+ if response.body.encoding.name == 'ASCII-8BIT'
57
+ iresponse['body'] = response.body
58
+ else
59
+ iresponse['body-bin'] = Base64.encode64(response.body)
60
+ end
61
+ end
62
+ end
63
+ instruct = Hash.new
64
+ instruct['hold'] = hold
65
+ if !iresponse.nil?
66
+ instruct['response'] = iresponse
67
+ end
68
+ return instruct.to_json
69
+ end
70
+
71
+ def self.create_hold_response(channels, response=nil)
72
+ return GripControl.create_hold('response', channels, response)
73
+ end
74
+
75
+ def self.create_hold_stream(channels, response=nil)
76
+ return create_hold('stream', channels, response)
77
+ end
78
+
79
+ def self.validate_sig(token, key)
80
+ token = token.encode('utf-8')
81
+ begin
82
+ claim = JWT.encode(claim, @auth_jwt_key).decode(token, key,
83
+ verify_expiration=false)
84
+ rescue
85
+ return false
86
+ end
87
+ exp = claim.get('exp')
88
+ if !claim.has_key?('exp')
89
+ return false
90
+ end
91
+ if Time.now.utc.to_i >= claim['exp']
92
+ return false
93
+ end
94
+ return true
95
+ end
96
+
97
+ def self.decode_websocket_events(body)
98
+ out = []
99
+ start = 0
100
+ while start < body.length do
101
+ at = body.index('\r\n', start)
102
+ if !at.nil?
103
+ raise 'bad format'
104
+ end
105
+ typeline = body[start..at - 1]
106
+ start = at + 2
107
+ at = typeline.index(' ')
108
+ if !at.nil?
109
+ etype = typeline[0..at - 1]
110
+ clen = ('0x' + typeline[at + 1..-1]).to_s(16)
111
+ content = body[start:start + clen - 1]
112
+ start += clen + 2
113
+ e = WebSocketEvent.new(etype, content)
114
+ else
115
+ e = WebSocketEvent.new(typeline)
116
+ end
117
+ out.push(e)
118
+ end
119
+ return out
120
+ end
121
+
122
+ def self.encode_websocket_events(events)
123
+ out = ''
124
+ events.each do |event|
125
+ if !event.content.nil?
126
+ out += '%s %x\r\n%s\r\n' % [e.type, len(event.content), event.content]
127
+ else
128
+ out += '%s\r\n' % [e.type]
129
+ end
130
+ end
131
+ return out
132
+ end
133
+
134
+ def self.websocket_control_message(type, args=nil)
135
+ if !args.nil?
136
+ # REVIEW: is this deep copy workaround effective in this case?
137
+ out = Marshal.load(Marshal.dump(args))
138
+ else
139
+ out = Hash.new
140
+ end
141
+ out['type'] = type
142
+ return out.to_json
143
+ end
144
+ end
@@ -0,0 +1,47 @@
1
+ # grippubcontrol.rb
2
+ # ~~~~~~~~~
3
+ # This module implements the GripPubControl class.
4
+ # :authors: Konstantin Bokarius.
5
+ # :copyright: (c) 2015 by Fanout, Inc.
6
+ # :license: MIT, see LICENSE for more details.
7
+
8
+ require 'pubcontrol'
9
+
10
+ class GripPubControl < PubControl
11
+ alias super_publish publish
12
+ alias super_publish_async publish_async
13
+
14
+ def publish_http_response(channel, http_response, id=nil, prev_id=nil)
15
+ if http_response.is_a?(String)
16
+ http_response = HttpResponseFormat.new(nil, nil, nil, http_response)
17
+ end
18
+ item = Item.new(http_response, id, prev_id)
19
+ super_publish(channel, item)
20
+ end
21
+
22
+ def publish_http_response_async(channel, http_response, id=nil,
23
+ prev_id=nil, callback=nil)
24
+ if http_response.is_a?(String)
25
+ http_response = HttpResponseFormat.new(nil, nil, nil, http_response)
26
+ end
27
+ item = Item.new(http_response, id, prev_id)
28
+ super_publish_async(channel, item, callback)
29
+ end
30
+
31
+ def publish_http_stream(channel, http_stream, id=nil, prev_id=nil)
32
+ if http_stream.is_a?(String)
33
+ http_stream = HttpStreamFormat.new(http_stream)
34
+ end
35
+ item = Item.new(http_stream, id, prev_id)
36
+ super_publish(channel, item)
37
+ end
38
+
39
+ def publish_http_stream_async(channel, http_stream, id=nil,
40
+ prev_id=nil, callback=nil)
41
+ if http_stream.is_a?(String)
42
+ http_stream = HttpStreamFormat.new(http_stream)
43
+ end
44
+ item = Item.new(http_stream, id, prev_id)
45
+ super_publish_async(channel, item, callback)
46
+ end
47
+ end
@@ -0,0 +1,49 @@
1
+ # httpresponseformat.rb
2
+ # ~~~~~~~~~
3
+ # This module implements the HttpResponseFormat class.
4
+ # :authors: Konstantin Bokarius.
5
+ # :copyright: (c) 2015 by Fanout, Inc.
6
+ # :license: MIT, see LICENSE for more details.
7
+
8
+ require 'base64'
9
+ require 'pubcontrol'
10
+
11
+ class HttpResponseFormat < Format
12
+ attr_accessor :code
13
+ attr_accessor :reason
14
+ attr_accessor :headers
15
+ attr_accessor :body
16
+
17
+ def initialize(code=nil, reason=nil, headers=nil, body=nil)
18
+ @code = code
19
+ @reason = reason
20
+ @headers = headers
21
+ @body = body
22
+ end
23
+
24
+ def name
25
+ return 'http-response'
26
+ end
27
+
28
+ def export
29
+ out = Hash.new
30
+ if !@code.nil?
31
+ out['code'] = @code
32
+ end
33
+ if !@reason.nil?
34
+ out['reason'] = @reason
35
+ end
36
+ if !@headers.nil? and @headers.length > 0
37
+ out['headers'] = @headers
38
+ end
39
+ if !@body.nil?
40
+ # REVIEW is this the right way to check for binary encoding?
41
+ if @body.encoding.name == 'ASCII-8BIT'
42
+ out['body-bin'] = Base64.encode64(@body)
43
+ else
44
+ out['body'] = @body
45
+ end
46
+ end
47
+ return out
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ # httpstreamformat.rb
2
+ # ~~~~~~~~~
3
+ # This module implements the HttpStreamFormat class.
4
+ # :authors: Konstantin Bokarius.
5
+ # :copyright: (c) 2015 by Fanout, Inc.
6
+ # :license: MIT, see LICENSE for more details.
7
+
8
+ require 'base64'
9
+ require 'pubcontrol'
10
+
11
+ class HttpStreamFormat < Format
12
+ attr_accessor :content
13
+ attr_accessor :close
14
+
15
+ def initialize(content=nil, close=false)
16
+ @content = content
17
+ @close = close
18
+ if !@close and @content.nil?
19
+ raise 'Content not set'
20
+ end
21
+ end
22
+
23
+ def name
24
+ return 'http-stream'
25
+ end
26
+
27
+ def export
28
+ out = Hash.new
29
+ if @close
30
+ out['action'] = 'close'
31
+ else
32
+ if @content.encoding.name == 'ASCII-8BIT'
33
+ out['body-bin'] = Base64.encode64(@content)
34
+ else
35
+ out['body'] = @content
36
+ end
37
+ end
38
+ return out
39
+ end
40
+ end
41
+
@@ -0,0 +1,16 @@
1
+ # websocketevent.rb
2
+ # ~~~~~~~~~
3
+ # This module implements the WebSocketEvent class.
4
+ # :authors: Konstantin Bokarius.
5
+ # :copyright: (c) 2015 by Fanout, Inc.
6
+ # :license: MIT, see LICENSE for more details.
7
+
8
+ class WebSocketEvent
9
+ attr_accessor :type
10
+ attr_accessor :content
11
+
12
+ def initialize(type, content=nil)
13
+ @type = type
14
+ @content = content
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ # websocketmessageformat.rb
2
+ # ~~~~~~~~~
3
+ # This module implements the WebSocketMessageFormat class.
4
+ # :authors: Konstantin Bokarius.
5
+ # :copyright: (c) 2015 by Fanout, Inc.
6
+ # :license: MIT, see LICENSE for more details.
7
+
8
+ require 'base64'
9
+ require 'pubcontrol'
10
+
11
+ class WebSocketMessageFormat < Format
12
+ attr_accessor :content
13
+
14
+ def initialize(content)
15
+ @content = content
16
+ end
17
+
18
+ def name
19
+ return 'ws-message'
20
+ end
21
+
22
+ def export
23
+ out = Hash.new
24
+ if @content.encoding.name == 'ASCII-8BIT'
25
+ out['body-bin'] = Base64.encode64(@content)
26
+ else
27
+ out['body'] = @content
28
+ end
29
+ return out
30
+ end
31
+ end
32
+
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gripcontrol
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Konstantin Bokarius
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby convenience library for using the GRIP protocol
14
+ email: bokarius@comcast.net
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/channel.rb
20
+ - lib/gripcontrol.rb
21
+ - lib/grippubcontrol.rb
22
+ - lib/httpresponseformat.rb
23
+ - lib/httpstreamformat.rb
24
+ - lib/websocketevent.rb
25
+ - lib/websocketmessageformat.rb
26
+ homepage: http://rubygems.org/gems/gripcontrol
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: 1.9.0
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.2.2
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: GRIP library for Ruby
50
+ test_files: []