heap 0.3.0 → 1.0.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 +4 -4
- data/.travis.yml +16 -0
- data/Gemfile +11 -8
- data/Gemfile187 +23 -0
- data/LICENSE.txt +2 -2
- data/README.md +120 -0
- data/Rakefile +8 -22
- data/VERSION +1 -1
- data/heap.gemspec +42 -43
- data/lib/heap.rb +10 -23
- data/lib/heap/client.rb +192 -0
- data/lib/heap/errors.rb +18 -0
- data/lib/heap/validations.rb +57 -0
- data/test/client_add_user_properties_test.rb +124 -0
- data/test/client_test.rb +78 -0
- data/test/client_track_test.rb +151 -0
- data/test/heap_test.rb +23 -0
- data/test/helper.rb +4 -17
- metadata +51 -52
- data/CHANGELOG.mdown +0 -18
- data/Gemfile.lock +0 -91
- data/README.mdown +0 -99
- data/lib/generators/heap/install_generator.rb +0 -12
- data/lib/generators/templates/heap.rb +0 -2
- data/lib/heap/events.rb +0 -22
- data/lib/heap/railtie.rb +0 -9
- data/lib/heap/view_helpers.rb +0 -28
- data/test/test_heap.rb +0 -35
- data/test/test_view_helper.rb +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b7f4270c4f06512b348491c08a91e7e44c293337
|
4
|
+
data.tar.gz: d7f95a6e3e4dbb399ee899dca956690eec30aef6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d873b49995396816be5c824b3dc47848a69ee614fd3228b9c3e62208e24a65e884303bd31c72e648597207289ef8b5e614e98bb0f9ca6f7591421c18ef3e0584
|
7
|
+
data.tar.gz: 8b27c441c8c46d0e2c15260006818dab9eed08f83f79567fd4564232e2fb73c33ff23aa918f575d11080f6fa71d7bcc194349b6b1938d49598ff27cdaa555911
|
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
matrix:
|
4
|
+
include:
|
5
|
+
- rvm: 1.8.7
|
6
|
+
gemfile: Gemfile187
|
7
|
+
- rvm: 1.9.3
|
8
|
+
gemfile: Gemfile
|
9
|
+
- rvm: 2.0.0
|
10
|
+
gemfile: Gemfile
|
11
|
+
- rvm: 2.1.8
|
12
|
+
gemfile: Gemfile
|
13
|
+
- rvm: 2.2.4
|
14
|
+
gemfile: Gemfile
|
15
|
+
- rvm: 2.3.0
|
16
|
+
gemfile: Gemfile
|
data/Gemfile
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
3
|
+
# Add dependencies required to use your gem here.
|
4
|
+
gem 'faraday', '>= 0.8.11'
|
5
|
+
gem 'faraday_middleware', '>= 0.8.0'
|
4
6
|
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
5
9
|
group :development do
|
6
|
-
gem
|
7
|
-
gem
|
8
|
-
gem
|
9
|
-
gem
|
10
|
-
gem
|
11
|
-
gem "webmock", "~>1.20"
|
10
|
+
gem 'minitest', '>= 0'
|
11
|
+
gem 'yard', '>= 0.8.7.6'
|
12
|
+
gem 'rdoc', '>= 3.12'
|
13
|
+
gem 'bundler', '>= 1.0'
|
14
|
+
gem 'jeweler', '>= 2.0.1'
|
12
15
|
end
|
data/Gemfile187
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Add dependencies required to use your gem here.
|
4
|
+
gem 'faraday', '~> 0.8.11'
|
5
|
+
gem 'faraday_middleware', '~> 0.8.0'
|
6
|
+
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
9
|
+
group :development do
|
10
|
+
gem 'minitest', '>= 0'
|
11
|
+
gem 'yard', '>= 0.8.7.6'
|
12
|
+
gem 'rdoc', '>= 3.12'
|
13
|
+
gem 'bundler', '>= 1.0'
|
14
|
+
|
15
|
+
# NOTE: The latest versions of these dependencies do not support Ruby 1.8.7.
|
16
|
+
gem 'jeweler', '~> 1.8.8'
|
17
|
+
|
18
|
+
# NOTE: These are dependencies of the gems that we require whose latest
|
19
|
+
# versions do not support Ruby 1.8.7.
|
20
|
+
gem 'addressable', '~> 2.3.8'
|
21
|
+
gem 'git', '~> 1.2.9'
|
22
|
+
gem 'highline', '~> 1.6.21'
|
23
|
+
end
|
data/LICENSE.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c)
|
1
|
+
Copyright (c) 2016 Heap Inc.
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining
|
4
4
|
a copy of this software and associated documentation files (the
|
@@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
17
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
18
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
19
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# Heap Server-Side API Client for Ruby
|
2
|
+
|
3
|
+
[](https://travis-ci.org/heap/heap-ruby)
|
4
|
+
[](http://rubydoc.info/github/heap/heap-ruby/master/frames)
|
5
|
+
[](https://badge.fury.io/rb/heap)
|
6
|
+
|
7
|
+
This is a Ruby client for the [Heap](https://heapanalytics.com/)
|
8
|
+
[server-side API](https://heapanalytics.com/docs/server-side).
|
9
|
+
|
10
|
+
|
11
|
+
## Prerequisites
|
12
|
+
|
13
|
+
This gem is tested on Ruby 1.8.7 and above.
|
14
|
+
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
If you're using [bundler](http://bundler.io/), add the following line to your
|
19
|
+
[Gemfile](http://bundler.io/v1.11/gemfile.html).
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'heap', '~> 1.0'
|
23
|
+
```
|
24
|
+
|
25
|
+
Otherwise, install the [heap](https://rubygems.org/gems/heap) gem and activate
|
26
|
+
it in your code manually.
|
27
|
+
|
28
|
+
```bash
|
29
|
+
gem install heap
|
30
|
+
```
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'heap'
|
34
|
+
```
|
35
|
+
|
36
|
+
|
37
|
+
## Setup
|
38
|
+
|
39
|
+
Place the following code in a file that executes when your application
|
40
|
+
initializes.
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
Heap.app_id = 'YOUR_APP_ID'
|
44
|
+
```
|
45
|
+
|
46
|
+
In a Ruby on Rails application, place the following snippet in an initializer,
|
47
|
+
such as `config/initializers/heap.rb`.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
if Rails.env.production?
|
51
|
+
Heap.app_id = 'YOUR_APP_ID'
|
52
|
+
else
|
53
|
+
Heap.app_id = 'YOUR_DEV_APP_ID'
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
In some testing environments, connecting to outside servers is undesirable. Set
|
58
|
+
the `stubbed` accessor to `true` to have all API calls succeed without
|
59
|
+
generating any network traffic.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class StubbedHeapTest < MiniTest::Test
|
63
|
+
def setup
|
64
|
+
Heap.stubbed = true
|
65
|
+
end
|
66
|
+
|
67
|
+
def teardown
|
68
|
+
Heap.stubbed = false
|
69
|
+
end
|
70
|
+
|
71
|
+
...
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
|
76
|
+
## Usage
|
77
|
+
|
78
|
+
[Track](https://heapanalytics.com/docs/server-side#track) a server-side event.
|
79
|
+
The properties are optional.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
Heap.track 'user-handle', 'event-name', property: 'value'
|
83
|
+
```
|
84
|
+
|
85
|
+
[Add properties](https://heapanalytics.com/docs/server-side#identify) to a
|
86
|
+
user.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
Heap.add_user_properties 'user-handle', plan: 'premium1'
|
90
|
+
```
|
91
|
+
|
92
|
+
If the global API client instance stored in `Heap` is not a good fit for your
|
93
|
+
application's architecture, create your own client instances.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
heap_client = Heap.new app_id: 'YOUR_APP_ID'
|
97
|
+
heap_client.track 'user-handle', 'event-name', property: 'value'
|
98
|
+
```
|
99
|
+
|
100
|
+
|
101
|
+
## Legacy Gem Releases
|
102
|
+
|
103
|
+
Gem versions below 1.0 come from
|
104
|
+
[this repository](https://github.com/HectorMalot/heap), which is an entirely
|
105
|
+
different codebase. We are very grateful to
|
106
|
+
[@HectorMalot](https://github.com/HectorMalot) for donating the `heap` gem name
|
107
|
+
to us.
|
108
|
+
|
109
|
+
If you are using a pre-1.0 gem, please consider upgrading to an officially
|
110
|
+
supported release. In the meantime, you can pin the old version in your
|
111
|
+
`Gemfile`.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
gem 'heap', '~> 0.3'
|
115
|
+
```
|
116
|
+
|
117
|
+
|
118
|
+
## Copyright
|
119
|
+
|
120
|
+
Copyright (c) 2016 Heap Inc., released under the MIT license.
|
data/Rakefile
CHANGED
@@ -15,13 +15,12 @@ require 'jeweler'
|
|
15
15
|
Jeweler::Tasks.new do |gem|
|
16
16
|
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
17
17
|
gem.name = "heap"
|
18
|
-
gem.homepage = "http://github.com/
|
18
|
+
gem.homepage = "http://github.com/heap/heap-ruby"
|
19
19
|
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{
|
21
|
-
gem.description = %Q{
|
22
|
-
gem.email = "
|
23
|
-
gem.
|
24
|
-
gem.authors = ["Dennis de Reus"]
|
20
|
+
gem.summary = %Q{Heap server-side API client}
|
21
|
+
gem.description = %Q{Implements Heap's server-side API}
|
22
|
+
gem.email = "victor@heapanalytics.com"
|
23
|
+
gem.authors = ["Victor Costan"]
|
25
24
|
# dependencies defined in Gemfile
|
26
25
|
end
|
27
26
|
Jeweler::RubygemsDotOrgTasks.new
|
@@ -29,24 +28,11 @@ Jeweler::RubygemsDotOrgTasks.new
|
|
29
28
|
require 'rake/testtask'
|
30
29
|
Rake::TestTask.new(:test) do |test|
|
31
30
|
test.libs << 'lib' << 'test'
|
32
|
-
test.pattern = 'test
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
33
32
|
test.verbose = true
|
34
33
|
end
|
35
34
|
|
36
|
-
desc "Code coverage detail"
|
37
|
-
task :simplecov do
|
38
|
-
ENV['COVERAGE'] = "true"
|
39
|
-
Rake::Task['test'].execute
|
40
|
-
end
|
41
|
-
|
42
35
|
task :default => :test
|
43
36
|
|
44
|
-
require '
|
45
|
-
Rake::
|
46
|
-
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
-
|
48
|
-
rdoc.rdoc_dir = 'rdoc'
|
49
|
-
rdoc.title = "heap #{version}"
|
50
|
-
rdoc.rdoc_files.include('README*')
|
51
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
-
end
|
37
|
+
require 'yard'
|
38
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/heap.gemspec
CHANGED
@@ -2,76 +2,75 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: heap 0.
|
5
|
+
# stub: heap 1.0.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "heap"
|
9
|
-
s.version = "0.
|
9
|
+
s.version = "1.0.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
|
-
s.authors = ["
|
14
|
-
s.date = "
|
15
|
-
s.description = "
|
16
|
-
s.email = "
|
13
|
+
s.authors = ["Victor Costan"]
|
14
|
+
s.date = "2016-02-29"
|
15
|
+
s.description = "Implements Heap's server-side API"
|
16
|
+
s.email = "victor@heapanalytics.com"
|
17
17
|
s.extra_rdoc_files = [
|
18
|
-
"CHANGELOG.mdown",
|
19
18
|
"LICENSE.txt",
|
20
|
-
"README.
|
19
|
+
"README.md"
|
21
20
|
]
|
22
21
|
s.files = [
|
23
22
|
".document",
|
24
|
-
"
|
23
|
+
".travis.yml",
|
25
24
|
"Gemfile",
|
26
|
-
"
|
25
|
+
"Gemfile187",
|
27
26
|
"LICENSE.txt",
|
28
|
-
"README.
|
27
|
+
"README.md",
|
29
28
|
"Rakefile",
|
30
29
|
"VERSION",
|
31
30
|
"heap.gemspec",
|
32
|
-
"lib/generators/heap/install_generator.rb",
|
33
|
-
"lib/generators/templates/heap.rb",
|
34
31
|
"lib/heap.rb",
|
35
|
-
"lib/heap/
|
36
|
-
"lib/heap/
|
37
|
-
"lib/heap/
|
38
|
-
"test/
|
39
|
-
"test/
|
40
|
-
"test/
|
32
|
+
"lib/heap/client.rb",
|
33
|
+
"lib/heap/errors.rb",
|
34
|
+
"lib/heap/validations.rb",
|
35
|
+
"test/client_add_user_properties_test.rb",
|
36
|
+
"test/client_test.rb",
|
37
|
+
"test/client_track_test.rb",
|
38
|
+
"test/heap_test.rb",
|
39
|
+
"test/helper.rb"
|
41
40
|
]
|
42
|
-
s.homepage = "
|
41
|
+
s.homepage = "http://github.com/heap/heap-ruby"
|
43
42
|
s.licenses = ["MIT"]
|
44
|
-
s.rubygems_version = "2.
|
45
|
-
s.summary = "
|
43
|
+
s.rubygems_version = "2.5.1"
|
44
|
+
s.summary = "Heap server-side API client"
|
46
45
|
|
47
46
|
if s.respond_to? :specification_version then
|
48
47
|
s.specification_version = 4
|
49
48
|
|
50
49
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
-
s.add_runtime_dependency(%q<
|
52
|
-
s.
|
53
|
-
s.add_development_dependency(%q<
|
54
|
-
s.add_development_dependency(%q<
|
55
|
-
s.add_development_dependency(%q<
|
56
|
-
s.add_development_dependency(%q<
|
57
|
-
s.add_development_dependency(%q<
|
50
|
+
s.add_runtime_dependency(%q<faraday>, [">= 0.8.11"])
|
51
|
+
s.add_runtime_dependency(%q<faraday_middleware>, [">= 0.8.0"])
|
52
|
+
s.add_development_dependency(%q<minitest>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<yard>, [">= 0.8.7.6"])
|
54
|
+
s.add_development_dependency(%q<rdoc>, [">= 3.12"])
|
55
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0"])
|
56
|
+
s.add_development_dependency(%q<jeweler>, [">= 2.0.1"])
|
58
57
|
else
|
59
|
-
s.add_dependency(%q<
|
60
|
-
s.add_dependency(%q<
|
61
|
-
s.add_dependency(%q<
|
62
|
-
s.add_dependency(%q<
|
63
|
-
s.add_dependency(%q<
|
64
|
-
s.add_dependency(%q<
|
65
|
-
s.add_dependency(%q<
|
58
|
+
s.add_dependency(%q<faraday>, [">= 0.8.11"])
|
59
|
+
s.add_dependency(%q<faraday_middleware>, [">= 0.8.0"])
|
60
|
+
s.add_dependency(%q<minitest>, [">= 0"])
|
61
|
+
s.add_dependency(%q<yard>, [">= 0.8.7.6"])
|
62
|
+
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
63
|
+
s.add_dependency(%q<bundler>, [">= 1.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, [">= 2.0.1"])
|
66
65
|
end
|
67
66
|
else
|
68
|
-
s.add_dependency(%q<
|
69
|
-
s.add_dependency(%q<
|
70
|
-
s.add_dependency(%q<
|
71
|
-
s.add_dependency(%q<
|
72
|
-
s.add_dependency(%q<
|
73
|
-
s.add_dependency(%q<
|
74
|
-
s.add_dependency(%q<
|
67
|
+
s.add_dependency(%q<faraday>, [">= 0.8.11"])
|
68
|
+
s.add_dependency(%q<faraday_middleware>, [">= 0.8.0"])
|
69
|
+
s.add_dependency(%q<minitest>, [">= 0"])
|
70
|
+
s.add_dependency(%q<yard>, [">= 0.8.7.6"])
|
71
|
+
s.add_dependency(%q<rdoc>, [">= 3.12"])
|
72
|
+
s.add_dependency(%q<bundler>, [">= 1.0"])
|
73
|
+
s.add_dependency(%q<jeweler>, [">= 2.0.1"])
|
75
74
|
end
|
76
75
|
end
|
77
76
|
|
data/lib/heap.rb
CHANGED
@@ -1,25 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
include HTTParty
|
7
|
-
base_uri 'https://heapanalytics.com/api'
|
8
|
-
headers "Content-Type" => "application/json"
|
9
|
-
|
10
|
-
def self.app_id=(obj)
|
11
|
-
@@app_id = obj
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.app_id
|
15
|
-
@@app_id
|
16
|
-
end
|
1
|
+
# Namespace for all the classes in this gem.
|
2
|
+
module HeapAPI
|
3
|
+
# @return {String} the version of this gem
|
4
|
+
VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).strip
|
5
|
+
end
|
17
6
|
|
18
|
-
|
19
|
-
|
20
|
-
|
7
|
+
require 'heap/client.rb'
|
8
|
+
require 'heap/errors.rb'
|
9
|
+
require 'heap/validations.rb'
|
21
10
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
11
|
+
# A global instance of {HeapAPI::Client}.
|
12
|
+
Heap = HeapAPI::Client.new
|
data/lib/heap/client.rb
ADDED
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
|
4
|
+
# This is the class of the Heap object.
|
5
|
+
class HeapAPI::Client
|
6
|
+
# @return [String] the Heap application ID from
|
7
|
+
# https://heapanalytics.com/app/install
|
8
|
+
attr_accessor :app_id
|
9
|
+
|
10
|
+
# @return [Boolean] if true, all the requests to the Heap API are stubbed to
|
11
|
+
# return successfully; this overrides the Faraday-related options
|
12
|
+
attr_reader :stubbed
|
13
|
+
|
14
|
+
# @return [Symbol] the Faraday adapter used by the Heap API server connection
|
15
|
+
# @see http://www.rubydoc.info/gems/faraday/
|
16
|
+
attr_reader :faraday_adapter
|
17
|
+
|
18
|
+
# @return [Array] arguments to the Faraday adapter used by the Heap API
|
19
|
+
# server connection
|
20
|
+
attr_reader :faraday_adapter_args
|
21
|
+
|
22
|
+
# @return [String] the User-Agent header value
|
23
|
+
attr_accessor :user_agent
|
24
|
+
|
25
|
+
# Creates a new client for the Heap server-side API.
|
26
|
+
#
|
27
|
+
# For simplicity, consider using the global client instance referenced by the
|
28
|
+
# Heap constant instead of creating and managing new instances.
|
29
|
+
#
|
30
|
+
# @param [Hash<Symbol, Object>] options initial values for attributes
|
31
|
+
# @option options [String] app_id the Heap application ID from
|
32
|
+
# @option options [Hash<Symbol, Object>] js_options default heap.js advanced
|
33
|
+
# options
|
34
|
+
# @option options [Boolean] stubbed if true, all the requests to the Heap API
|
35
|
+
# are stubbed to return successfully; this overrides the Faraday-related
|
36
|
+
# options
|
37
|
+
# @option options [Symbol] faraday_adapter the Faraday adapter used by the
|
38
|
+
# Heap API server connection
|
39
|
+
# @option options [Array] faraday_adapter_args arguments to the Faraday
|
40
|
+
# adapter used by the Heap API server connection
|
41
|
+
def initialize(options = {})
|
42
|
+
@app_id = nil
|
43
|
+
@live_connection = nil
|
44
|
+
@stubbed_connection = false
|
45
|
+
@stubbed = false
|
46
|
+
@faraday_adapter = Faraday.default_adapter
|
47
|
+
@faraday_adapter_args = []
|
48
|
+
@user_agent = "heap-ruby/#{HeapAPI::VERSION} " +
|
49
|
+
"faraday/#{Faraday::VERSION} ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM})"
|
50
|
+
|
51
|
+
options.each do |key, value|
|
52
|
+
self.send :"#{key}=", value
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def stubbed=(new_value)
|
57
|
+
@connection = nil
|
58
|
+
@stubbed = new_value
|
59
|
+
end
|
60
|
+
|
61
|
+
def faraday_adapter=(new_adapter)
|
62
|
+
raise RuntimeError, 'Faraday connection already initialized' if @connection
|
63
|
+
@faraday_adapter = new_adapter
|
64
|
+
end
|
65
|
+
|
66
|
+
def faraday_adapter_args=(new_args)
|
67
|
+
raise RuntimeError, 'Faraday connection already initialized' if @connection
|
68
|
+
unless new_args.instance_of? Array
|
69
|
+
raise ArgumentErrorm, "Arguments must be an array"
|
70
|
+
end
|
71
|
+
@faraday_adapter_args = new_args
|
72
|
+
end
|
73
|
+
|
74
|
+
# Assign custom properties to an existing user.
|
75
|
+
#
|
76
|
+
# @param [String] identity an e-mail, handle, or Heap-generated user ID
|
77
|
+
# @param [Hash<String, String|Number>] properties key-value properties
|
78
|
+
# associated with the event; each key must have fewer than 1024 characters;
|
79
|
+
# each value must be a Number or String with fewer than 1024 characters
|
80
|
+
# @return [HeapAPI::Client] self
|
81
|
+
# @see https://heapanalytics.com/docs/server-side#identify
|
82
|
+
def add_user_properties(identity, properties)
|
83
|
+
ensure_valid_app_id!
|
84
|
+
ensure_valid_properties! properties
|
85
|
+
|
86
|
+
body = {
|
87
|
+
:app_id => @app_id,
|
88
|
+
:identity => identity.to_s,
|
89
|
+
:properties => properties,
|
90
|
+
}
|
91
|
+
response = connection.post '/api/identify', body,
|
92
|
+
'User-Agent' => user_agent
|
93
|
+
raise HeapAPI::ApiError.new(response) unless response.success?
|
94
|
+
self
|
95
|
+
end
|
96
|
+
|
97
|
+
# Sends a custom event to the Heap API servers.
|
98
|
+
#
|
99
|
+
# @param [String] event the name of the server-side event; limited to 1024
|
100
|
+
# characters
|
101
|
+
# @param [String] identity an e-mail, handle, or Heap-generated user ID
|
102
|
+
# @param [Hash<String, String|Number>] properties key-value properties
|
103
|
+
# associated with the event; each key must have fewer than 1024 characters;
|
104
|
+
# each value must be a Number or String with fewer than 1024 characters
|
105
|
+
# @return [HeapAPI::Client] self
|
106
|
+
# @see https://heapanalytics.com/docs/server-side#track
|
107
|
+
def track(event, identity, properties = nil)
|
108
|
+
ensure_valid_app_id!
|
109
|
+
|
110
|
+
event_name = event.to_s
|
111
|
+
ensure_valid_event_name! event_name
|
112
|
+
|
113
|
+
body = {
|
114
|
+
:app_id => @app_id,
|
115
|
+
:identity => identity.to_s,
|
116
|
+
:event => event,
|
117
|
+
}
|
118
|
+
unless properties.nil?
|
119
|
+
body[:properties] = properties
|
120
|
+
ensure_valid_properties! properties
|
121
|
+
end
|
122
|
+
|
123
|
+
response = connection.post '/api/track', body,
|
124
|
+
'User-Agent' => user_agent
|
125
|
+
raise HeapAPI::ApiError.new(response) unless response.success?
|
126
|
+
self
|
127
|
+
end
|
128
|
+
|
129
|
+
# The underlying Faraday connection used to make HTTP requests.
|
130
|
+
#
|
131
|
+
# @return [Faraday::Connection] a Faraday connection object
|
132
|
+
def connection
|
133
|
+
@connection ||= @stubbed ? stubbed_connection : live_connection
|
134
|
+
end
|
135
|
+
|
136
|
+
# The Faraday connection used to make HTTP requests to the Heap API server.
|
137
|
+
#
|
138
|
+
# This is used when {#stubbed?} is false.
|
139
|
+
#
|
140
|
+
# @return [Faraday::Connection] a Faraday connection object
|
141
|
+
def live_connection
|
142
|
+
@live_connection ||= live_connection!
|
143
|
+
end
|
144
|
+
private :live_connection
|
145
|
+
|
146
|
+
# The Faraday connection that stubs API calls to the Heap servers.
|
147
|
+
#
|
148
|
+
# This is used when {#stubbed?} is true.
|
149
|
+
#
|
150
|
+
# @return [Faraday::Connection] a Faraday connection object
|
151
|
+
def stubbed_connection
|
152
|
+
@stubbed_connection ||= stubbed_connection!
|
153
|
+
end
|
154
|
+
private :stubbed_connection
|
155
|
+
|
156
|
+
# Creates a new Faraday connection to the Heap API server.
|
157
|
+
#
|
158
|
+
# @return [Faraday::Connection] a Faraday connection object
|
159
|
+
def live_connection!
|
160
|
+
Faraday.new 'https://heapanalytics.com' do |c|
|
161
|
+
c.request :json
|
162
|
+
c.adapter @faraday_adapter, *@faraday_adapter_args
|
163
|
+
end
|
164
|
+
end
|
165
|
+
private :live_connection!
|
166
|
+
|
167
|
+
# Creates a new Faraday connection that stubs API calls to the Heap servers.
|
168
|
+
#
|
169
|
+
# @return [Faraday::Connection] a Faraday connection object
|
170
|
+
def stubbed_connection!
|
171
|
+
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
172
|
+
stub.post('/api/identify') { |env| [204, {}, ''] }
|
173
|
+
stub.post('/api/track') { |env| [204, {}, ''] }
|
174
|
+
end
|
175
|
+
|
176
|
+
Faraday.new 'https://heapanalytics.com' do |c|
|
177
|
+
c.request :json
|
178
|
+
c.adapter :test, stubs
|
179
|
+
end
|
180
|
+
end
|
181
|
+
private :stubbed_connection!
|
182
|
+
|
183
|
+
# Creates a new client instance.
|
184
|
+
#
|
185
|
+
# This is defined here so `Heap.new` can be used as a shorthand for
|
186
|
+
# `HeapAPI::Client.new`.
|
187
|
+
#
|
188
|
+
# @see {HeapAPI::Client#initialize}
|
189
|
+
def new(*args)
|
190
|
+
HeapAPI::Client.new(*args)
|
191
|
+
end
|
192
|
+
end
|