rocket_chat-realtime 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.gitlab-ci.yml +50 -0
  4. data/.overcommit.yml +20 -0
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +12 -0
  7. data/.travis.yml +6 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +23 -0
  10. data/Gemfile.lock +100 -0
  11. data/LICENSE +201 -0
  12. data/README.md +40 -0
  13. data/Rakefile +8 -0
  14. data/bin/console +12 -0
  15. data/bin/setup +8 -0
  16. data/lib/rocket_chat/realtime.rb +58 -0
  17. data/lib/rocket_chat/realtime/adapter.rb +51 -0
  18. data/lib/rocket_chat/realtime/async_task.rb +60 -0
  19. data/lib/rocket_chat/realtime/client.rb +100 -0
  20. data/lib/rocket_chat/realtime/connector.rb +79 -0
  21. data/lib/rocket_chat/realtime/dispatcher.rb +80 -0
  22. data/lib/rocket_chat/realtime/event_emitter.rb +65 -0
  23. data/lib/rocket_chat/realtime/handlers/base.rb +52 -0
  24. data/lib/rocket_chat/realtime/handlers/changed.rb +31 -0
  25. data/lib/rocket_chat/realtime/handlers/ready.rb +23 -0
  26. data/lib/rocket_chat/realtime/handlers/result.rb +34 -0
  27. data/lib/rocket_chat/realtime/message.rb +40 -0
  28. data/lib/rocket_chat/realtime/messages/changed.rb +44 -0
  29. data/lib/rocket_chat/realtime/messages/method.rb +37 -0
  30. data/lib/rocket_chat/realtime/messages/result.rb +40 -0
  31. data/lib/rocket_chat/realtime/messages/subscribe.rb +37 -0
  32. data/lib/rocket_chat/realtime/methods/auth.rb +33 -0
  33. data/lib/rocket_chat/realtime/methods/message.rb +33 -0
  34. data/lib/rocket_chat/realtime/reactor.rb +123 -0
  35. data/lib/rocket_chat/realtime/subscriptions/room.rb +30 -0
  36. data/lib/rocket_chat/realtime/version.rb +7 -0
  37. data/rocket_chat-realtime.gemspec +31 -0
  38. metadata +122 -0
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rocket_chat/realtime/messages/method'
4
+
5
+ module RocketChat
6
+ module Realtime
7
+ module Methods
8
+ # Message methods
9
+ #
10
+ # @since 0.1.0
11
+ module Message
12
+ # Login into server
13
+ #
14
+ # @param room_id [String]
15
+ # @param message [String]
16
+ #
17
+ # @since 0.1.0
18
+ def send_message(room_id, message)
19
+ method = Messages::Method.new(
20
+ 'sendMessage',
21
+ {
22
+ rid: room_id,
23
+ msg: message
24
+ }
25
+ )
26
+ AsyncTask.start(method.id) do
27
+ driver.text(method.to_json)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+
6
+ require 'nio'
7
+
8
+ module RocketChat
9
+ module Realtime
10
+ # The scheduler manager
11
+ #
12
+ # @since 0.1.0
13
+ class Reactor
14
+ class << self
15
+ extend Forwardable
16
+
17
+ # @since 0.1.0
18
+ delegate %w[
19
+ selector
20
+ register
21
+ deregister
22
+ registered?
23
+ clients
24
+ run
25
+ stop
26
+ stopped?
27
+ reset
28
+ ] => :instance
29
+ end
30
+
31
+ include Singleton
32
+
33
+ # @since 0.1.0
34
+ attr_reader :selector, :clients
35
+
36
+ # Initialize Reactor
37
+ #
38
+ # @since 0.1.0
39
+ def initialize
40
+ @selector = NIO::Selector.new
41
+ @clients = Set.new
42
+ reset
43
+ end
44
+
45
+ # The client is registered
46
+ #
47
+ # @param client [Rocket::Realtime::Client]
48
+ #
49
+ # @return [Boolean]
50
+ #
51
+ # @since 0.1.0
52
+ def registered?(client)
53
+ @clients.include?(client)
54
+ end
55
+
56
+ # Register Client
57
+ #
58
+ # @param client [RocketChat::Realtime::Client]
59
+ #
60
+ # @since 0.1.0
61
+ def register(client)
62
+ return if registered?(client)
63
+
64
+ @clients.add(client)
65
+ monitor = selector.register(client.connector.socket, :rw)
66
+ monitor.value = client
67
+ end
68
+
69
+ # Deregister Client
70
+ #
71
+ # @param client [RocketChat::Realtime::Client]
72
+ #
73
+ # @since 0.1.0
74
+ def deregister(client)
75
+ return unless registered?(client)
76
+
77
+ @clients.delete(client)
78
+ selector.deregister(client.connector.socket)
79
+ end
80
+
81
+ # Reset reactor state
82
+ #
83
+ # @since 0.1.0
84
+ def reset
85
+ # TODO: Clear clients and registered monitor
86
+ @thread&.exit
87
+ @thread = nil
88
+ end
89
+
90
+ # Stop reactor
91
+ #
92
+ # @since 0.1.0
93
+ def stop
94
+ @thread&.exit
95
+ @thread = nil
96
+ end
97
+
98
+ # @return [Boolean] the reactor is stopped
99
+ #
100
+ # @since 0.1.0
101
+ def stopped?
102
+ @thread.nil?
103
+ end
104
+
105
+ # Wait I/O ready for read or write
106
+ #
107
+ # @since 0.1.0
108
+ def run
109
+ return unless stopped?
110
+
111
+ @thread = Thread.start do
112
+ Thread.current.abort_on_exception = true
113
+ until stopped?
114
+ selector.select(1) do |monitor|
115
+ monitor.value.process(monitor)
116
+ end
117
+ Thread.pass
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rocket_chat/realtime/messages/subscribe'
4
+
5
+ module RocketChat
6
+ module Realtime
7
+ module Subscriptions
8
+ # Room Subscriptions
9
+ #
10
+ # @since 0.1.0
11
+ module Room
12
+ # Subscribe to room messages
13
+ #
14
+ # @param room_id [String]
15
+ #
16
+ # @since 0.1.0
17
+ def subscribe_room_messages(room_id)
18
+ subscription = Messages::Subscribe.new(
19
+ 'stream-room-messages',
20
+ room_id,
21
+ false
22
+ )
23
+ AsyncTask.start(subscription.id) do
24
+ driver.text(subscription.to_json)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RocketChat
4
+ module Realtime
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/rocket_chat/realtime/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rocket_chat-realtime'
7
+ spec.version = RocketChat::Realtime::VERSION
8
+ spec.authors = ['5xRuby']
9
+ spec.email = ['hello@5xruby.com']
10
+
11
+ spec.summary = 'Rocket.Chat realtime api client'
12
+ spec.description = 'Rocket.Chat realtime api client'
13
+ spec.homepage = 'https://github.com/5xRuby/rocket_chat-realtime'
14
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_runtime_dependency 'concurrent-ruby', '~> 1.1'
29
+ spec.add_runtime_dependency 'nio4r', '~> 2.5'
30
+ spec.add_runtime_dependency 'websocket-driver', '~> 0.7'
31
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rocket_chat-realtime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 5xRuby
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nio4r
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: websocket-driver
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ description: Rocket.Chat realtime api client
56
+ email:
57
+ - hello@5xruby.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".gitlab-ci.yml"
64
+ - ".overcommit.yml"
65
+ - ".rspec"
66
+ - ".rubocop.yml"
67
+ - ".travis.yml"
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - bin/console
75
+ - bin/setup
76
+ - lib/rocket_chat/realtime.rb
77
+ - lib/rocket_chat/realtime/adapter.rb
78
+ - lib/rocket_chat/realtime/async_task.rb
79
+ - lib/rocket_chat/realtime/client.rb
80
+ - lib/rocket_chat/realtime/connector.rb
81
+ - lib/rocket_chat/realtime/dispatcher.rb
82
+ - lib/rocket_chat/realtime/event_emitter.rb
83
+ - lib/rocket_chat/realtime/handlers/base.rb
84
+ - lib/rocket_chat/realtime/handlers/changed.rb
85
+ - lib/rocket_chat/realtime/handlers/ready.rb
86
+ - lib/rocket_chat/realtime/handlers/result.rb
87
+ - lib/rocket_chat/realtime/message.rb
88
+ - lib/rocket_chat/realtime/messages/changed.rb
89
+ - lib/rocket_chat/realtime/messages/method.rb
90
+ - lib/rocket_chat/realtime/messages/result.rb
91
+ - lib/rocket_chat/realtime/messages/subscribe.rb
92
+ - lib/rocket_chat/realtime/methods/auth.rb
93
+ - lib/rocket_chat/realtime/methods/message.rb
94
+ - lib/rocket_chat/realtime/reactor.rb
95
+ - lib/rocket_chat/realtime/subscriptions/room.rb
96
+ - lib/rocket_chat/realtime/version.rb
97
+ - rocket_chat-realtime.gemspec
98
+ homepage: https://github.com/5xRuby/rocket_chat-realtime
99
+ licenses: []
100
+ metadata:
101
+ homepage_uri: https://github.com/5xRuby/rocket_chat-realtime
102
+ source_code_uri: https://github.com/5xRuby/rocket_chat-realtime
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: 2.3.0
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.0.3
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Rocket.Chat realtime api client
122
+ test_files: []