piper-ruby 1.0.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: cfc1c00fe2695fa0f233af913096fedc63e00fe5
4
+ data.tar.gz: 0a218f8cc28a0019383d2aa7e7a8525bf20ed539
5
+ SHA512:
6
+ metadata.gz: 12aa9c159a8b4d6aef85db5b6c08124b5c8a073af68ba7ef3b960d1db0838ceae2ccd4553d1162faadccb0512696d313524b74378e85296b7dbf983a3862876a
7
+ data.tar.gz: 931b9df08582a5f6b3cdfc7259f12d7b46b170b36c9ae138b3cf9d33aafc876f44732f8ecd6c773078224731344de3086da6b07d3e78c1d2d9b1a49ef91068fb
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Peter Ohler
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # piper-push-ruby
2
+ A Ruby management client to push JSON to [Piper Push Cache](http://www.piperpushcache.com/index.html).
3
+
4
+ [Piper Push Cache](http://www.piperpushcache.com/index.html). is a websocket
5
+ based push cache. A JSON cache is maintained that can be updated via a REST HTTP
6
+ API or through alternative means. This cache is pushed through websockets to
7
+ browsers using a class and object based subscription protocol that mirrors the
8
+ objects in the REST API. Piper is also a simple web server.
9
+
10
+ Piper Push Ruby is a thin client that can be used to push JSON to
11
+ [Piper](http://www.piperpushcache.com/index.html). Raw JSON can be pushed to
12
+ Piper or Ruby Objects can be pushed and [Oj](http://www.ohler.com/oj) will be
13
+ used to convert those Objects into JSON using the compat mode.
@@ -0,0 +1,57 @@
1
+
2
+ require 'net/http'
3
+ require 'oj'
4
+
5
+ module Piper
6
+
7
+ # Piper::Manager is a simple class that is used to manage cached content in a
8
+ # Piper Push Cache server. It uses HTTP request in a REST pattern.
9
+ class Manager
10
+
11
+ attr_accessor :host
12
+ attr_accessor :port
13
+
14
+ def initialize(host, port)
15
+ @host = host
16
+ @port = port
17
+ end
18
+
19
+ # Pushes JSON to Piper and associates it with the provided record
20
+ # identifier.
21
+ # @param [String] rid record identifier
22
+ # @param [String] json JSON to store in Piper
23
+ # @return [Net::HTTP::Response] of the send request.
24
+ def push_json(rid, json)
25
+ h = Net::HTTP.new(@host, @port)
26
+ h.send_request('PUT', "/#{rid}", json, {'Content-Type' => 'text/plain; charset=utf-8'})
27
+ end
28
+
29
+ # Converts an object to JSON and then pushed that JSON to Piper and
30
+ # associates it with the provided record identifier.
31
+ # @param [String] rid record identifier
32
+ # @param [Object] object Object to convert to JSON before storing
33
+ # @return [Net::HTTP::Response] of the send request.
34
+ def push(rid, obj)
35
+ push_json(rid, Oj::dump(obj, mode: :compat))
36
+ end
37
+
38
+ # Deletes a record from the Piper cache.
39
+ # @param [String] rid record identifier of the record to delete
40
+ # @return [Net::HTTP::Response] of the send request.
41
+ def delete(rid)
42
+ h = Net::HTTP.new(@host, @port)
43
+ h.send_request('DELETE', "/#{rid}")
44
+ end
45
+
46
+ # Gets a record from the Piper cache. The JSON will be in the body of the
47
+ # response if successful.
48
+ # @param [String] rid record identifier of the record to get
49
+ # @return [Net::HTTP::Response] of the send request.
50
+ def get(rid)
51
+ h = Net::HTTP.new(@host, @port)
52
+ h.send_request('GET', "/#{rid}")
53
+ end
54
+
55
+ end # Manager
56
+
57
+ end # Piper
@@ -0,0 +1,4 @@
1
+ module Piper
2
+ # Current version of the module.
3
+ VERSION = '1.0.0'
4
+ end
data/lib/piper.rb ADDED
@@ -0,0 +1,6 @@
1
+
2
+ module Piper
3
+ end # Piper
4
+
5
+ require 'piper/version'
6
+ require 'piper/manager'
data/test/pipe_test.rb ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $VERBOSE = true
5
+
6
+ $: << File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'lib')
7
+
8
+ require 'minitest'
9
+ require 'minitest/autorun'
10
+ require 'piper'
11
+
12
+ $verbose = true
13
+
14
+ class PipeTest < Minitest::Test
15
+
16
+ # Steps need to be done in order since they modify and then inspect the remote
17
+ # piper cache.
18
+ #
19
+ # Before running the tests, start the connection_count sample in Piper Push
20
+ # Cache. Piper Push Cache can be downloaded from piperpushcache.com. Follow
21
+ # the instructions there to start the sample.
22
+ def test_all
23
+ man = Piper::Manager.new('localhost', 7661)
24
+ iron = {
25
+ 'class' => 'Pipe',
26
+ 'id' => 'pipe-iron',
27
+ 'color' => 'gray',
28
+ 'hot-ok' => true
29
+ }
30
+ resp = man.push(iron['id'], iron);
31
+ puts " PUT /#{iron['id']} response: #{resp.code} - #{resp.body}" if $verbose
32
+ assert_equal(202, resp.code.to_i, 'expected a 202 when pushing iron')
33
+
34
+ black = {
35
+ 'class' => 'Pipe',
36
+ 'id' => 'pipe-black',
37
+ 'color' => 'black',
38
+ 'hot-ok' => true
39
+ }
40
+ resp = man.push(black['id'], black);
41
+ puts " PUT /#{black['id']} response: #{resp.code} - #{resp.body}" if $verbose
42
+ assert_equal(202, resp.code.to_i, 'expected a 202 when pushing black')
43
+
44
+ resp = man.get(iron['id']);
45
+ puts " GET /#{iron['id']} response: #{resp.code} - #{resp.body}" if $verbose
46
+ assert_equal(202, resp.code.to_i, 'expected a 202 when getting iron')
47
+
48
+ resp = man.delete(iron['id']);
49
+ puts " DELETE /#{iron['id']} response: #{resp.code} - #{resp.body}" if $verbose
50
+ assert_equal(202, resp.code.to_i, 'expected a 202 when deleting iron')
51
+
52
+ resp = man.get(iron['id']);
53
+ puts " GET /#{iron['id']} response: #{resp.code} - #{resp.body}" if $verbose
54
+ assert_equal(404, resp.code.to_i, 'expected a 404 when getting iron after delete')
55
+
56
+ end
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: piper-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter Ohler
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby management client to push JSON to Piper Push Cache.
14
+ email: peter@ohler.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files:
18
+ - README.md
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/piper.rb
23
+ - lib/piper/manager.rb
24
+ - lib/piper/version.rb
25
+ - test/pipe_test.rb
26
+ homepage: http://www.piperpushcache.com
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options:
32
+ - "--main"
33
+ - README.md
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project: piper-ruby
48
+ rubygems_version: 2.4.5
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Management client for Piper Push Cache.
52
+ test_files: []
53
+ has_rdoc: true