ws_rails_client 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 217afa0db6070705bd2d958954fb254c79b1e7f3
4
+ data.tar.gz: b8e7f007c93eba1a69ed641b30f5819758220a15
5
+ SHA512:
6
+ metadata.gz: 99b951596e957fb770bf5ea1b1083af0d0ea884ff0771e45752e9daf3282fc330422d72e6daf203e59d1487c7427f4702052f634a73933ad9e5182b74e7f8b37
7
+ data.tar.gz: a67d6b4c9fb8d1abeb9a873ab50e0869cbb2639aa05d9fe84d17e6b244b744c467fba396061154a6f7948db77f0478c664d7bcd575f8047fa20ffaf067b5f9e6
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Claude Tech
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # ws-rails-client
2
+
3
+ Simple Ruby client to for Websocket Rails server.
4
+
5
+ Example:
6
+
7
+ ```ruby
8
+ require 'ws_rails_client'
9
+
10
+ EM.run do
11
+ client = WsRails::Client.new("ws://localhost:3000/websocket")
12
+
13
+ client.handle 'my_event' do |message|
14
+ client.send('my_response', message)
15
+ end
16
+ end
17
+ ```
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'WsRailsClient'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
@@ -0,0 +1,3 @@
1
+ %w{client version}.each do |m|
2
+ require File.join(File.dirname(__FILE__), 'ws_rails_client', m)
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'faye/websocket'
2
+ require 'json'
3
+
4
+ module WsRails
5
+ class Client
6
+ def initialize(endpoint)
7
+ @handlers = {}
8
+ @pong = nil
9
+ initialize_client(endpoint)
10
+ end
11
+
12
+ def send(name, data={})
13
+ message = { data: data }
14
+ message[:connection_id] = @connection_id
15
+ @ws.send([name, message].to_json)
16
+ end
17
+
18
+ def handle(name, &action)
19
+ @handlers[name.to_sym] = action
20
+ end
21
+
22
+ private
23
+ def initialize_client(endpoint)
24
+ @ws = Faye::WebSocket::Client.new(endpoint)
25
+ @ws.on :message do |event|
26
+ type, message = ::JSON.parse(event.data)[0]
27
+ symbolize_keys_deep!(message)
28
+ @handlers[type.to_sym].call(message) unless @handlers[type.to_sym].nil?
29
+ end
30
+ init_handlers
31
+ end
32
+
33
+ def init_handlers
34
+ handle :client_connected do |data|
35
+ @connection_id = data[:connection_id]
36
+ end
37
+ handle 'websocket_rails.ping' do
38
+ self.send('websocket_rails.pong', connection_id: @connection_id)
39
+ end
40
+ end
41
+
42
+ def symbolize_keys_deep!(h)
43
+ h.keys.each do |k|
44
+ ks = k.respond_to?(:to_sym) ? k.to_sym : k
45
+ h[ks] = h.delete k
46
+ symbolize_keys_deep! h[ks] if h[ks].kind_of? Hash
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,6 @@
1
+ module WsRails
2
+ MAJOR = 0
3
+ MINOR = 1
4
+ PATCH = 0
5
+ VERSION = "#{MAJOR}.#{MINOR}.#{PATCH}"
6
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ws_rails_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Perez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faye-websocket
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |
42
+ # ws-rails-client
43
+
44
+ Simple Ruby client to for Websocket Rails server.
45
+
46
+ Example:
47
+
48
+ ```ruby
49
+ require 'ws_rails_client'
50
+
51
+ EM.run do
52
+ client = WsRails::Client.new("ws://localhost:3000/websocket")
53
+
54
+ client.handle 'my_event' do |message|
55
+ client.send('my_response', message)
56
+ end
57
+ end
58
+ ```
59
+ email:
60
+ - tuvistavie@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/ws_rails_client.rb
69
+ - lib/ws_rails_client/client.rb
70
+ - lib/ws_rails_client/version.rb
71
+ homepage: https://github.com/dena-techstudig-2014/ws-rails-client
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: websocket-rails client
95
+ test_files: []