em-pusher-client 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +24 -0
- data/.gitignore +51 -0
- data/.rspec +3 -0
- data/.rubocop.yml +50 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +8 -0
- data/Guardfile +30 -0
- data/README.md +91 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/bump +66 -0
- data/em-pusher-client.gemspec +35 -0
- data/em-pusher-client.sublime-project +13 -0
- data/examples/normal.rb +50 -0
- data/lib/em/pusher/client/connection.rb +107 -0
- data/lib/em/pusher/client/msg_parser.rb +40 -0
- data/lib/em/pusher/client/version.rb +9 -0
- data/lib/em/pusher/client.rb +40 -0
- metadata +204 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9b6613c790b8733c6c079eef2f2363a6b9cab073
|
4
|
+
data.tar.gz: 7a8d966f51b78a1dfe666951a36ae5e9b7a5e347
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1985a4aa80bf970dc44cb817aa40408b87460ff561cbe24f9e1b4aa3839387dec6dad01dfb7b7b4f0fede179d857eb921a0d605454468ae00caaf5ab29603317
|
7
|
+
data.tar.gz: 9b24b97a39619613c27f125073d972f7e92e213cb8a29d9f107685512914d08d3361982131f9bbb18dd011eaf47b8d418639cf856a18aca81f56e03a8e7a4636
|
data/.editorconfig
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# EditorConfig helps developers define and maintain consistent
|
2
|
+
# coding styles between different editors and IDEs
|
3
|
+
# editorconfig.org
|
4
|
+
|
5
|
+
root = true
|
6
|
+
|
7
|
+
[*]
|
8
|
+
|
9
|
+
# Change these settings to your own preference
|
10
|
+
indent_style = space
|
11
|
+
indent_size = 2
|
12
|
+
|
13
|
+
# We recommend you to keep these unchanged
|
14
|
+
end_of_line = lf
|
15
|
+
charset = utf-8
|
16
|
+
trim_trailing_whitespace = true
|
17
|
+
insert_final_newline = true
|
18
|
+
|
19
|
+
[*.md]
|
20
|
+
trim_trailing_whitespace = false
|
21
|
+
|
22
|
+
[nginx.conf.erb]
|
23
|
+
indent_style = tabs
|
24
|
+
indent_size = 4
|
data/.gitignore
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
51
|
+
*.sublime-workspace
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.3.5
|
5
|
+
|
6
|
+
Style/Documentation:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Metrics/BlockLength:
|
10
|
+
Exclude:
|
11
|
+
- Guardfile
|
12
|
+
- 'spec/**/*'
|
13
|
+
- 'examples/**/*'
|
14
|
+
- '*.gemspec'
|
15
|
+
|
16
|
+
Style/HashSyntax:
|
17
|
+
EnforcedStyle: ruby19
|
18
|
+
|
19
|
+
Style/TrailingCommaInArguments:
|
20
|
+
EnforcedStyleForMultiline: consistent_comma
|
21
|
+
|
22
|
+
Style/TrailingCommaInArrayLiteral:
|
23
|
+
EnforcedStyleForMultiline: consistent_comma
|
24
|
+
|
25
|
+
Style/TrailingCommaInHashLiteral:
|
26
|
+
EnforcedStyleForMultiline: consistent_comma
|
27
|
+
|
28
|
+
Layout/AccessModifierIndentation:
|
29
|
+
EnforcedStyle: outdent
|
30
|
+
|
31
|
+
Style/SignalException:
|
32
|
+
EnforcedStyle: semantic
|
33
|
+
|
34
|
+
Metrics/LineLength:
|
35
|
+
Max: 120
|
36
|
+
|
37
|
+
Metrics/ClassLength:
|
38
|
+
Max: 150
|
39
|
+
|
40
|
+
Metrics/ModuleLength:
|
41
|
+
Max: 150
|
42
|
+
|
43
|
+
Style/Documentation:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
RSpec/ExampleLength:
|
47
|
+
Max: 10
|
48
|
+
|
49
|
+
Style/SafeNavigation:
|
50
|
+
Enabled: false
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
em-pusher-client
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.5
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
notification :terminal_notifier
|
4
|
+
|
5
|
+
rspec_options = {
|
6
|
+
all_after_pass: true,
|
7
|
+
cmd: 'rspec spec',
|
8
|
+
failed_mode: :focus,
|
9
|
+
}
|
10
|
+
|
11
|
+
clearing :on
|
12
|
+
|
13
|
+
guard :rspec, rspec_options do
|
14
|
+
require 'ostruct'
|
15
|
+
|
16
|
+
# Generic Ruby apps
|
17
|
+
rspec = OpenStruct.new
|
18
|
+
rspec.spec = ->(m) { "spec/#{m}_spec.rb" }
|
19
|
+
rspec.spec_dir = 'spec'
|
20
|
+
rspec.spec_helper = 'spec/spec_helper.rb'
|
21
|
+
|
22
|
+
watch(%r{^spec/.+_spec\.rb$})
|
23
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
24
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
25
|
+
end
|
26
|
+
|
27
|
+
guard :rubocop, all_on_start: true do
|
28
|
+
watch(/.+\.rb$/)
|
29
|
+
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
30
|
+
end
|
data/README.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# Em::Pusher::Client
|
2
|
+
|
3
|
+
[![Gem Version][rubygems-image]][rubygems-url]
|
4
|
+
[![Build Status][travis-image]][travis-url]
|
5
|
+
[![Coverage Status][coverage-image]][coverage-url]
|
6
|
+
|
7
|
+
EventMachine client library for Pusher
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'em-pusher-client'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install em-pusher-client
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
EM.run do
|
29
|
+
opts = {
|
30
|
+
key: 'my-key',
|
31
|
+
cluster: 'us2',
|
32
|
+
port: 80,
|
33
|
+
scheme: 'ws',
|
34
|
+
}
|
35
|
+
EM::Pusher::Client.connect(opts) do |conn|
|
36
|
+
conn.connected do
|
37
|
+
puts 'connected'
|
38
|
+
end
|
39
|
+
|
40
|
+
conn.callback do
|
41
|
+
puts 'callback'
|
42
|
+
msg = {
|
43
|
+
event: 'pusher:subscribe',
|
44
|
+
data: {
|
45
|
+
channel: 'my-channel',
|
46
|
+
},
|
47
|
+
}
|
48
|
+
conn.send_msg(msg)
|
49
|
+
end
|
50
|
+
|
51
|
+
conn.errback do |e|
|
52
|
+
puts "Got error: #{e}"
|
53
|
+
end
|
54
|
+
|
55
|
+
conn.stream do |msg|
|
56
|
+
puts "stream: <#{msg}>"
|
57
|
+
case msg.event
|
58
|
+
when 'pusher:connection_established'
|
59
|
+
puts 'Connection Established'
|
60
|
+
when 'pusher_internal:subscription_succeeded'
|
61
|
+
puts "Subscribed to #{msg.json['channel']}"
|
62
|
+
when 'someevent'
|
63
|
+
puts "someevent: #{msg.data}"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
conn.disconnect do
|
68
|
+
puts 'gone'
|
69
|
+
EM.stop_event_loop
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
## Development
|
76
|
+
|
77
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `rake console` for an interactive prompt that will allow you to experiment.
|
78
|
+
|
79
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
80
|
+
|
81
|
+
## Contributing
|
82
|
+
|
83
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/em-pusher-client.
|
84
|
+
|
85
|
+
|
86
|
+
[rubygems-image]: https://badge.fury.io/rb/em-pusher-client.svg
|
87
|
+
[rubygems-url]: https://badge.fury.io/rb/em-pusher-client
|
88
|
+
[travis-image]: https://travis-ci.org/genaromadrid/em-pusher-client.svg?branch=master
|
89
|
+
[travis-url]: https://travis-ci.org/genaromadrid/em-pusher-client
|
90
|
+
[coverage-image]: https://coveralls.io/repos/github/genaromadrid/em-pusher-client/badge.svg?branch=master
|
91
|
+
[coverage-url]: https://coveralls.io/github/genaromadrid/em-pusher-client?branch=master
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'em/pusher/client'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/bump
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
#!/usr/bin/env python
|
2
|
+
import re, glob, sys, os
|
3
|
+
|
4
|
+
__USAGE__ = \
|
5
|
+
"""BUMP is a semantic versioning bump script which accepts the following
|
6
|
+
mutually exclusive arguments:
|
7
|
+
-m - a "major" version bump equal to +1.0.0
|
8
|
+
-n - a "minor" version bump equal to +0.1.0
|
9
|
+
-p - a "patch" version bump equal to +0.0.1
|
10
|
+
-h - a "hot fix" version bump equal to +0.0.1
|
11
|
+
|
12
|
+
All of these options allow for the -r flag, which indicates that the state
|
13
|
+
is a RELEASE not a SNAPSHOT. If -r is not specified, then -SNAPSHOT is
|
14
|
+
appended to the updated version string."""
|
15
|
+
|
16
|
+
__INITIAL__ = ['0', '0', '1']
|
17
|
+
|
18
|
+
|
19
|
+
if __name__ == "__main__":
|
20
|
+
v = []
|
21
|
+
try:
|
22
|
+
version_file = glob.glob("lib/em/pusher/client/version.rb")[0]
|
23
|
+
raw_v = re.search(r'VERSION = \'(.*)\'', open(version_file).read(), re.M|re.I|re.S).group(1)
|
24
|
+
v = re.split(re.compile("\.|-"), raw_v)
|
25
|
+
v = v[0:3]
|
26
|
+
map(int, v)
|
27
|
+
|
28
|
+
except ValueError:
|
29
|
+
print("failed to parse the existing VERSION file, assuming v 0.0.1")
|
30
|
+
v = ['0', '0', '1']
|
31
|
+
|
32
|
+
op = ''
|
33
|
+
try:
|
34
|
+
op = sys.argv[1]
|
35
|
+
except:
|
36
|
+
print(__USAGE__)
|
37
|
+
sys.exit(-1)
|
38
|
+
|
39
|
+
if(op == '-m'):
|
40
|
+
v = [str(int(v[0])+1), '0', '0']
|
41
|
+
|
42
|
+
elif(op == '-n'):
|
43
|
+
v = [v[0], str(int(v[1])+1), '0']
|
44
|
+
|
45
|
+
elif(op == '-p' or op == '-h'):
|
46
|
+
v = [v[0], v[1], str(int(v[2])+1)]
|
47
|
+
|
48
|
+
else:
|
49
|
+
print(__USAGE__)
|
50
|
+
sys.exit(-1)
|
51
|
+
|
52
|
+
v = '.'.join(v)
|
53
|
+
|
54
|
+
if(op == '-h'):
|
55
|
+
os.system("git checkout -b hotfix/v%s master" % v)
|
56
|
+
|
57
|
+
else:
|
58
|
+
os.system("git checkout -b release/v%s develop" % v)
|
59
|
+
|
60
|
+
os.system("bump set %s" % v)
|
61
|
+
|
62
|
+
v += "\n"
|
63
|
+
|
64
|
+
print(v)
|
65
|
+
|
66
|
+
sys.exit(0)
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'em/pusher/client/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'em-pusher-client'
|
9
|
+
spec.version = Em::Pusher::Client::VERSION
|
10
|
+
spec.authors = ['Genaro Madrid']
|
11
|
+
spec.email = ['genmadrid@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Eventmachine pusher.com client'
|
14
|
+
spec.description = 'Subscribe to channels with eventmachine'
|
15
|
+
spec.homepage = 'https://github.com/genaromadrid/em-pusher-client'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency 'eventmachine', '~> 1.2.5'
|
25
|
+
spec.add_dependency 'websocket', '~> 1.2.5'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
28
|
+
spec.add_development_dependency 'coveralls', '0.8.21'
|
29
|
+
spec.add_development_dependency 'pry', '~> 0.11'
|
30
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'rspec', '~> 3.7'
|
32
|
+
spec.add_development_dependency 'rubocop', '~> 0.54'
|
33
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.24'
|
34
|
+
spec.add_development_dependency 'simplecov', '~> 0.14'
|
35
|
+
end
|
data/examples/normal.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require './lib/em/pusher/client'
|
4
|
+
|
5
|
+
EM.run do
|
6
|
+
opts = {
|
7
|
+
key: 'my-key',
|
8
|
+
cluster: 'us2',
|
9
|
+
port: 80,
|
10
|
+
scheme: 'ws',
|
11
|
+
}
|
12
|
+
EM::Pusher::Client.connect(opts) do |conn|
|
13
|
+
conn.connected do
|
14
|
+
puts 'connected'
|
15
|
+
end
|
16
|
+
|
17
|
+
conn.callback do
|
18
|
+
puts 'callback'
|
19
|
+
msg = {
|
20
|
+
event: 'pusher:subscribe',
|
21
|
+
data: {
|
22
|
+
channel: 'my-channel',
|
23
|
+
},
|
24
|
+
}
|
25
|
+
conn.send_msg(msg)
|
26
|
+
end
|
27
|
+
|
28
|
+
conn.errback do |e|
|
29
|
+
puts "Got error: #{e}"
|
30
|
+
end
|
31
|
+
|
32
|
+
conn.stream do |msg|
|
33
|
+
puts "stream: <#{msg}>"
|
34
|
+
case msg.event
|
35
|
+
when 'pusher:connection_established'
|
36
|
+
puts 'Connection Established'
|
37
|
+
when 'pusher_internal:subscription_succeeded'
|
38
|
+
puts "Subscribed to #{msg.json['channel']}"
|
39
|
+
when 'someevent'
|
40
|
+
puts "someevent: #{msg.data}"
|
41
|
+
end
|
42
|
+
# conn.close_connection if closed?
|
43
|
+
end
|
44
|
+
|
45
|
+
conn.disconnect do
|
46
|
+
puts 'gone'
|
47
|
+
EM.stop_event_loop
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'eventmachine'
|
4
|
+
require 'uri'
|
5
|
+
require 'json'
|
6
|
+
require 'websocket'
|
7
|
+
|
8
|
+
module EM
|
9
|
+
module Pusher
|
10
|
+
module Client
|
11
|
+
class Connection < EM::Connection
|
12
|
+
include EM::Deferrable
|
13
|
+
|
14
|
+
attr_accessor :url
|
15
|
+
attr_accessor :protocol_version
|
16
|
+
attr_accessor :origin
|
17
|
+
|
18
|
+
def self.connect(uri, opts = {})
|
19
|
+
p_uri = URI.parse(uri)
|
20
|
+
conn = EM.connect(p_uri.host, p_uri.port || 80, self) do |c|
|
21
|
+
c.url = uri
|
22
|
+
c.protocol_version = opts[:version]
|
23
|
+
c.origin = opts[:origin]
|
24
|
+
end
|
25
|
+
yield conn if block_given?
|
26
|
+
conn
|
27
|
+
end
|
28
|
+
|
29
|
+
def post_init
|
30
|
+
@handshaked = false
|
31
|
+
@frame = ::WebSocket::Frame::Incoming::Client.new
|
32
|
+
end
|
33
|
+
|
34
|
+
def connection_completed
|
35
|
+
@connect.yield if @connect
|
36
|
+
@hs = ::WebSocket::Handshake::Client.new(
|
37
|
+
url: @url,
|
38
|
+
origin: @origin,
|
39
|
+
version: @protocol_version,
|
40
|
+
)
|
41
|
+
send_data(@hs.to_s)
|
42
|
+
end
|
43
|
+
|
44
|
+
def stream(&cback)
|
45
|
+
@stream = cback
|
46
|
+
end
|
47
|
+
|
48
|
+
def connected(&cback)
|
49
|
+
@connect = cback
|
50
|
+
end
|
51
|
+
|
52
|
+
def disconnect(&cback)
|
53
|
+
@disconnect = cback
|
54
|
+
end
|
55
|
+
|
56
|
+
# https://pusher.com/docs/pusher_protocol#subscription-events
|
57
|
+
def subscribe(channel, auth = nil, channel_data = nil)
|
58
|
+
msg = {
|
59
|
+
event: 'pusher:subscribe',
|
60
|
+
data: {
|
61
|
+
channel: channel,
|
62
|
+
auth: auth,
|
63
|
+
channel_data: channel_data,
|
64
|
+
},
|
65
|
+
}
|
66
|
+
conn.send_msg(msg)
|
67
|
+
end
|
68
|
+
|
69
|
+
def receive_data(data)
|
70
|
+
return handle_received_data(data) if @handshaked
|
71
|
+
@hs << data
|
72
|
+
if @hs.finished?
|
73
|
+
@handshaked = true
|
74
|
+
succeed
|
75
|
+
end
|
76
|
+
|
77
|
+
receive_data(@hs.leftovers) if @hs.leftovers
|
78
|
+
end
|
79
|
+
|
80
|
+
def send_msg(data, args = {})
|
81
|
+
type = args[:type] || :text
|
82
|
+
data = data.to_json if data.is_a?(Hash)
|
83
|
+
frame = ::WebSocket::Frame::Outgoing::Client.new(
|
84
|
+
data: data,
|
85
|
+
type: type,
|
86
|
+
version: @hs.version,
|
87
|
+
)
|
88
|
+
send_data(frame.to_s)
|
89
|
+
end
|
90
|
+
|
91
|
+
def unbind
|
92
|
+
super
|
93
|
+
@disconnect.call if @disconnect
|
94
|
+
end
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def handle_received_data(data)
|
99
|
+
@frame << data
|
100
|
+
while (msg = @frame.next)
|
101
|
+
@stream.call(EM::Pusher::Client::MsgParser.new(msg)) if @stream
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EM
|
4
|
+
module Pusher
|
5
|
+
module Client
|
6
|
+
class MsgParser
|
7
|
+
attr_reader :json,
|
8
|
+
:data
|
9
|
+
|
10
|
+
def initialize(msg)
|
11
|
+
parse(msg)
|
12
|
+
end
|
13
|
+
|
14
|
+
def event
|
15
|
+
@event ||= json['event']
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
@msg.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def parse(msg)
|
25
|
+
@msg = msg
|
26
|
+
@json = JSON.parse(msg.data)
|
27
|
+
@data =
|
28
|
+
if json['data'].is_a?(String)
|
29
|
+
JSON.parse(json['data'])
|
30
|
+
else
|
31
|
+
json['data']
|
32
|
+
end
|
33
|
+
rescue JSON::ParserError => e
|
34
|
+
puts "Error parsing event '#{event}': '#{e.message}'"
|
35
|
+
# logger.error("Error parsing msg #{e}")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'client/version'
|
4
|
+
require_relative 'client/connection'
|
5
|
+
require_relative 'client/msg_parser'
|
6
|
+
|
7
|
+
module EM
|
8
|
+
module Pusher
|
9
|
+
module Client
|
10
|
+
DEFAULT_OPTIONS = {
|
11
|
+
app_id: nil,
|
12
|
+
app_secret: nil,
|
13
|
+
scheme: 'ws',
|
14
|
+
port: 80,
|
15
|
+
encrypted: 'on',
|
16
|
+
protocol: 4,
|
17
|
+
version: '4.2',
|
18
|
+
client_name: 'em-pusher-client',
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
REQUIRED_OPTIONS = %i[key cluster].freeze
|
22
|
+
|
23
|
+
def self.connect(options)
|
24
|
+
uri = url(options)
|
25
|
+
Connection.connect(uri).tap do |conn|
|
26
|
+
yield conn if block_given?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.url(options)
|
31
|
+
opts = DEFAULT_OPTIONS.merge(options)
|
32
|
+
REQUIRED_OPTIONS.each { |opt| fail ArgumentError, "option #{opt} is required" unless opts[opt] }
|
33
|
+
"#{opts[:scheme]}://ws-#{opts[:cluster]}.pusher.com:#{opts[:port]}/app/#{opts[:key]}" \
|
34
|
+
"?protocol=#{opts[:protocol]}" \
|
35
|
+
"&client=#{opts[:client_name]}" \
|
36
|
+
"&version=#{opts[:version]}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-pusher-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genaro Madrid
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-26 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.5
|
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.5
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: websocket
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.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: 1.2.5
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.8.21
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.8.21
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.11'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.11'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.7'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.7'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.54'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.54'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop-rspec
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '1.24'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '1.24'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.14'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.14'
|
153
|
+
description: Subscribe to channels with eventmachine
|
154
|
+
email:
|
155
|
+
- genmadrid@gmail.com
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- ".editorconfig"
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- ".ruby-gemset"
|
165
|
+
- ".ruby-version"
|
166
|
+
- ".travis.yml"
|
167
|
+
- Gemfile
|
168
|
+
- Guardfile
|
169
|
+
- README.md
|
170
|
+
- Rakefile
|
171
|
+
- bin/console
|
172
|
+
- bin/setup
|
173
|
+
- bump
|
174
|
+
- em-pusher-client.gemspec
|
175
|
+
- em-pusher-client.sublime-project
|
176
|
+
- examples/normal.rb
|
177
|
+
- lib/em/pusher/client.rb
|
178
|
+
- lib/em/pusher/client/connection.rb
|
179
|
+
- lib/em/pusher/client/msg_parser.rb
|
180
|
+
- lib/em/pusher/client/version.rb
|
181
|
+
homepage: https://github.com/genaromadrid/em-pusher-client
|
182
|
+
licenses: []
|
183
|
+
metadata: {}
|
184
|
+
post_install_message:
|
185
|
+
rdoc_options: []
|
186
|
+
require_paths:
|
187
|
+
- lib
|
188
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
189
|
+
requirements:
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
version: '0'
|
193
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
194
|
+
requirements:
|
195
|
+
- - ">="
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: '0'
|
198
|
+
requirements: []
|
199
|
+
rubyforge_project:
|
200
|
+
rubygems_version: 2.5.2.1
|
201
|
+
signing_key:
|
202
|
+
specification_version: 4
|
203
|
+
summary: Eventmachine pusher.com client
|
204
|
+
test_files: []
|