analytics-ruby 1.1.0 → 2.0.1

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.
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: 1.1.0
4
+ version: 2.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,66 +9,69 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-04-17 00:00:00.000000000 Z
12
+ date: 2014-05-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &11720620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ! '>='
19
+ - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: '10.3'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
24
+ version_requirements: *11720620
25
+ - !ruby/object:Gem::Dependency
26
+ name: wrong
27
+ requirement: &11720120 !ruby/object:Gem::Requirement
25
28
  none: false
26
29
  requirements:
27
- - - ! '>='
30
+ - - ~>
28
31
  - !ruby/object:Gem::Version
29
- version: '0'
32
+ version: '0.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *11720120
30
36
  - !ruby/object:Gem::Dependency
31
37
  name: rspec
32
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &11719660 !ruby/object:Gem::Requirement
33
39
  none: false
34
40
  requirements:
35
- - - ! '>='
41
+ - - ~>
36
42
  - !ruby/object:Gem::Version
37
- version: '0'
43
+ version: '2.0'
38
44
  type: :development
39
45
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
46
+ version_requirements: *11719660
46
47
  description: The Segment.io ruby analytics library
47
48
  email: friends@segment.io
48
49
  executables: []
49
50
  extensions: []
50
51
  extra_rdoc_files: []
51
52
  files:
52
- - analytics-ruby-0.6.0.gem
53
- - analytics-ruby-1.0.0.gem
54
53
  - analytics-ruby.gemspec
55
54
  - Gemfile
55
+ - Gemfile.lock
56
56
  - History.md
57
- - lib/analytics-ruby/client.rb
58
- - lib/analytics-ruby/consumer.rb
59
- - lib/analytics-ruby/defaults.rb
60
- - lib/analytics-ruby/request.rb
61
- - lib/analytics-ruby/response.rb
62
- - lib/analytics-ruby/util.rb
63
- - lib/analytics-ruby/version.rb
64
- - lib/analytics-ruby.rb
57
+ - lib/segment/analytics/client.rb
58
+ - lib/segment/analytics/defaults.rb
59
+ - lib/segment/analytics/logging.rb
60
+ - lib/segment/analytics/request.rb
61
+ - lib/segment/analytics/response.rb
62
+ - lib/segment/analytics/utils.rb
63
+ - lib/segment/analytics/version.rb
64
+ - lib/segment/analytics/worker.rb
65
+ - lib/segment/analytics.rb
66
+ - lib/segment.rb
65
67
  - Makefile
66
68
  - Rakefile
67
69
  - README.md
68
- - spec/client_spec.rb
69
- - spec/consumer_spec.rb
70
- - spec/module_spec.rb
70
+ - spec/segment/analytics/client_spec.rb
71
+ - spec/segment/analytics/worker_spec.rb
72
+ - spec/segment/analytics_spec.rb
71
73
  - spec/spec_helper.rb
74
+ - tags
72
75
  homepage: https://github.com/segmentio/analytics-ruby
73
76
  licenses:
74
77
  - MIT
@@ -90,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
93
  version: '0'
91
94
  requirements: []
92
95
  rubyforge_project:
93
- rubygems_version: 1.8.28
96
+ rubygems_version: 1.8.11
94
97
  signing_key:
95
98
  specification_version: 3
96
99
  summary: Segment.io analytics library
Binary file
Binary file
@@ -1,51 +0,0 @@
1
- require 'analytics-ruby/version'
2
- require 'analytics-ruby/client'
3
-
4
- module AnalyticsRuby
5
- module ClassMethods
6
- # By default use a single client for the module
7
- def init(options = {})
8
- @client = AnalyticsRuby::Client.new options
9
- end
10
-
11
- def track(options)
12
- return false unless @client
13
- @client.track options
14
- end
15
-
16
- def identify(options)
17
- return false unless @client
18
- @client.identify options
19
- end
20
-
21
- def alias(options)
22
- return false unless @client
23
- @client.alias options
24
- end
25
-
26
- def group(options)
27
- return false unless @client
28
- @client.group options
29
- end
30
-
31
- def page(options)
32
- return false unless @client
33
- @client.page options
34
- end
35
-
36
- def screen(options)
37
- return false unless @client
38
- @client.screen options
39
- end
40
-
41
- def flush
42
- return false unless @client
43
- @client.flush
44
- end
45
-
46
- def initialized?
47
- !!@client
48
- end
49
- end
50
- extend ClassMethods
51
- end
@@ -1,311 +0,0 @@
1
-
2
- require 'time'
3
- require 'thread'
4
- require 'analytics-ruby/defaults'
5
- require 'analytics-ruby/consumer'
6
- require 'analytics-ruby/request'
7
- require 'analytics-ruby/util'
8
-
9
- module AnalyticsRuby
10
-
11
- class Client
12
-
13
- # Sub-class thread so we have a named thread (useful for debugging in Thread.list).
14
- class ConsumerThread < Thread
15
- end
16
-
17
- # public: Creates a new client
18
- #
19
- # options - Hash
20
- # :secret - String of your project's secret
21
- # :max_queue_size - Fixnum of the max calls to remain queued (optional)
22
- # :on_error - Proc which handles error calls from the API
23
- def initialize(options = {})
24
-
25
- Util.symbolize_keys! options
26
-
27
- @queue = Queue.new
28
- @secret = options[:secret]
29
- @max_queue_size = options[:max_queue_size] || Defaults::Queue::MAX_SIZE
30
-
31
- check_secret
32
-
33
- @consumer = Consumer.new @queue, @secret, options
34
- @thread = ConsumerThread.new { @consumer.run }
35
-
36
- at_exit do
37
- # Let the consumer thread know it should exit.
38
- @thread[:should_exit] = true
39
-
40
- # Push a flag value to the consumer queue in case it's blocked waiting for a value. This will allow it
41
- # to continue its normal chain of processing, giving it a chance to exit.
42
- @queue << nil
43
- end
44
- end
45
-
46
- # public: Synchronously waits until the consumer has flushed the queue.
47
- # Use only for scripts which are not long-running, and will
48
- # specifically exit
49
- #
50
- def flush
51
- while !@queue.empty? || @consumer.is_requesting?
52
- sleep(0.1)
53
- end
54
- end
55
-
56
- # public: Tracks an event
57
- #
58
- # options - Hash
59
- # :event - String of event name.
60
- # :user_id - String of the user id.
61
- # :properties - Hash of event properties. (optional)
62
- # :timestamp - Time of when the event occurred. (optional)
63
- # :context - Hash of context. (optional)
64
- def track(options)
65
-
66
- check_secret
67
-
68
- Util.symbolize_keys! options
69
-
70
- event = options[:event]
71
- user_id = options[:user_id].to_s
72
- properties = options[:properties] || {}
73
- timestamp = options[:timestamp] || Time.new
74
- context = options[:context] || {}
75
-
76
- ensure_user user_id
77
- check_timestamp timestamp
78
-
79
- if event.nil? || event.empty?
80
- fail ArgumentError, 'Must supply event as a non-empty string'
81
- end
82
-
83
- fail ArgumentError, 'Properties must be a Hash' unless properties.is_a? Hash
84
- Util.isoify_dates! properties
85
-
86
- add_context context
87
-
88
- enqueue({
89
- :event => event,
90
- :userId => user_id,
91
- :context => context,
92
- :properties => properties,
93
- :timestamp => timestamp.iso8601,
94
- :action => 'track'
95
- })
96
- end
97
-
98
- # public: Identifies a user
99
- #
100
- # options - Hash
101
- # :user_id - String of the user id
102
- # :traits - Hash of user traits. (optional)
103
- # :timestamp - Time of when the event occurred. (optional)
104
- # :context - Hash of context. (optional)
105
- def identify(options)
106
-
107
- check_secret
108
-
109
- Util.symbolize_keys! options
110
-
111
- user_id = options[:user_id].to_s
112
- traits = options[:traits] || {}
113
- timestamp = options[:timestamp] || Time.new
114
- context = options[:context] || {}
115
-
116
- ensure_user user_id
117
- check_timestamp timestamp
118
-
119
- fail ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash
120
- Util.isoify_dates! traits
121
-
122
- add_context context
123
-
124
- enqueue({
125
- :userId => user_id,
126
- :context => context,
127
- :traits => traits,
128
- :timestamp => timestamp.iso8601,
129
- :action => 'identify'
130
- })
131
- end
132
-
133
- # public: Aliases a user from one id to another
134
- #
135
- # options - Hash
136
- # :from - String of the id to alias from
137
- # :to - String of the id to alias to
138
- # :timestamp - Time of when the alias occured (optional)
139
- # :context - Hash of context (optional)
140
- def alias(options)
141
- check_secret
142
- Util.symbolize_keys! options
143
- from = options[:from].to_s
144
- to = options[:to].to_s
145
- timestamp = options[:timestamp] || Time.new
146
- context = options[:context] || {}
147
-
148
- ensure_user from
149
- ensure_user to
150
- check_timestamp timestamp
151
- add_context context
152
-
153
- enqueue({
154
- :from => from,
155
- :to => to,
156
- :context => context,
157
- :timestamp => timestamp.iso8601,
158
- :action => 'alias'
159
- })
160
- end
161
-
162
- # public: Associates a user identity with a group.
163
- #
164
- # options - Hash
165
- # :from - String of the id to alias from
166
- # :to - String of the id to alias to
167
- # :timestamp - Time of when the alias occured (optional)
168
- # :context - Hash of context (optional)
169
- def group(options)
170
- check_secret
171
- Util.symbolize_keys! options
172
- group_id = options[:group_id].to_s
173
- user_id = options[:user_id].to_s
174
- traits = options[:traits] || {}
175
- timestamp = options[:timestamp] || Time.new
176
- context = options[:context] || {}
177
-
178
- fail ArgumentError, '.traits must be a hash' unless traits.is_a? Hash
179
-
180
- ensure_user group_id
181
- ensure_user user_id
182
- check_timestamp timestamp
183
- add_context context
184
-
185
- enqueue({
186
- :groupId => group_id,
187
- :userId => user_id,
188
- :traits => traits,
189
- :context => context,
190
- :timestamp => timestamp.iso8601,
191
- :action => 'group'
192
- })
193
- end
194
-
195
- # public: Records a page view
196
- #
197
- # options - Hash
198
- # :user_id - String of the id to alias from
199
- # :name - String name of the page
200
- # :properties - Hash of page properties (optional)
201
- # :timestamp - Time of when the pageview occured (optional)
202
- # :context - Hash of context (optional)
203
- def page(options)
204
- check_secret
205
- Util.symbolize_keys! options
206
- user_id = options[:user_id].to_s
207
- name = options[:name].to_s
208
- properties = options[:properties] || {}
209
- timestamp = options[:timestamp] || Time.new
210
- context = options[:context] || {}
211
-
212
- fail ArgumentError, '.name must be a string' unless !name.empty?
213
- fail ArgumentError, '.properties must be a hash' unless properties.is_a? Hash
214
- Util.isoify_dates! properties
215
-
216
- ensure_user user_id
217
- check_timestamp timestamp
218
- add_context context
219
-
220
- enqueue({
221
- :userId => user_id,
222
- :name => name,
223
- :properties => properties,
224
- :context => context,
225
- :timestamp => timestamp.iso8601,
226
- :action => 'page'
227
- })
228
- end
229
- # public: Records a screen view (for a mobile app)
230
- #
231
- # options - Hash
232
- # :user_id - String of the id to alias from
233
- # :name - String name of the screen
234
- # :properties - Hash of screen properties (optional)
235
- # :timestamp - Time of when the screen occured (optional)
236
- # :context - Hash of context (optional)
237
- def screen(options)
238
- check_secret
239
- Util.symbolize_keys! options
240
- user_id = options[:user_id].to_s
241
- name = options[:name].to_s
242
- properties = options[:properties] || {}
243
- timestamp = options[:timestamp] || Time.new
244
- context = options[:context] || {}
245
-
246
- fail ArgumentError, '.name must be a string' if name.empty?
247
- fail ArgumentError, '.properties must be a hash' unless properties.is_a? Hash
248
- Util.isoify_dates! properties
249
-
250
- ensure_user user_id
251
- check_timestamp timestamp
252
- add_context context
253
-
254
- enqueue({
255
- :userId => user_id,
256
- :name => name,
257
- :properties => properties,
258
- :context => context,
259
- :timestamp => timestamp.iso8601,
260
- :action => 'screen'
261
- })
262
- end
263
-
264
- # public: Returns the number of queued messages
265
- #
266
- # returns Fixnum of messages in the queue
267
- def queued_messages
268
- @queue.length
269
- end
270
-
271
- private
272
-
273
- # private: Enqueues the action.
274
- #
275
- # returns Boolean of whether the item was added to the queue.
276
- def enqueue(action)
277
- # add our request id for tracing purposes
278
- action[:requestId] = Util.uid
279
-
280
- queue_full = @queue.length >= @max_queue_size
281
- @queue << action unless queue_full
282
-
283
- !queue_full
284
- end
285
-
286
- # private: Ensures that a user id was passed in.
287
- #
288
- # user_id - String of the user id
289
- #
290
- def ensure_user(user_id)
291
- fail ArgumentError, 'Must supply a non-empty user_id' if user_id.empty?
292
- end
293
-
294
- # private: Adds contextual information to the call
295
- #
296
- # context - Hash of call context
297
- def add_context(context)
298
- context[:library] = 'analytics-ruby'
299
- end
300
-
301
- # private: Checks that the secret is properly initialized
302
- def check_secret
303
- fail 'Secret must be initialized' if @secret.nil?
304
- end
305
-
306
- # private: Checks the timstamp option to make sure it is a Time.
307
- def check_timestamp(timestamp)
308
- fail ArgumentError, 'Timestamp must be a Time' unless timestamp.is_a? Time
309
- end
310
- end
311
- end