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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25d5b980ef324ae850b4a96e57c7f55bd4a61539
4
- data.tar.gz: eaea905439f9e4445d7b124e04701bc7cec6f575
3
+ metadata.gz: faf3d3984de6bb4894a85d6f39038256c6073e67
4
+ data.tar.gz: a2b31c5d4786604de19cc0dcf5eea328c00820cc
5
5
  SHA512:
6
- metadata.gz: e9dafea998ef3affcfd00a5427d99ca256bd82e4892b60a7295e4cc024633ee162bb1fe8d36876a500fde79e83a37f37ff6f9ceeff982296d026410cd40968ad
7
- data.tar.gz: 22a853b9b32d680fa05c891366f38d6b798c0a787a5a4be90966d294ca293df4779ccba03fa126a2b64b0175e55fb1a29fe6c0f133a88430347777a15dee9f8c
6
+ metadata.gz: af390121943693dd4d69f8223eb3eaaed249d0ac24092f576e7265274c97841258628a647f4803d6b7608d5f63eef9894523cd934dd38fa6884289e5bdc7d785
7
+ data.tar.gz: fa61f0cffe17e44f862aef545c930546368145710549f9de9845d525c8c41778e89e7654389e76b6dd8795a0c43eb2a713219c47a4d24b78203b9d8c42447367
data/.gitignore CHANGED
@@ -21,3 +21,4 @@ tmp
21
21
  *.o
22
22
  *.a
23
23
  mkmf.log
24
+ .ruby-version
@@ -1,5 +1,5 @@
1
1
  require 'nark/version'
2
- require 'keen'
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
- hash.merge! keen: { timestamp: timestamp } if timestamp
17
- Keen.publish self.class.collection_name, hash
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
@@ -1,3 +1,3 @@
1
1
  module Nark
2
- VERSION = '0.0.3'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -22,5 +22,5 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency 'rspec', '~> 3.0'
23
23
  spec.add_development_dependency 'rubocop'
24
24
 
25
- spec.add_dependency 'keen', '~> 0.8'
25
+ spec.add_dependency 'influxdb', '~> 0.1.8'
26
26
  end
@@ -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
@@ -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
- it 'should emit event to keen' do
15
- expect(Keen).to \
16
- receive(:publish).with :test_signups, user_name: 'everydayhero'
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 to keen with specific timestamp' do
29
+ it 'should emit event with specific timestamp' do
21
30
  time = Time.now
22
- expect(Keen).to \
23
- receive(:publish).with :test_signups,
24
- user_name: 'everydayhero',
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(Keen).to receive :publish
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(Keen).to receive :publish
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.3
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-07-01 00:00:00.000000000 Z
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: keen
70
+ name: influxdb
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.8'
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: '0.8'
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