piper-ruby 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfc1c00fe2695fa0f233af913096fedc63e00fe5
4
- data.tar.gz: 0a218f8cc28a0019383d2aa7e7a8525bf20ed539
3
+ metadata.gz: 331d325f2fb2468aef24b9ccea3fd8de4a7b0b2a
4
+ data.tar.gz: 4c6af26939954d8055f64b487239184b6d4008e1
5
5
  SHA512:
6
- metadata.gz: 12aa9c159a8b4d6aef85db5b6c08124b5c8a073af68ba7ef3b960d1db0838ceae2ccd4553d1162faadccb0512696d313524b74378e85296b7dbf983a3862876a
7
- data.tar.gz: 931b9df08582a5f6b3cdfc7259f12d7b46b170b36c9ae138b3cf9d33aafc876f44732f8ecd6c773078224731344de3086da6b07d3e78c1d2d9b1a49ef91068fb
6
+ metadata.gz: 00a0790054bba47fe7c9d804757e713fea1f72cef35330e4440956a642232b55a99899773f1c886635ee096597f15bf836f85d0cb50dfa5b23c0752447adca36
7
+ data.tar.gz: 50bd81cafe0874446d3ffc618e91235a640c0ac3923a6f39fa983bc15aae6f3fb97d12facb8aee4ceac6677026186012256e881870fa4410f854cbeff83aa347
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # piper-push-ruby
2
+
2
3
  A Ruby management client to push JSON to [Piper Push Cache](http://www.piperpushcache.com/index.html).
3
4
 
4
5
  [Piper Push Cache](http://www.piperpushcache.com/index.html). is a websocket
@@ -11,3 +12,25 @@ Piper Push Ruby is a thin client that can be used to push JSON to
11
12
  [Piper](http://www.piperpushcache.com/index.html). Raw JSON can be pushed to
12
13
  Piper or Ruby Objects can be pushed and [Oj](http://www.ohler.com/oj) will be
13
14
  used to convert those Objects into JSON using the compat mode.
15
+
16
+ Piper Push Cache has special support for log messages. This gem provides methods
17
+ for sending those log messages to Piper.
18
+
19
+ This Piper client support publishing on [NATS](http://nats.io) as a means of
20
+ delivery if the NATS gem is installed. A flag is also provided to control which
21
+ method is used to deliver log messages to Piper.
22
+
23
+ ## Release Notes
24
+
25
+ ### Release 1.1.0 - November 12, 2015
26
+
27
+ - Added support for publishing to Piper.
28
+
29
+ - Added support for sending log records to Piper.
30
+
31
+ ### Release 1.0.0 - October 8, 2015
32
+
33
+ - Initial release
34
+
35
+
36
+
@@ -4,3 +4,4 @@ end # Piper
4
4
 
5
5
  require 'piper/version'
6
6
  require 'piper/manager'
7
+ require 'piper/logrec'
@@ -0,0 +1,58 @@
1
+
2
+ require 'oj'
3
+
4
+ module Piper
5
+
6
+ # Piper::LogRec is a class for creating JSON log records for Piper Push Cache
7
+ # server.
8
+ class LogRec
9
+ @@level_map = {
10
+ 'fatal' => 0,
11
+ 'error' => 1,
12
+ 'warn' => 2,
13
+ 'info' => 3,
14
+ 'debug' => 4,
15
+ :fatal => 0,
16
+ :error => 1,
17
+ :warn => 2,
18
+ :info => 3,
19
+ :debug => 4,
20
+ }
21
+ @@who = File.basename($PROGRAM_NAME)
22
+
23
+ def self.who()
24
+ return @@who
25
+ end
26
+ attr_reader :kind
27
+ attr_accessor :when
28
+ attr_accessor :who
29
+ attr_accessor :where
30
+ attr_accessor :what
31
+ attr_accessor :tid
32
+
33
+ def initialize(level, what, tid=nil, where=nil)
34
+ @kind = 'Log'
35
+ if level.is_a?(Integer)
36
+ @level = level
37
+ else
38
+ @level = @@level_map[level]
39
+ end
40
+ @who = @@who
41
+ if where.nil?
42
+ @where = "#{File.basename(__FILE__)}:#{__LINE__}"
43
+ else
44
+ @where = where
45
+ end
46
+ @what = what
47
+ @when = Time.now
48
+ @when.gmtime unless @when.gmt?
49
+ @tid = tid
50
+ end
51
+
52
+ def to_s()
53
+ Oj.dump(self, :mode => :compat, :time_format => :unix)
54
+ end
55
+
56
+ end # LogRec
57
+
58
+ end # Piper
@@ -2,6 +2,11 @@
2
2
  require 'net/http'
3
3
  require 'oj'
4
4
 
5
+ begin
6
+ require 'nats/client'
7
+ rescue Exception
8
+ end
9
+
5
10
  module Piper
6
11
 
7
12
  # Piper::Manager is a simple class that is used to manage cached content in a
@@ -14,6 +19,9 @@ module Piper
14
19
  def initialize(host, port)
15
20
  @host = host
16
21
  @port = port
22
+ @prev_log_rid = nil
23
+ @nats_url = nil
24
+ @subject = nil
17
25
  end
18
26
 
19
27
  # Pushes JSON to Piper and associates it with the provided record
@@ -52,6 +60,95 @@ module Piper
52
60
  h.send_request('GET', "/#{rid}")
53
61
  end
54
62
 
63
+ # Sets up NATS. A connection is not made until a log entry is made or a
64
+ # publish is made. The subject should provided will be the prefix for
65
+ # published records and for log messages. Log messages are published on
66
+ # <subject>.log
67
+ # @param [String] url URL to connect to NATS server e.g., nats://localhost:4222
68
+ # @param [String] subject subkect prefix
69
+ def nats_init(url, subject)
70
+ if (defined? NATS)
71
+ @nats_url = url
72
+ @nats_url = 'nats://localhost:4222' if url.nil?
73
+ @subject = subject
74
+ else
75
+ raise Exception.new("NATS gem not installed.")
76
+ end
77
+ end
78
+
79
+ # Publishs a JSON message using a NATS client.
80
+ # @param [String] rid record identifier for the published record
81
+ # @param [Object] object Object to convert to JSON before storing
82
+ def publish(rid, obj)
83
+ NATS.start(:uri => uri) { NATS.publish("#{@subject}.#{rid}", Oj::dump(obj, mode: :compat)) { NATS.stop } }
84
+ end
85
+
86
+ # Publishs a JSON message using a NATS client.
87
+ # @param [String] rid record identifier for the published record
88
+ # @param [String] json json to publish to Piper
89
+ def publish_json(rid, json)
90
+ NATS.start(:uri => uri) { NATS.publish("#{@subject}.#{rid}", json) { NATS.stop } }
91
+ end
92
+
93
+ # Send a log entry to Piper either by a publish over NATS or by a HTTP PUT.
94
+ # @param [Integer|String|Symbol] level severity level (e.g., 1, "error", or :error)
95
+ # @param [String] what reason or contents of the record
96
+ # @param [String] tid tracking identifier if any
97
+ # @param [String] where where the log even was created, default is <file>:<line>
98
+ # @param [String] the record identifier of the created log record.
99
+ def log(level, what, tid=nil, where=nil)
100
+ rec = LogRec.new(level, what, tid, where)
101
+ rid_num = rec.when.strftime('%Y%m%d%H%M%S%N').to_i
102
+ rid_num = @prev_log_rid + 1 if !@prev_log_rid.nil? && @prev_log_rid >= rid_num
103
+ rid = "#{LogRec.who}-#{rid_num}"
104
+ if @subject.nil?
105
+ push_json(rid, rec.to_s)
106
+ else
107
+ NATS.start(:uri => uri) { NATS.publish("#{@subject}.#{rid}", rec.to_s) { NATS.stop } }
108
+ end
109
+ rid
110
+ end
111
+
112
+ # Put or publish a fatal log record.
113
+ # @param [String] what reason or contents of the record
114
+ # @param [String] tid tracking identifier if any
115
+ # @param [String] the record identifier of the created log record.
116
+ def fatal(what, tid=nil)
117
+ log(:fatal, what, tid)
118
+ end
119
+
120
+ # Put or publish a error log record.
121
+ # @param [String] what reason or contents of the record
122
+ # @param [String] tid tracking identifier if any
123
+ # @param [String] the record identifier of the created log record.
124
+ def error(what, tid=nil)
125
+ log(:error, what, tid)
126
+ end
127
+
128
+ # Put or publish a warning log record.
129
+ # @param [String] what reason or contents of the record
130
+ # @param [String] tid tracking identifier if any
131
+ # @param [String] the record identifier of the created log record.
132
+ def warn(what, tid=nil)
133
+ log(:warn, what, tid)
134
+ end
135
+
136
+ # Put or publish a info log record.
137
+ # @param [String] what reason or contents of the record
138
+ # @param [String] tid tracking identifier if any
139
+ # @param [String] the record identifier of the created log record.
140
+ def info(what, tid=nil)
141
+ log(:info, what, tid)
142
+ end
143
+
144
+ # Put or publish a debug log record.
145
+ # @param [String] what reason or contents of the record
146
+ # @param [String] tid tracking identifier if any
147
+ # @param [String] the record identifier of the created log record.
148
+ def debug(what, tid=nil)
149
+ log(:debug, what, tid)
150
+ end
151
+
55
152
  end # Manager
56
153
 
57
154
  end # Piper
@@ -1,4 +1,4 @@
1
1
  module Piper
2
2
  # Current version of the module.
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
  end
@@ -8,6 +8,7 @@ $: << File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'lib')
8
8
  require 'minitest'
9
9
  require 'minitest/autorun'
10
10
  require 'piper'
11
+ require 'oj'
11
12
 
12
13
  $verbose = true
13
14
 
@@ -53,6 +54,15 @@ class PipeTest < Minitest::Test
53
54
  puts " GET /#{iron['id']} response: #{resp.code} - #{resp.body}" if $verbose
54
55
  assert_equal(404, resp.code.to_i, 'expected a 404 when getting iron after delete')
55
56
 
57
+ puts man.log(2, "what happens?")
58
+ resp = man.get('foobar')
59
+ puts " GET /foobar response: #{resp.code} - #{resp.body}"
60
+
61
+ rid = man.info("Done with test.")
62
+ resp = man.get(rid);
63
+ puts " GET /#{rid} response: #{resp.code} - #{resp.body}" if $verbose
64
+ assert_equal(202, resp.code.to_i, 'expected a 202 when getting #{rid} after logging')
65
+
56
66
  end
57
67
 
58
68
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: piper-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ohler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-08 00:00:00.000000000 Z
11
+ date: 2015-11-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Ruby management client to push JSON to Piper Push Cache.
14
14
  email: peter@ohler.com
@@ -20,6 +20,7 @@ files:
20
20
  - LICENSE
21
21
  - README.md
22
22
  - lib/piper.rb
23
+ - lib/piper/logrec.rb
23
24
  - lib/piper/manager.rb
24
25
  - lib/piper/version.rb
25
26
  - test/pipe_test.rb