amplitude-api 0.0.5 → 0.0.6

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: 9fa498ab2765d7b0e14d78aacd3caf34d212abde
4
- data.tar.gz: c2cf793047298e2d0679c2ec748eae6d9ae46727
3
+ metadata.gz: a9ac7cdd479dd7fc5397ab04697d01c1a018db1d
4
+ data.tar.gz: 07d0b31956d6c0d5fabf80460733bc2501dd29b8
5
5
  SHA512:
6
- metadata.gz: b73383081f15f0bca95841663f0d58b9bcf863c5a183b18b52400a3148d406721ba21dd8fc6ffaf3ff1debfffe86475d25eee9b623766ac31052579526536416
7
- data.tar.gz: 9bada553602d018841b12bbd6836f338971359316d3852fe4147e08f4b9d8f35918f221e8bfa59cedc877c673b76714c43c9dd3b98ffa9cf9adc82d60ab9fd76
6
+ metadata.gz: e5ba9200d8fc3415f8a6ee3d0450bb0a2849482d8cd8cb7fc5b2caf1cfa5175ee0e1c2f7074cebbf85c0410649ee1044b385c6acece36516f01c713bc2688ea0
7
+ data.tar.gz: b81df95283f2c5c2da128ec00b2ffb40e8b4e76b7ee34b990fd26e0834ac892e7d76a855e0b6b08a265b42c82fd71be725d12df09c112eaa9d84de14b81c6359
@@ -1,2 +1,3 @@
1
+ require: rubocop-rspec
1
2
  Metrics/LineLength:
2
3
  Max: 120
data/Gemfile CHANGED
@@ -8,4 +8,5 @@ group :development, :test do
8
8
  gem 'rake', '>= 10.0'
9
9
  gem 'rspec', '>= 2.99.0'
10
10
  gem 'rubocop', '~> 0.37.2', require: false
11
+ gem 'rubocop-rspec'
11
12
  end
@@ -2,11 +2,11 @@ GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
4
  ast (2.2.0)
5
- coderay (1.1.0)
5
+ coderay (1.1.1)
6
6
  diff-lcs (1.2.5)
7
- ethon (0.7.3)
7
+ ethon (0.8.1)
8
8
  ffi (>= 1.3.0)
9
- ffi (1.9.8)
9
+ ffi (1.9.10)
10
10
  method_source (0.8.2)
11
11
  parser (2.3.0.6)
12
12
  ast (~> 2.2)
@@ -16,25 +16,31 @@ GEM
16
16
  method_source (~> 0.8)
17
17
  slop (~> 3.4)
18
18
  rainbow (2.1.0)
19
- rake (10.4.2)
20
- rspec (2.99.0)
21
- rspec-core (~> 2.99.0)
22
- rspec-expectations (~> 2.99.0)
23
- rspec-mocks (~> 2.99.0)
24
- rspec-core (2.99.2)
25
- rspec-expectations (2.99.2)
26
- diff-lcs (>= 1.1.3, < 2.0)
27
- rspec-mocks (2.99.3)
19
+ rake (10.5.0)
20
+ rspec (3.4.0)
21
+ rspec-core (~> 3.4.0)
22
+ rspec-expectations (~> 3.4.0)
23
+ rspec-mocks (~> 3.4.0)
24
+ rspec-core (3.4.3)
25
+ rspec-support (~> 3.4.0)
26
+ rspec-expectations (3.4.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.4.0)
29
+ rspec-mocks (3.4.1)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.4.0)
32
+ rspec-support (3.4.1)
28
33
  rubocop (0.37.2)
29
34
  parser (>= 2.3.0.4, < 3.0)
30
35
  powerpack (~> 0.1)
31
36
  rainbow (>= 1.99.1, < 3.0)
32
37
  ruby-progressbar (~> 1.7)
33
38
  unicode-display_width (~> 0.3)
39
+ rubocop-rspec (1.4.0)
34
40
  ruby-progressbar (1.7.5)
35
- slop (3.5.0)
36
- typhoeus (0.7.1)
37
- ethon (>= 0.7.1)
41
+ slop (3.6.0)
42
+ typhoeus (0.7.3)
43
+ ethon (>= 0.7.4)
38
44
  unicode-display_width (0.3.1)
39
45
 
40
46
  PLATFORMS
@@ -45,6 +51,7 @@ DEPENDENCIES
45
51
  rake (>= 10.0)
46
52
  rspec (>= 2.99.0)
47
53
  rubocop (~> 0.37.2)
54
+ rubocop-rspec
48
55
  typhoeus (~> 0.7.1)
49
56
 
50
57
  BUNDLED WITH
@@ -22,15 +22,18 @@ class AmplitudeAPI
22
22
  #
23
23
  # @param [ String ] event_name a string that describes the event, e.g. "clicked on Home"
24
24
  # @param [ String ] user a string or integer that uniquely identifies a user.
25
- # @param [ Hash ] properties a hash that is serialized to JSON,
25
+ # @param [ Hash ] event_properties a hash that is serialized to JSON,
26
26
  # and can contain any other property to be stored on the Event
27
+ # @param [ Hash ] user_properties a hash that is serialized to JSON,
28
+ # and contains user properties to be associated with the user
27
29
  #
28
30
  # @return [ Typhoeus::Response ]
29
- def send_event(event_name, user, properties = {})
31
+ def send_event(event_name, user, event_properties: {}, user_properties: {})
30
32
  event = AmplitudeAPI::Event.new(
31
33
  user_id: user,
32
34
  event_type: event_name,
33
- event_properties: properties
35
+ event_properties: event_properties,
36
+ user_properties: user_properties
34
37
  )
35
38
  track(event)
36
39
  end
@@ -10,6 +10,9 @@ class AmplitudeAPI
10
10
  # @!attribute [ rw ] event_properties
11
11
  # @return [ String ] the event_properties to be attached to the Amplitude Event
12
12
  attr_accessor :event_properties
13
+ # @!attribute [ rw ] user_properties
14
+ # @return [ String ] the user_properties to be passed for the user
15
+ attr_accessor :user_properties
13
16
  # @!attribute [ rw ] time
14
17
  # @return [ Time ] Time that the event occurred (defaults to now)
15
18
  attr_accessor :time
@@ -20,10 +23,11 @@ class AmplitudeAPI
20
23
  # @param [ String ] event_type a name for the event
21
24
  # @param [ Hash ] event_properties various properties to attach to the event
22
25
  # @param [ Time ] Time that the event occurred (defaults to now)
23
- def initialize(user_id: '', event_type: '', event_properties: {}, time: nil)
26
+ def initialize(user_id: '', event_type: '', event_properties: {}, user_properties: {}, time: nil)
24
27
  self.user_id = user_id
25
28
  self.event_type = event_type
26
29
  self.event_properties = event_properties
30
+ self.user_properties = user_properties
27
31
  self.time = time
28
32
  end
29
33
 
@@ -44,6 +48,7 @@ class AmplitudeAPI
44
48
  serialized_event[:event_type] = event_type
45
49
  serialized_event[:user_id] = user_id
46
50
  serialized_event[:event_properties] = event_properties
51
+ serialized_event[:user_properties] = user_properties
47
52
  serialized_event[:time] = formatted_time if time
48
53
  serialized_event
49
54
  end
@@ -1,3 +1,3 @@
1
1
  class AmplitudeAPI
2
- VERSION = '0.0.5'.freeze
2
+ VERSION = '0.0.6'.freeze
3
3
  end
data/readme.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # Amplitude API
2
2
  [![Build Status](https://travis-ci.org/toothrot/amplitude-api.svg?branch=master)](https://travis-ci.org/toothrot/amplitude-api)
3
+ [![Code Climate](https://codeclimate.com/github/toothrot/amplitude-api/badges/gpa.svg)](https://codeclimate.com/github/toothrot/amplitude-api)
3
4
  [![Gem Version](https://badge.fury.io/rb/amplitude-api.svg)](http://badge.fury.io/rb/amplitude-api)
4
5
 
5
6
  ## Installation
@@ -1,13 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AmplitudeAPI::Event do
4
- User = Struct.new(:id)
4
+ user = Struct.new(:id)
5
5
 
6
6
  context 'with a user object' do
7
7
  describe '#body' do
8
8
  it "populates with the user's id" do
9
- event = AmplitudeAPI::Event.new(
10
- user_id: User.new(123),
9
+ event = described_class.new(
10
+ user_id: user.new(123),
11
11
  event_type: 'clicked on home'
12
12
  )
13
13
  expect(event.to_hash[:user_id]).to eq(123)
@@ -18,7 +18,7 @@ describe AmplitudeAPI::Event do
18
18
  context 'with a user id' do
19
19
  describe '#body' do
20
20
  it "populates with the user's id" do
21
- event = AmplitudeAPI::Event.new(
21
+ event = described_class.new(
22
22
  user_id: 123,
23
23
  event_type: 'clicked on home'
24
24
  )
@@ -30,7 +30,7 @@ describe AmplitudeAPI::Event do
30
30
  context 'without a user' do
31
31
  describe '#body' do
32
32
  it 'populates with the unknown user' do
33
- event = AmplitudeAPI::Event.new(
33
+ event = described_class.new(
34
34
  user_id: nil,
35
35
  event_type: 'clicked on home'
36
36
  )
@@ -41,7 +41,7 @@ describe AmplitudeAPI::Event do
41
41
 
42
42
  describe '#body' do
43
43
  it 'includes the event type' do
44
- event = AmplitudeAPI::Event.new(
44
+ event = described_class.new(
45
45
  user_id: 123,
46
46
  event_type: 'clicked on home'
47
47
  )
@@ -49,7 +49,7 @@ describe AmplitudeAPI::Event do
49
49
  end
50
50
 
51
51
  it 'includes arbitrary properties' do
52
- event = AmplitudeAPI::Event.new(
52
+ event = described_class.new(
53
53
  user_id: 123,
54
54
  event_type: 'clicked on home',
55
55
  event_properties: { abc: :def }
@@ -59,7 +59,7 @@ describe AmplitudeAPI::Event do
59
59
 
60
60
  it 'includes a time for the event' do
61
61
  time = Time.parse('2016-01-01 00:00:00 -0000')
62
- event = AmplitudeAPI::Event.new(
62
+ event = described_class.new(
63
63
  user_id: 123,
64
64
  event_type: 'clicked on home',
65
65
  time: time
@@ -68,7 +68,7 @@ describe AmplitudeAPI::Event do
68
68
  end
69
69
 
70
70
  it 'does not include time if it is not set' do
71
- event = AmplitudeAPI::Event.new(
71
+ event = described_class.new(
72
72
  user_id: 123,
73
73
  event_type: 'clicked on home'
74
74
  )
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AmplitudeAPI::Identification do
4
- User = Struct.new(:id)
4
+ user = Struct.new(:id)
5
5
 
6
6
  context 'with a user object' do
7
7
  describe '#body' do
8
8
  it "populates with the user's id" do
9
- identification = AmplitudeAPI::Identification.new(user_id: User.new(123))
9
+ identification = described_class.new(user_id: user.new(123))
10
10
  expect(identification.to_hash[:user_id]).to eq(123)
11
11
  end
12
12
  end
@@ -15,7 +15,7 @@ describe AmplitudeAPI::Identification do
15
15
  context 'with a user id' do
16
16
  describe '#body' do
17
17
  it "populates with the user's id" do
18
- identification = AmplitudeAPI::Identification.new(user_id: 123)
18
+ identification = described_class.new(user_id: 123)
19
19
  expect(identification.to_hash[:user_id]).to eq(123)
20
20
  end
21
21
  end
@@ -24,7 +24,7 @@ describe AmplitudeAPI::Identification do
24
24
  context 'without a user' do
25
25
  describe '#body' do
26
26
  it 'populates with the unknown user' do
27
- identification = AmplitudeAPI::Identification.new(user_id: nil)
27
+ identification = described_class.new(user_id: nil)
28
28
  expect(identification.to_hash[:user_id]).to eq(AmplitudeAPI::USER_WITH_NO_ACCOUNT)
29
29
  end
30
30
  end
@@ -32,12 +32,12 @@ describe AmplitudeAPI::Identification do
32
32
 
33
33
  describe '#body' do
34
34
  it 'includes the user id' do
35
- identification = AmplitudeAPI::Identification.new(user_id: 123)
35
+ identification = described_class.new(user_id: 123)
36
36
  expect(identification.to_hash[:user_id]).to eq(123)
37
37
  end
38
38
 
39
39
  it 'includes arbitrary user properties' do
40
- identification = AmplitudeAPI::Identification.new(
40
+ identification = described_class.new(
41
41
  user_id: 123,
42
42
  user_properties: {
43
43
  first_name: 'John',
@@ -1,9 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe AmplitudeAPI do
4
- before do
5
- @user = Struct.new(:id).new(123)
6
- end
4
+ let(:user) { Struct.new(:id).new(123) }
7
5
 
8
6
  describe '.track' do
9
7
  context 'with a single event' do
@@ -13,13 +11,13 @@ describe AmplitudeAPI do
13
11
  event_type: 'clicked on sign up'
14
12
  )
15
13
  body = {
16
- api_key: AmplitudeAPI.api_key,
14
+ api_key: described_class.api_key,
17
15
  event: JSON.generate([event.to_hash])
18
16
  }
19
17
 
20
18
  expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
21
19
 
22
- AmplitudeAPI.track(event)
20
+ described_class.track(event)
23
21
  end
24
22
  end
25
23
 
@@ -34,13 +32,13 @@ describe AmplitudeAPI do
34
32
  event_type: 'liked a widget'
35
33
  )
36
34
  body = {
37
- api_key: AmplitudeAPI.api_key,
35
+ api_key: described_class.api_key,
38
36
  event: JSON.generate([event.to_hash, event2.to_hash])
39
37
  }
40
38
 
41
39
  expect(Typhoeus).to receive(:post).with(AmplitudeAPI::TRACK_URI_STRING, body: body)
42
40
 
43
- AmplitudeAPI.track([event, event2])
41
+ described_class.track([event, event2])
44
42
  end
45
43
  end
46
44
  end
@@ -56,13 +54,13 @@ describe AmplitudeAPI do
56
54
  }
57
55
  )
58
56
  body = {
59
- api_key: AmplitudeAPI.api_key,
57
+ api_key: described_class.api_key,
60
58
  identification: JSON.generate([identification.to_hash])
61
59
  }
62
60
 
63
61
  expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
64
62
 
65
- AmplitudeAPI.identify(identification)
63
+ described_class.identify(identification)
66
64
  end
67
65
  end
68
66
 
@@ -83,13 +81,13 @@ describe AmplitudeAPI do
83
81
  }
84
82
  )
85
83
  body = {
86
- api_key: AmplitudeAPI.api_key,
84
+ api_key: described_class.api_key,
87
85
  identification: JSON.generate([identification.to_hash, identification2.to_hash])
88
86
  }
89
87
 
90
88
  expect(Typhoeus).to receive(:post).with(AmplitudeAPI::IDENTIFY_URI_STRING, body: body)
91
89
 
92
- AmplitudeAPI.identify([identification, identification2])
90
+ described_class.identify([identification, identification2])
93
91
  end
94
92
  end
95
93
  end
@@ -100,7 +98,8 @@ describe AmplitudeAPI do
100
98
  expect(event.to_hash).to eq(
101
99
  event_type: '',
102
100
  user_id: '',
103
- event_properties: {}
101
+ event_properties: {},
102
+ user_properties: {}
104
103
  )
105
104
  end
106
105
 
@@ -115,7 +114,8 @@ describe AmplitudeAPI do
115
114
  expect(event.to_hash).to eq(
116
115
  event_type: 'test_event',
117
116
  user_id: 123,
118
- event_properties: { test_property: 1 }
117
+ event_properties: { test_property: 1 },
118
+ user_properties: {}
119
119
  )
120
120
  end
121
121
  end
@@ -123,13 +123,13 @@ describe AmplitudeAPI do
123
123
  describe '.send_event' do
124
124
  it 'sends an event to AmplitudeAPI' do
125
125
  event = AmplitudeAPI::Event.new(
126
- user_id: @user,
126
+ user_id: user,
127
127
  event_type: 'test_event',
128
128
  event_properties: { test_property: 1 }
129
129
  )
130
- expect(AmplitudeAPI).to receive(:track).with(event)
130
+ expect(described_class).to receive(:track).with(event)
131
131
 
132
- AmplitudeAPI.send_event('test_event', @user, test_property: 1)
132
+ described_class.send_event('test_event', user, event_properties: { test_property: 1 })
133
133
  end
134
134
 
135
135
  context 'the user is nil' do
@@ -139,9 +139,9 @@ describe AmplitudeAPI do
139
139
  event_type: 'test_event',
140
140
  event_properties: { test_property: 1 }
141
141
  )
142
- expect(AmplitudeAPI).to receive(:track).with(event)
142
+ expect(described_class).to receive(:track).with(event)
143
143
 
144
- AmplitudeAPI.send_event('test_event', nil, test_property: 1)
144
+ described_class.send_event('test_event', nil, event_properties: { test_property: 1 })
145
145
  end
146
146
  end
147
147
 
@@ -152,9 +152,26 @@ describe AmplitudeAPI do
152
152
  event_type: 'test_event',
153
153
  event_properties: { test_property: 1 }
154
154
  )
155
- expect(AmplitudeAPI).to receive(:track).with(event)
155
+ expect(described_class).to receive(:track).with(event)
156
156
 
157
- AmplitudeAPI.send_event('test_event', @user.id, test_property: 1)
157
+ described_class.send_event('test_event', user.id, event_properties: { test_property: 1 })
158
+ end
159
+
160
+ it 'sends arbitrary user_properties to AmplitudeAPI' do
161
+ event = AmplitudeAPI::Event.new(
162
+ user_id: 123,
163
+ event_type: 'test_event',
164
+ event_properties: { test_property: 1 },
165
+ user_properties: { test_user_property: 'abc' }
166
+ )
167
+ expect(described_class).to receive(:track).with(event)
168
+
169
+ described_class.send_event(
170
+ 'test_event',
171
+ user.id,
172
+ event_properties: { test_property: 1 },
173
+ user_properties: { test_user_property: 'abc' }
174
+ )
158
175
  end
159
176
  end
160
177
  end
@@ -162,15 +179,15 @@ describe AmplitudeAPI do
162
179
  describe '.send_identify' do
163
180
  it 'sends an identify to AmplitudeAPI' do
164
181
  identification = AmplitudeAPI::Identification.new(
165
- user_id: @user,
182
+ user_id: user,
166
183
  user_properties: {
167
184
  first_name: 'John',
168
185
  last_name: 'Doe'
169
186
  }
170
187
  )
171
- expect(AmplitudeAPI).to receive(:identify).with(identification)
188
+ expect(described_class).to receive(:identify).with(identification)
172
189
 
173
- AmplitudeAPI.send_identify(@user, first_name: 'John', last_name: 'Doe')
190
+ described_class.send_identify(user, first_name: 'John', last_name: 'Doe')
174
191
  end
175
192
 
176
193
  context 'the user is nil' do
@@ -182,9 +199,9 @@ describe AmplitudeAPI do
182
199
  last_name: 'Doe'
183
200
  }
184
201
  )
185
- expect(AmplitudeAPI).to receive(:identify).with(identification)
202
+ expect(described_class).to receive(:identify).with(identification)
186
203
 
187
- AmplitudeAPI.send_identify(nil, first_name: 'John', last_name: 'Doe')
204
+ described_class.send_identify(nil, first_name: 'John', last_name: 'Doe')
188
205
  end
189
206
  end
190
207
 
@@ -197,35 +214,38 @@ describe AmplitudeAPI do
197
214
  last_name: 'Doe'
198
215
  }
199
216
  )
200
- expect(AmplitudeAPI).to receive(:identify).with(identification)
217
+ expect(described_class).to receive(:identify).with(identification)
201
218
 
202
- AmplitudeAPI.send_identify(@user.id, first_name: 'John', last_name: 'Doe')
219
+ described_class.send_identify(user.id, first_name: 'John', last_name: 'Doe')
203
220
  end
204
221
  end
205
222
  end
206
223
 
207
224
  describe '#body' do
208
- it 'should add an api key' do
225
+ it 'adds an api key' do
209
226
  event = AmplitudeAPI::Event.new(
210
- user_id: @user,
227
+ user_id: user,
211
228
  event_type: 'test_event',
212
229
  event_properties: {
213
230
  test_property: 1
214
231
  }
215
232
  )
216
- body = AmplitudeAPI.track_body(event)
233
+ body = described_class.track_body(event)
217
234
  expect(body[:api_key]).to eq('stub api key')
218
235
  end
219
236
 
220
- it 'should create an event' do
237
+ it 'creates an event' do
221
238
  event = AmplitudeAPI::Event.new(
222
239
  user_id: 23,
223
240
  event_type: 'test_event',
224
241
  event_properties: {
225
242
  foo: 'bar'
243
+ },
244
+ user_properties: {
245
+ abc: '123'
226
246
  }
227
247
  )
228
- body = AmplitudeAPI.track_body(event)
248
+ body = described_class.track_body(event)
229
249
 
230
250
  expected = JSON.generate(
231
251
  [
@@ -234,6 +254,9 @@ describe AmplitudeAPI do
234
254
  user_id: 23,
235
255
  event_properties: {
236
256
  foo: 'bar'
257
+ },
258
+ user_properties: {
259
+ abc: '123'
237
260
  }
238
261
  }
239
262
  ]
@@ -7,7 +7,6 @@ require 'pry'
7
7
  #
8
8
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
9
  RSpec.configure do |config|
10
- config.treat_symbols_as_metadata_keys_with_true_values = true
11
10
  config.run_all_when_everything_filtered = true
12
11
  config.filter_run :focus
13
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amplitude-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Rakoczy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-21 00:00:00.000000000 Z
11
+ date: 2016-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -101,8 +101,8 @@ files:
101
101
  - lib/amplitude_api/identification.rb
102
102
  - lib/amplitude_api/version.rb
103
103
  - readme.md
104
- - spec/lib/amplitude-api/event_spec.rb
105
- - spec/lib/amplitude-api/identification_spec.rb
104
+ - spec/lib/amplitude_api/event_spec.rb
105
+ - spec/lib/amplitude_api/identification_spec.rb
106
106
  - spec/lib/amplitude_api_spec.rb
107
107
  - spec/spec_helper.rb
108
108
  homepage: https://github.com/toothrot/amplitude-api
@@ -130,7 +130,7 @@ signing_key:
130
130
  specification_version: 4
131
131
  summary: Send events to the Amplitude API
132
132
  test_files:
133
- - spec/lib/amplitude-api/event_spec.rb
134
- - spec/lib/amplitude-api/identification_spec.rb
133
+ - spec/lib/amplitude_api/event_spec.rb
134
+ - spec/lib/amplitude_api/identification_spec.rb
135
135
  - spec/lib/amplitude_api_spec.rb
136
136
  - spec/spec_helper.rb