analytics-ruby 0.1.4 → 0.2.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.
- data/History.md +4 -0
- data/analytics-ruby-0.1.4.gem +0 -0
- data/lib/analytics-ruby.rb +5 -0
- data/lib/analytics-ruby/client.rb +12 -2
- data/lib/analytics-ruby/consumer.rb +23 -6
- data/lib/analytics-ruby/version.rb +1 -1
- data/spec/client_spec.rb +12 -3
- data/spec/consumer_spec.rb +27 -0
- data/spec/module_spec.rb +8 -0
- metadata +13 -12
data/History.md
CHANGED
Binary file
|
data/lib/analytics-ruby.rb
CHANGED
@@ -15,7 +15,7 @@ module AnalyticsRuby
|
|
15
15
|
# :secret - String of your project's secret
|
16
16
|
# :max_queue_size - Fixnum of the max calls to remain queued (optional)
|
17
17
|
# :on_error - Proc which handles error calls from the API
|
18
|
-
def initialize
|
18
|
+
def initialize(options = {})
|
19
19
|
|
20
20
|
@queue = Queue.new
|
21
21
|
@secret = options[:secret]
|
@@ -24,7 +24,17 @@ module AnalyticsRuby
|
|
24
24
|
check_secret
|
25
25
|
|
26
26
|
@consumer = AnalyticsRuby::Consumer.new(@queue, @secret, options)
|
27
|
-
Thread.new { @consumer.run }
|
27
|
+
@thread = Thread.new { @consumer.run }
|
28
|
+
end
|
29
|
+
|
30
|
+
# public: Synchronously waits until the consumer has flushed the queue.
|
31
|
+
# Use only for scripts which are not long-running, and will
|
32
|
+
# specifically exit
|
33
|
+
#
|
34
|
+
def flush
|
35
|
+
while !@queue.empty? || @consumer.is_requesting?
|
36
|
+
sleep(0.1)
|
37
|
+
end
|
28
38
|
end
|
29
39
|
|
30
40
|
# public: Tracks an event
|
@@ -24,6 +24,8 @@ module AnalyticsRuby
|
|
24
24
|
@on_error = options[:on_error] || Proc.new { |status, error| }
|
25
25
|
|
26
26
|
@current_batch = []
|
27
|
+
|
28
|
+
@mutex = Mutex.new
|
27
29
|
end
|
28
30
|
|
29
31
|
# public: Continuously runs the loop to check for new events
|
@@ -37,18 +39,33 @@ module AnalyticsRuby
|
|
37
39
|
# public: Flush some events from our queue
|
38
40
|
#
|
39
41
|
def flush
|
40
|
-
|
41
42
|
# Block until we have something to send
|
42
|
-
|
43
|
+
item = @queue.pop()
|
43
44
|
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
# Synchronize on additions to the current batch
|
46
|
+
@mutex.synchronize {
|
47
|
+
@current_batch << item
|
48
|
+
until @current_batch.length >= @batch_size || @queue.empty?
|
49
|
+
@current_batch << @queue.pop()
|
50
|
+
end
|
51
|
+
}
|
47
52
|
|
48
53
|
req = AnalyticsRuby::Request.new
|
49
54
|
res = req.post(@secret, @current_batch)
|
50
55
|
@on_error.call(res.status, res.error) unless res.status == 200
|
51
|
-
@
|
56
|
+
@mutex.synchronize {
|
57
|
+
@current_batch = []
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
# public: Check whether we have outstanding requests.
|
62
|
+
#
|
63
|
+
def is_requesting?
|
64
|
+
requesting = nil
|
65
|
+
@mutex.synchronize {
|
66
|
+
requesting = !@current_batch.empty?
|
67
|
+
}
|
68
|
+
requesting
|
52
69
|
end
|
53
70
|
|
54
71
|
end
|
data/spec/client_spec.rb
CHANGED
@@ -13,7 +13,6 @@ describe Analytics::Client do
|
|
13
13
|
it 'should not error if a secret is supplied' do
|
14
14
|
Analytics::Client.new secret: AnalyticsHelpers::SECRET
|
15
15
|
end
|
16
|
-
|
17
16
|
end
|
18
17
|
|
19
18
|
describe '#track' do
|
@@ -33,7 +32,6 @@ describe Analytics::Client do
|
|
33
32
|
it 'should not error with the required options' do
|
34
33
|
@client.track AnalyticsHelpers::Queued::TRACK
|
35
34
|
end
|
36
|
-
|
37
35
|
end
|
38
36
|
|
39
37
|
|
@@ -50,9 +48,20 @@ describe Analytics::Client do
|
|
50
48
|
it 'should not error with the required options' do
|
51
49
|
@client.identify AnalyticsHelpers::Queued::IDENTIFY
|
52
50
|
end
|
53
|
-
|
54
51
|
end
|
55
52
|
|
53
|
+
describe '#flush' do
|
54
|
+
before(:all) do
|
55
|
+
@client = Analytics::Client.new secret: AnalyticsHelpers::SECRET
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should wait for the queue to finish on a flush' do
|
59
|
+
@client.identify AnalyticsHelpers::Queued::IDENTIFY
|
60
|
+
@client.track AnalyticsHelpers::Queued::TRACK
|
61
|
+
@client.flush
|
62
|
+
@client.queued_messages.should == 0
|
63
|
+
end
|
64
|
+
end
|
56
65
|
end
|
57
66
|
|
58
67
|
|
data/spec/consumer_spec.rb
CHANGED
@@ -58,4 +58,31 @@ describe Analytics::Consumer do
|
|
58
58
|
queue.should be_empty
|
59
59
|
end
|
60
60
|
end
|
61
|
+
|
62
|
+
describe '#is_requesting?' do
|
63
|
+
|
64
|
+
it 'should not return true if there isn\'t a current batch' do
|
65
|
+
|
66
|
+
queue = Queue.new
|
67
|
+
consumer = Analytics::Consumer.new(queue, 'testsecret')
|
68
|
+
|
69
|
+
consumer.is_requesting?.should == false
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should return true if there is a current batch' do
|
73
|
+
|
74
|
+
queue = Queue.new
|
75
|
+
queue << AnalyticsHelpers::Requested::TRACK
|
76
|
+
consumer = Analytics::Consumer.new(queue, 'testsecret')
|
77
|
+
|
78
|
+
Thread.new {
|
79
|
+
consumer.flush
|
80
|
+
consumer.is_requesting?.should == false
|
81
|
+
}
|
82
|
+
|
83
|
+
# sleep barely long enough to let thread flush the queue.
|
84
|
+
sleep(0.001)
|
85
|
+
consumer.is_requesting?.should == true
|
86
|
+
end
|
87
|
+
end
|
61
88
|
end
|
data/spec/module_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: analytics-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-03-
|
12
|
+
date: 2013-03-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
16
|
-
requirement: &
|
16
|
+
requirement: &10049160 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
version: '0.10'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *10049160
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: faraday_middleware
|
30
|
-
requirement: &
|
30
|
+
requirement: &10048220 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - ! '>='
|
@@ -38,10 +38,10 @@ dependencies:
|
|
38
38
|
version: '0.10'
|
39
39
|
type: :runtime
|
40
40
|
prerelease: false
|
41
|
-
version_requirements: *
|
41
|
+
version_requirements: *10048220
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: multi_json
|
44
|
-
requirement: &
|
44
|
+
requirement: &10047320 !ruby/object:Gem::Requirement
|
45
45
|
none: false
|
46
46
|
requirements:
|
47
47
|
- - ~>
|
@@ -49,10 +49,10 @@ dependencies:
|
|
49
49
|
version: '1.0'
|
50
50
|
type: :runtime
|
51
51
|
prerelease: false
|
52
|
-
version_requirements: *
|
52
|
+
version_requirements: *10047320
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
54
|
name: rake
|
55
|
-
requirement: &
|
55
|
+
requirement: &10046680 !ruby/object:Gem::Requirement
|
56
56
|
none: false
|
57
57
|
requirements:
|
58
58
|
- - ! '>='
|
@@ -60,10 +60,10 @@ dependencies:
|
|
60
60
|
version: '0'
|
61
61
|
type: :development
|
62
62
|
prerelease: false
|
63
|
-
version_requirements: *
|
63
|
+
version_requirements: *10046680
|
64
64
|
- !ruby/object:Gem::Dependency
|
65
65
|
name: rspec
|
66
|
-
requirement: &
|
66
|
+
requirement: &10044520 !ruby/object:Gem::Requirement
|
67
67
|
none: false
|
68
68
|
requirements:
|
69
69
|
- - ! '>='
|
@@ -71,7 +71,7 @@ dependencies:
|
|
71
71
|
version: '0'
|
72
72
|
type: :development
|
73
73
|
prerelease: false
|
74
|
-
version_requirements: *
|
74
|
+
version_requirements: *10044520
|
75
75
|
description: The Segment.io ruby analytics library
|
76
76
|
email: friends@segment.io
|
77
77
|
executables: []
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- lib/analytics-ruby/version.rb
|
89
89
|
- lib/analytics-ruby/json.rb
|
90
90
|
- lib/analytics-ruby/client.rb
|
91
|
+
- analytics-ruby-0.1.4.gem
|
91
92
|
- History.md
|
92
93
|
- analytics-ruby.gemspec
|
93
94
|
- Rakefile
|