amplitude-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 481d091552b42dfbe151aa5e58e50dedf2436d38
4
+ data.tar.gz: 8b391658f981b64f12d0fd683f1cc996a215dd36
5
+ SHA512:
6
+ metadata.gz: 6418d071d74f61e23b996c32a04929f70efe45a9df7ffd194f956214a855ab4a3033f90a43dd1726096b382833abf8bea45c4d319dca20fea6da621ebd0e24a4
7
+ data.tar.gz: 8b1324f82be861baf1e40e90e3e5ed861fffc5fbeac7fecc1b515849a7f240cdac8fc3491bf7159e660f9c9b5ae1ecbfb846236e96eeb0c1a2eee4b812dbf43e
data/.gitignore ADDED
@@ -0,0 +1,35 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /vendor/bundle
26
+ /lib/bundler/man/
27
+
28
+ # for a library or gem, you might want to ignore these files since the code is
29
+ # intended to run in multiple environments; otherwise, check them in:
30
+ # Gemfile.lock
31
+ # .ruby-version
32
+ # .ruby-gemset
33
+
34
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.2.2
5
+ - 2.1.6
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # Generated from /Users/alex/development/amplitude-api/amplitude-api.gemspec
2
+ source 'https://rubygems.org'
3
+ gem "typhoeus", "~> 0.7.1"
4
+
5
+ group :development do
6
+ gem "rspec", ">= 2.99.0"
7
+ gem "rake", ">= 10.0"
8
+ gem "pry", "~> 0.9.12.6"
9
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ coderay (1.1.0)
5
+ diff-lcs (1.2.5)
6
+ ethon (0.7.3)
7
+ ffi (>= 1.3.0)
8
+ ffi (1.9.8)
9
+ method_source (0.8.2)
10
+ pry (0.9.12.6)
11
+ coderay (~> 1.0)
12
+ method_source (~> 0.8)
13
+ slop (~> 3.4)
14
+ rake (10.4.2)
15
+ rspec (2.99.0)
16
+ rspec-core (~> 2.99.0)
17
+ rspec-expectations (~> 2.99.0)
18
+ rspec-mocks (~> 2.99.0)
19
+ rspec-core (2.99.2)
20
+ rspec-expectations (2.99.2)
21
+ diff-lcs (>= 1.1.3, < 2.0)
22
+ rspec-mocks (2.99.3)
23
+ slop (3.5.0)
24
+ typhoeus (0.7.1)
25
+ ethon (>= 0.7.1)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ pry (~> 0.9.12.6)
32
+ rake (>= 10.0)
33
+ rspec (>= 2.99.0)
34
+ typhoeus (~> 0.7.1)
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 alexander rakoczy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task(:default).clear
10
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'amplitude-api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "amplitude-api"
8
+ spec.version = AmplitudeAPI::VERSION
9
+ spec.authors = ["Alex Rakoczy"]
10
+ spec.email = ["arakoczy@gmail.com"]
11
+ spec.summary = %q{Send events to the Amplitude API}
12
+ spec.description = %q{Provides an integration for sending events to Amplitude}
13
+ spec.homepage = "https://github.com/toothrot/amplitude-api"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rspec", "~> 2.99", '>= 2.99.0'
22
+ spec.add_development_dependency "rake", "~> 10.0", '>= 10.0'
23
+ spec.add_development_dependency "pry", "~> 0.9.12.6"
24
+ spec.add_dependency "typhoeus", "~> 0.7.1"
25
+ end
@@ -0,0 +1,33 @@
1
+ require 'json'
2
+ require 'bundler/setup'
3
+ Bundler.require(:default)
4
+ require 'amplitude-api/event'
5
+
6
+ class AmplitudeAPI
7
+ URI_STRING = "https://api.amplitude.com/httpapi"
8
+
9
+ USER_WITH_NO_ACCOUNT = "user who doesn't have an account"
10
+
11
+ class << self
12
+ attr_accessor :api_key
13
+
14
+ def send_event(event_name, user, properties = {})
15
+ event = AmplitudeAPI::Event.new(user_id: user, event_type: event_name, event_properties: properties)
16
+ track(event)
17
+ end
18
+
19
+ def body(*events)
20
+ event_body = events.flatten.map do |event|
21
+ event.to_hash
22
+ end
23
+ post_body = {
24
+ api_key: self.api_key,
25
+ event: JSON.generate(event_body)
26
+ }
27
+ end
28
+
29
+ def track(*events)
30
+ Typhoeus.post(URI_STRING, body: body(events))
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,36 @@
1
+ class AmplitudeAPI
2
+ class Event
3
+ attr_accessor :user_id, :event_type, :event_properties
4
+
5
+ def initialize(user_id: , event_type: , event_properties: {})
6
+ self.user_id = user_id
7
+ self.event_type = event_type
8
+ self.event_properties = event_properties
9
+ end
10
+
11
+ def user_id=(value)
12
+ @user_id =
13
+ if value.respond_to?(:id)
14
+ value.id
15
+ else
16
+ value || AmplitudeAPI::USER_WITH_NO_ACCOUNT
17
+ end
18
+ end
19
+
20
+ def to_hash
21
+ {
22
+ event_type: self.event_type,
23
+ user_id: self.user_id,
24
+ event_properties: self.event_properties
25
+ }
26
+ end
27
+
28
+ def ==(other)
29
+ if other.respond_to?(:to_hash)
30
+ self.to_hash == other.to_hash
31
+ else
32
+ false
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ class AmplitudeAPI
2
+ VERSION = '0.0.1'
3
+ end
data/readme.md ADDED
@@ -0,0 +1,2 @@
1
+ # Amplitude API
2
+ [![Build Status](https://travis-ci.org/toothrot/amplitude-api.svg?branch=master)](https://travis-ci.org/toothrot/amplitude-api)
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe AmplitudeAPI::Event do
4
+ User = Struct.new(:id)
5
+
6
+ context "with a user object" do
7
+ describe "#body" do
8
+ it "populates with the user's id" do
9
+ event = AmplitudeAPI::Event.new(user_id: User.new(123), event_type: 'clicked on home')
10
+ expect(event.to_hash[:user_id]).to eq(123)
11
+ end
12
+ end
13
+ end
14
+
15
+ context "with a user id" do
16
+ describe "#body" do
17
+ it "populates with the user's id" do
18
+ event = AmplitudeAPI::Event.new(user_id: 123, event_type: 'clicked on home')
19
+ expect(event.to_hash[:user_id]).to eq(123)
20
+ end
21
+ end
22
+ end
23
+
24
+ context "without a user" do
25
+ describe "#body" do
26
+ it "populates with the unknown user" do
27
+ event = AmplitudeAPI::Event.new(user_id: nil, event_type: 'clicked on home')
28
+ expect(event.to_hash[:user_id]).to eq(AmplitudeAPI::USER_WITH_NO_ACCOUNT)
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#body' do
34
+ it "includes the event type" do
35
+ event = AmplitudeAPI::Event.new(user_id: 123, event_type: 'clicked on home')
36
+ expect(event.to_hash[:event_type]).to eq('clicked on home')
37
+ end
38
+
39
+ it "includes arbitrary properties" do
40
+ event = AmplitudeAPI::Event.new(user_id: 123, event_type: 'clicked on home', event_properties: {abc: :def})
41
+ expect(event.to_hash[:event_properties]).to eq(abc: :def)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+
3
+ describe AmplitudeAPI do
4
+ before do
5
+ @user = Struct.new(:id).new(123)
6
+ end
7
+
8
+ describe ".track" do
9
+ context "with a single event" do
10
+ it "sends the event to Amplitude" do
11
+ event = AmplitudeAPI::Event.new(user_id: 123, event_type: 'clicked on sign up')
12
+ body = {api_key: AmplitudeAPI.api_key, event: JSON.generate([event.to_hash])}
13
+
14
+ expect(Typhoeus).to receive(:post).with(AmplitudeAPI::URI_STRING, body: body)
15
+
16
+ AmplitudeAPI.track(event)
17
+ end
18
+ end
19
+
20
+ context "with multiple events" do
21
+ it "sends all events in a single request" do
22
+ event = AmplitudeAPI::Event.new(user_id: 123, event_type: 'clicked on sign up')
23
+ event2 = AmplitudeAPI::Event.new(user_id: 456, event_type: 'liked a widget')
24
+ body = {api_key: AmplitudeAPI.api_key, event: JSON.generate([event.to_hash, event2.to_hash])}
25
+
26
+ expect(Typhoeus).to receive(:post).with(AmplitudeAPI::URI_STRING, body: body)
27
+
28
+ AmplitudeAPI.track([event, event2])
29
+ end
30
+ end
31
+ end
32
+
33
+ describe ".send_event" do
34
+ it "sends an event to AmplitudeAPI" do
35
+ event = AmplitudeAPI::Event.new(user_id: @user, event_type: "test_event", event_properties: {test_property: 1})
36
+ expect(AmplitudeAPI).to receive(:track).with(event)
37
+
38
+ AmplitudeAPI.send_event("test_event", @user, test_property: 1)
39
+ end
40
+
41
+ context "the user is nil" do
42
+ it "sends an event with the no account user" do
43
+ event = AmplitudeAPI::Event.new(user_id: nil, event_type: "test_event", event_properties: {test_property: 1})
44
+ expect(AmplitudeAPI).to receive(:track).with(event)
45
+
46
+ AmplitudeAPI.send_event("test_event", nil, test_property: 1)
47
+ end
48
+ end
49
+
50
+ context "the user is a user_id" do
51
+ it "sends an event to AmplitudeAPI" do
52
+ event = AmplitudeAPI::Event.new(user_id: 123, event_type: "test_event", event_properties: {test_property: 1})
53
+ expect(AmplitudeAPI).to receive(:track).with(event)
54
+
55
+ AmplitudeAPI.send_event("test_event", @user.id, test_property: 1)
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "#body" do
61
+ it "should add an api key" do
62
+ event = AmplitudeAPI::Event.new(user_id: @user, event_type: "test_event", event_properties: {test_property: 1})
63
+ body = AmplitudeAPI.body(event)
64
+ expect(body[:api_key]).to eq('stub api key')
65
+ end
66
+
67
+ it "should create an event" do
68
+ event = AmplitudeAPI::Event.new(user_id: 23, event_type: "test_event", event_properties: {foo: "bar"})
69
+ body = AmplitudeAPI.body(event)
70
+
71
+ expected = JSON.generate([{event_type: "test_event", user_id: 23, event_properties: {foo: "bar"}}])
72
+ expect(body[:event]).to eq(expected)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.join('..', '..', 'lib', 'amplitude-api'), __FILE__)
2
+ require 'pry'
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+
20
+ config.before(:suite) do
21
+ AmplitudeAPI.api_key = 'stub api key'
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: amplitude-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Rakoczy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.99'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.99.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.99'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.99.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '10.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '10.0'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '10.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '10.0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: pry
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 0.9.12.6
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: 0.9.12.6
67
+ - !ruby/object:Gem::Dependency
68
+ name: typhoeus
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: 0.7.1
74
+ type: :runtime
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: 0.7.1
81
+ description: Provides an integration for sending events to Amplitude
82
+ email:
83
+ - arakoczy@gmail.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - ".gitignore"
89
+ - ".rspec"
90
+ - ".travis.yml"
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE
94
+ - Rakefile
95
+ - amplitude-api.gemspec
96
+ - lib/amplitude-api.rb
97
+ - lib/amplitude-api/event.rb
98
+ - lib/amplitude-api/version.rb
99
+ - readme.md
100
+ - spec/lib/amplitude-api/event_spec.rb
101
+ - spec/lib/amplitude_api_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/toothrot/amplitude-api
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.5
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Send events to the Amplitude API
127
+ test_files:
128
+ - spec/lib/amplitude-api/event_spec.rb
129
+ - spec/lib/amplitude_api_spec.rb
130
+ - spec/spec_helper.rb