nark 0.0.3 → 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 +4 -4
- data/.gitignore +1 -0
- data/lib/nark.rb +40 -3
- data/lib/nark/influxdb_emitter.rb +25 -0
- data/lib/nark/version.rb +1 -1
- data/nark.gemspec +1 -1
- data/spec/influxdb_emitter_spec.rb +39 -0
- data/spec/nark_spec.rb +45 -11
- metadata +8 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: faf3d3984de6bb4894a85d6f39038256c6073e67
|
4
|
+
data.tar.gz: a2b31c5d4786604de19cc0dcf5eea328c00820cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af390121943693dd4d69f8223eb3eaaed249d0ac24092f576e7265274c97841258628a647f4803d6b7608d5f63eef9894523cd934dd38fa6884289e5bdc7d785
|
7
|
+
data.tar.gz: fa61f0cffe17e44f862aef545c930546368145710549f9de9845d525c8c41778e89e7654389e76b6dd8795a0c43eb2a713219c47a4d24b78203b9d8c42447367
|
data/.gitignore
CHANGED
data/lib/nark.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'nark/version'
|
2
|
-
require '
|
2
|
+
require 'nark/influxdb_emitter'
|
3
3
|
|
4
4
|
module Nark
|
5
5
|
def self.included(klass)
|
@@ -13,15 +13,52 @@ module Nark
|
|
13
13
|
|
14
14
|
def emit(timestamp: nil)
|
15
15
|
hash = serializable_hash.clone
|
16
|
-
|
17
|
-
|
16
|
+
Nark.emitter.emit(collection_name, hash, timestamp)
|
17
|
+
|
18
18
|
self
|
19
19
|
end
|
20
20
|
|
21
|
+
def collection_name(name = nil)
|
22
|
+
@collection_name = name if name
|
23
|
+
@collection_name ||= self.class.collection_name
|
24
|
+
|
25
|
+
@collection_name
|
26
|
+
end
|
27
|
+
|
21
28
|
module ClassMethods
|
22
29
|
def collection_name(value = nil)
|
23
30
|
@collection_name = value if value
|
24
31
|
@collection_name
|
25
32
|
end
|
33
|
+
|
34
|
+
def emit(narks = {})
|
35
|
+
return if narks.empty?
|
36
|
+
|
37
|
+
nark_hash =
|
38
|
+
narks.each_with_object(Hash.new { |h, k| h[k] = [] }) do |nark, hash|
|
39
|
+
hash[nark.collection_name] << nark.serializable_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
Nark.emitter.emit_bulk(nark_hash)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class << self
|
47
|
+
attr_writer :emitter
|
48
|
+
|
49
|
+
def configure(&block)
|
50
|
+
block.call self
|
51
|
+
end
|
52
|
+
|
53
|
+
def emitter
|
54
|
+
@emitter ||= InfluxDBEmitter.new(
|
55
|
+
ENV['INFLUXDB_DATABASE'],
|
56
|
+
username: ENV['INFLUXDB_USERNAME'],
|
57
|
+
password: ENV['INFLUXDB_PASSWORD'],
|
58
|
+
hosts: ENV['INFLUXDB_HOST'],
|
59
|
+
port: ENV['INFLUXDB_PORT'],
|
60
|
+
use_ssl: ENV['INFLUXDB_USE_SSL']
|
61
|
+
)
|
62
|
+
end
|
26
63
|
end
|
27
64
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'influxdb'
|
2
|
+
|
3
|
+
module Nark
|
4
|
+
class InfluxDBEmitter
|
5
|
+
def initialize(*args)
|
6
|
+
@influxdb_client = InfluxDB::Client.new(*args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def emit(collection_name, data, timestamp = nil)
|
10
|
+
data.merge!(time: timestamp.to_i) if timestamp
|
11
|
+
|
12
|
+
influxdb_client.write_point(collection_name, data)
|
13
|
+
end
|
14
|
+
|
15
|
+
def emit_bulk(data_hash)
|
16
|
+
data_hash.each do |collection_name, data|
|
17
|
+
influxdb_client.write_point(collection_name, data)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :influxdb_client
|
24
|
+
end
|
25
|
+
end
|
data/lib/nark/version.rb
CHANGED
data/nark.gemspec
CHANGED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'nark'
|
2
|
+
|
3
|
+
describe Nark::InfluxDBEmitter do
|
4
|
+
let(:client) { Object }
|
5
|
+
let(:emitter) do
|
6
|
+
Nark::InfluxDBEmitter.new.tap do |emitter|
|
7
|
+
emitter.instance_variable_set :@influxdb_client, client
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should write data to right collection on influxdb' do
|
12
|
+
data = { hello: 'hi' }
|
13
|
+
expect(client).to \
|
14
|
+
receive(:write_point).with :collection, data
|
15
|
+
|
16
|
+
emitter.emit :collection, data
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should write data to collection with correct time' do
|
20
|
+
data = { hello: 'hi' }
|
21
|
+
time = Time.now
|
22
|
+
expect(client).to \
|
23
|
+
receive(:write_point).with :collection, data.merge(time: time.to_i)
|
24
|
+
|
25
|
+
emitter.emit :collection, data, time
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should bulk emit data to multiple collections' do
|
29
|
+
data = { hello: 'hi', time: Time.now.to_i }
|
30
|
+
events = {
|
31
|
+
c_1: [data],
|
32
|
+
c_2: [data, data]
|
33
|
+
}
|
34
|
+
expect(client).to receive(:write_point).with :c_1, [data]
|
35
|
+
expect(client).to receive(:write_point).with :c_2, [data, data]
|
36
|
+
|
37
|
+
emitter.emit_bulk events
|
38
|
+
end
|
39
|
+
end
|
data/spec/nark_spec.rb
CHANGED
@@ -5,37 +5,71 @@ class TestSignup
|
|
5
5
|
|
6
6
|
collection_name :test_signups
|
7
7
|
|
8
|
-
def initialize(attributes)
|
8
|
+
def initialize(attributes = {})
|
9
9
|
serializable_hash attributes
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
describe TestSignup do
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
let(:emitter) { Object }
|
15
|
+
|
16
|
+
before do
|
17
|
+
Nark.configure do |nark|
|
18
|
+
nark.emitter = emitter
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should emit event to emitter' do
|
23
|
+
expect(emitter).to \
|
24
|
+
receive(:emit).with :test_signups, { user_name: 'everydayhero' }, nil
|
25
|
+
|
17
26
|
TestSignup.new(user_name: 'everydayhero').emit
|
18
27
|
end
|
19
28
|
|
20
|
-
it 'should emit event
|
29
|
+
it 'should emit event with specific timestamp' do
|
21
30
|
time = Time.now
|
22
|
-
expect(
|
23
|
-
receive(:
|
24
|
-
|
25
|
-
keen: { timestamp: time }
|
31
|
+
expect(emitter).to \
|
32
|
+
receive(:emit).with :test_signups, { user_name: 'everydayhero' }, time
|
33
|
+
|
26
34
|
TestSignup.new(user_name: 'everydayhero').emit timestamp: time
|
27
35
|
end
|
28
36
|
|
37
|
+
it 'should emit event with specific collection_name' do
|
38
|
+
expect(emitter).to \
|
39
|
+
receive(:emit).with 'signup2', { user_name: 'everydayhero' }, nil
|
40
|
+
|
41
|
+
TestSignup.new(user_name: 'everydayhero').tap do |signup|
|
42
|
+
signup.collection_name 'signup2'
|
43
|
+
end.emit
|
44
|
+
end
|
45
|
+
|
29
46
|
it 'should not mutate serializable_hash' do
|
30
|
-
allow(
|
47
|
+
allow(emitter).to receive :emit
|
31
48
|
signup = TestSignup.new({})
|
32
49
|
signup.emit timestamp: Time.now
|
33
50
|
expect(signup.serializable_hash).to eq({})
|
34
51
|
end
|
35
52
|
|
36
53
|
it 'should emit returning self' do
|
37
|
-
allow(
|
54
|
+
allow(emitter).to receive :emit
|
38
55
|
signup = TestSignup.new({})
|
56
|
+
|
39
57
|
expect(signup.emit).to eq(signup)
|
40
58
|
end
|
59
|
+
|
60
|
+
it 'should emit mutiple events based on the configured collection_name' do
|
61
|
+
bulk_events_data = {
|
62
|
+
test_signups: [{}],
|
63
|
+
signup_1: [{}, {}]
|
64
|
+
}
|
65
|
+
expect(emitter).to receive(:emit_bulk).with bulk_events_data
|
66
|
+
|
67
|
+
events = [
|
68
|
+
TestSignup.new,
|
69
|
+
TestSignup.new.tap { |event| event.collection_name :signup_1 },
|
70
|
+
TestSignup.new.tap { |event| event.collection_name :signup_1 }
|
71
|
+
]
|
72
|
+
|
73
|
+
TestSignup.emit(events)
|
74
|
+
end
|
41
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Ryall
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,19 +67,19 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: influxdb
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
75
|
+
version: 0.1.8
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
82
|
+
version: 0.1.8
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- mark@ryall.name
|
@@ -94,8 +94,10 @@ files:
|
|
94
94
|
- README.md
|
95
95
|
- Rakefile
|
96
96
|
- lib/nark.rb
|
97
|
+
- lib/nark/influxdb_emitter.rb
|
97
98
|
- lib/nark/version.rb
|
98
99
|
- nark.gemspec
|
100
|
+
- spec/influxdb_emitter_spec.rb
|
99
101
|
- spec/nark_spec.rb
|
100
102
|
homepage: https://github.com/everydayhero/nark
|
101
103
|
licenses:
|
@@ -122,4 +124,5 @@ signing_key:
|
|
122
124
|
specification_version: 4
|
123
125
|
summary: abstraction layer for publishing analytics events.
|
124
126
|
test_files:
|
127
|
+
- spec/influxdb_emitter_spec.rb
|
125
128
|
- spec/nark_spec.rb
|