deepstream 0.0.3

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: 27521eb9ef36bc18d1fe6a152a72b434f4f88cfd
4
+ data.tar.gz: eb40eca01f6c60b97334831ac3615ec9795c3888
5
+ SHA512:
6
+ metadata.gz: d23e50627674027fc34eaeb08935eb723fdb5152f3bfdaf3f2082c17be15361fffb26463eb1438f47610f1f64dc5755aae55e4678979683842ff571fb3f5a206
7
+ data.tar.gz: aa5d2e0b062f15130a372a6a23fb3dc3dd285e660cfed7a76cadcd4d4e5da94fb2378bd030e64de71aad0176b37d457faf658c31975635ac3e694fe5b5ebac2a
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,54 @@
1
+ Apache License
2
+
3
+ Version 2.0, January 2004
4
+
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
16
+
17
+ "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
18
+
19
+ "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
20
+
21
+ "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
22
+
23
+ "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
24
+
25
+ "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
26
+
27
+ "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
28
+
29
+ "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
30
+
31
+ 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
32
+
33
+ 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
34
+
35
+ 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
36
+
37
+ You must give any other recipients of the Work or Derivative Works a copy of this License; and
38
+ You must cause any modified files to carry prominent notices stating that You changed the files; and
39
+ You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
40
+ If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
41
+
42
+ You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
43
+
44
+ 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
45
+
46
+ 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
47
+
48
+ 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
49
+
50
+ 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
51
+
52
+ 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
53
+
54
+ END OF TERMS AND CONDITIONS
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # deepstream-ruby
2
+ deepstream.io ruby client
3
+
4
+
5
+ ### Usage
6
+
7
+ ```ruby
8
+ ds = Deepstream::Client.new('localhost')
9
+
10
+ # Emit events
11
+
12
+ ds.emit 'my_event'
13
+ # or
14
+ ds.emit 'my_event', foo: 'bar', bar: 'foo'
15
+
16
+
17
+ # Subscribe to events
18
+ ds.on('some_event') do |msg|
19
+ puts msg
20
+ end
21
+
22
+
23
+ # Get records
24
+ foo = ds.get('foo')
25
+
26
+ # Update record
27
+ foo.bar = 'bar'
28
+ # or
29
+ foo.set('bar', 'bar')
30
+
31
+ # Set whole record
32
+ foo.set(foo: 'foo', bar: 1, )
33
+
34
+ ```
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "deepstream"
7
+ spec.version = "0.0.3"
8
+ spec.authors = ["Piotr Szczudlak"]
9
+ spec.email = ["thisredoned@gmail.com"]
10
+
11
+ spec.summary = %q{deepstream.io ruby client}
12
+ spec.description = %q{Basic ruby client for the deepstream.io server}
13
+ spec.homepage = "https://github.com/thisredone/deepstream-ruby"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+ spec.license = "Apache License 2.0"
20
+ end
data/lib/deepstream.rb ADDED
@@ -0,0 +1,146 @@
1
+ # coding: utf-8
2
+
3
+ # Copyright (c) 2015, Currency-One S.A.
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ # Unless required by applicable law or agreed to in writing, software
9
+ # distributed under the License is distributed on an "AS IS" BASIS,
10
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ # See the License for the specific language governing permissions and
12
+ # limitations under the License.
13
+
14
+
15
+ require 'socket'
16
+ require 'json'
17
+ require 'timeout'
18
+
19
+
20
+ module Deepstream end
21
+
22
+
23
+ class Deepstream::Record
24
+
25
+ def initialize(client, name, data, version)
26
+ @client, @name, @data, @version = client, name, data, version
27
+ end
28
+
29
+ def set(*args)
30
+ if args.size == 1
31
+ @client._write('R', 'U', @name, (@version += 1), JSON.dump(args[0]))
32
+ @data = OpenStruct.new(args[0])
33
+ else
34
+ @client._write('R', 'P', @name, (@version += 1), args[0][0..-2], @client._typed(args[1]))
35
+ end
36
+ end
37
+
38
+ def _patch(version, field, value)
39
+ @version = version.to_i
40
+ @data[field] = value
41
+ end
42
+
43
+ def _update(version, data)
44
+ @version = version.to_i
45
+ @data = data
46
+ end
47
+
48
+ def method_missing(name, *args)
49
+ set(name, *args) if name[-1] == '='
50
+ @data.send(name, *args)
51
+ end
52
+
53
+ end
54
+
55
+
56
+ class Deepstream::Client
57
+
58
+ def initialize(address, port = 6021)
59
+ @address, @port, @unread_msg, @event_callbacks, @records = address, port, nil, {}, {}
60
+ end
61
+
62
+ def emit(event, value = nil)
63
+ _write('E', 'EVT', event, _typed(value))
64
+ end
65
+
66
+ def on(event, &block)
67
+ _write_and_read('E', 'S', event)
68
+ @event_callbacks[event] = block
69
+ end
70
+
71
+ def get(record_name)
72
+ _write_and_read('R', 'CR', record_name)
73
+ msg = _read
74
+ @records[record_name] = Deepstream::Record.new(self, record_name, _parse_data(msg[4]), msg[3].to_i)
75
+ end
76
+
77
+ def _open_socket
78
+ timeout(2) { @socket = TCPSocket.new(@address, @port) }
79
+ Thread.start do
80
+ loop { _process_msg(@socket.gets(30.chr).tap { |m| break m.chomp(30.chr).split(31.chr) if m }) }
81
+ end
82
+ rescue
83
+ print Time.now.to_s[/.+ .+ /], "Can't connect to deepstream server\n"
84
+ raise
85
+ end
86
+
87
+ def _connect
88
+ _open_socket
89
+ @connected = true
90
+ @connected = _write_and_read(%w{A REQ {}}) { |msg| msg == %w{A A} }
91
+ end
92
+
93
+ def _write_and_read(*args)
94
+ @unread_msg = nil
95
+ _write(*args)
96
+ yield _read if block_given?
97
+ end
98
+
99
+ def _write(*args)
100
+ _connect unless @connected
101
+ @socket.write(args.join(31.chr) + 30.chr)
102
+ rescue
103
+ @connected = false
104
+ end
105
+
106
+ def _process_msg(msg)
107
+ case msg[0..1]
108
+ when %w{E EVT} then _fire_event_callback(msg)
109
+ when %w{R P} then @records[msg[2]]._patch(msg[3], msg[4], _parse_data(msg[5]))
110
+ when %w{R U} then @records[msg[2]]._update(msg[3], _parse_data(msg[4]))
111
+ else @unread_msg = msg
112
+ end
113
+ end
114
+
115
+ def _read
116
+ loop { break @unread_msg || (next sleep 0.01) }.tap { @unread_msg = nil }
117
+ end
118
+
119
+ def _fire_event_callback(msg)
120
+ @event_callbacks[msg[2]].tap { |cb| cb.(_parse_data(msg[3])) if cb }
121
+ end
122
+
123
+ def _typed(value)
124
+ case value
125
+ when Hash then "O#{value.to_json}"
126
+ when String then "S#{value}"
127
+ when Numeric then "N#{value}"
128
+ when TrueClass then 'T'
129
+ when FalseClass then 'F'
130
+ when NilClass then 'L'
131
+ end
132
+ end
133
+
134
+ def _parse_data(payload)
135
+ case payload[0]
136
+ when 'O' then JSON.parse(payload[1..-1], object_class: OpenStruct)
137
+ when '{' then JSON.parse(payload, object_class: OpenStruct)
138
+ when 'S' then payload[1..-1]
139
+ when 'N' then payload[1..-1].to_f
140
+ when 'T' then true
141
+ when 'F' then false
142
+ when 'L' then nil
143
+ end
144
+ end
145
+
146
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deepstream
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Szczudlak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Basic ruby client for the deepstream.io server
14
+ email:
15
+ - thisredoned@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - deepstream.gemspec
25
+ - lib/deepstream.rb
26
+ homepage: https://github.com/thisredone/deepstream-ruby
27
+ licenses:
28
+ - Apache License 2.0
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: '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: deepstream.io ruby client
50
+ test_files: []
51
+ has_rdoc: