space_elevator 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5c508252df0ffc2f5f92b49f94034b9e869ea59
4
+ data.tar.gz: f8c316961a39b24f63c9608326336888a117a9be
5
+ SHA512:
6
+ metadata.gz: '069f5e2925bd28ec63d1eef3b3ff926374ccfaeee6e74523f60e825c8ebc39a10718bc9e1977a12b3dbe0d2d37962e74b9a6a394e7e228713f5bf519d0fced51'
7
+ data.tar.gz: 1906914eafafe526dbc65acfea27cf004feede44429dd4a65fd836336ed1208d07501161421027d2a445de0c5d028750f8ebc32013cfe115f01a2fd6290bb00b
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .DS_Store
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in space_elevator.gemspec
4
+ gemspec
@@ -0,0 +1,74 @@
1
+ # space_elevator - The ActionCable Client for Ruby
2
+
3
+ space_elevator is a _client_ for integrating a ruby application with a remote ActionCable-based backend provided by Rails 5 or compatible framework. It allows for subscription and publication to multiple _channels_ simultaneously, and eavesdropping on wire-level messages. Harness the power of WebSockets to receive push notifications in your own Ruby applications!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'space_elevator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install space_elevator
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'space_elevator'
25
+ require 'eventmachine'
26
+ require 'em-websocket-client'
27
+
28
+ EventMachine.run do
29
+
30
+ url = 'ws://example.com'
31
+
32
+ # Create a SpaceElevator::Client with a disconnect handler.
33
+ client = SpaceElevator::Client.new(url) do
34
+ puts 'Disconnected. Exiting...'
35
+ EventMachine.stop_event_loop
36
+ end
37
+
38
+ # Connect the client using the provided callback block.
39
+ client.connect do |msg|
40
+ case msg['type'] # The server will always set a 'type'.
41
+ when 'welcome' # Sent after a successful connection.
42
+ puts 'The server says "welcome".'
43
+
44
+ # Subscribe to something..
45
+ client.subscribe(channel: 'ChatChannel') do |chat|
46
+ puts "Received Chat Event: #{chat}"
47
+ if chat['type'] == 'confirm_subscription'
48
+ puts "Subscription to #{chat['identifier']['channel']} confirmed!"
49
+ # Broadcast to the channel!
50
+ client.publish('ChatChannel', {subject: 'Hi', text: "What's up, y'all!?!?"})
51
+ end
52
+ end
53
+
54
+ # Subscribe to something else simultaneously. Note the additional parameters!
55
+ client.subscribe(channel: 'PlatformChannel', platform_id: platform_id) do |m|
56
+ puts "Received Platform #{platform_id} Event: #{m}"
57
+ # Do whatever, here.
58
+ end
59
+ when 'ping'
60
+ puts 'The server just pinged us.'
61
+ else
62
+ puts msg
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/preston/space_elevator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
71
+
72
+ ## License
73
+
74
+ This work is published under the Apache 2.0 license. Copyright (c) 2017 Preston Lee.
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,84 @@
1
+ require 'space_elevator/version'
2
+
3
+ module SpaceElevator
4
+ # Super-light utility for integrating with ActionCable-based backends.
5
+ class Client
6
+ attr_accessor :client
7
+ attr_accessor :connected
8
+ attr_accessor :connection_handler
9
+ attr_accessor :channel_handlers
10
+ attr_accessor :disconnect_handler
11
+ attr_accessor :url
12
+
13
+ def initialize(url, &disconnect_handler)
14
+ self.url = url
15
+ self.disconnect_handler = disconnect_handler
16
+ end
17
+
18
+ def connect(&block)
19
+ self.connection_handler = block
20
+ self.channel_handlers = {}
21
+ self.client = EventMachine::WebSocketClient.connect(url)
22
+ client.callback do
23
+ puts "Connected WebSocket to #{url}."
24
+ self.connected = true
25
+ end
26
+ client.stream do |raw|
27
+ message = parse_message(raw.to_s)
28
+ if message['identifier'] && message['identifier']['channel']
29
+ channel_handlers[message['identifier']['channel']].call(message)
30
+ else
31
+ connection_handler.call(message)
32
+ end
33
+ end
34
+ client.disconnect do
35
+ self.connected = false
36
+ disconnect_handler.call
37
+ end
38
+ end
39
+
40
+ def subscribe(identifier, &block)
41
+ push(create_subscribe_message(identifier))
42
+ channel_handlers[identifier[:channel]] = block
43
+ end
44
+
45
+ def push(msg)
46
+ client.send_msg(msg)
47
+ end
48
+
49
+ def publish(channel, data)
50
+ msg = create_publish_message(channel, data)
51
+ # puts "PUSHING: #{msg}"
52
+ push(msg)
53
+ end
54
+
55
+ def create_publish_message(channel, data)
56
+ message = {
57
+ command: 'message',
58
+ identifier: { channel: channel }
59
+ }
60
+ message[:data] = data.to_json if data
61
+ message[:identifier] = message[:identifier].to_json
62
+ message.to_json
63
+ end
64
+
65
+ def create_subscribe_message(identifier)
66
+ message = {
67
+ command: 'subscribe',
68
+ identifier: identifier.to_json
69
+ }
70
+ # message[:identifier].merge!(data) if data
71
+ # message[:data] = data.to_json if data
72
+ # message[:identifier] = message[:identifier].to_json
73
+ # puts message
74
+ message.to_json
75
+ end
76
+
77
+ def parse_message(message)
78
+ result = JSON.parse(message)
79
+ result['identifier'] = JSON.parse(result['identifier']) if result['identifier']
80
+ result['data'] = JSON.parse(result['data']) if result['data']
81
+ result
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module SpaceElevator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'space_elevator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "space_elevator"
8
+ spec.version = SpaceElevator::VERSION
9
+ spec.authors = ["Preston Lee"]
10
+ spec.email = ["preston.lee@prestonlee.com"]
11
+
12
+ spec.summary = %q{Ruby client for integrating a ruby application with a remote ActionCable-based backend provided by Rails 5 or compatible framework.}
13
+ spec.description = %q{Ruby client for integrating a ruby application with a remote ActionCable-based backend provided by Rails 5 or compatible framework. It allows for subscription and publication to multiple _channels_ simultaneously, and eavesdropping on wire-level messages. Harness the power of WebSockets to receive push notifications in your own Ruby applications!}
14
+ spec.homepage = "https://github.com/preston/space_elevator"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "eventmachine", ">= 1.2.3"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest", "~> 5.0"
28
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: space_elevator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Preston Lee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eventmachine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description: Ruby client for integrating a ruby application with a remote ActionCable-based
70
+ backend provided by Rails 5 or compatible framework. It allows for subscription
71
+ and publication to multiple _channels_ simultaneously, and eavesdropping on wire-level
72
+ messages. Harness the power of WebSockets to receive push notifications in your
73
+ own Ruby applications!
74
+ email:
75
+ - preston.lee@prestonlee.com
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - README.md
84
+ - Rakefile
85
+ - lib/space_elevator.rb
86
+ - lib/space_elevator/version.rb
87
+ - space_elevator.gemspec
88
+ homepage: https://github.com/preston/space_elevator
89
+ licenses: []
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.6.8
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Ruby client for integrating a ruby application with a remote ActionCable-based
111
+ backend provided by Rails 5 or compatible framework.
112
+ test_files: []