simple_segment 0.1.1 → 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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.hound.yml +2 -0
- data/.rubocop.yml +10 -0
- data/README.md +28 -13
- data/Rakefile +3 -3
- data/bin/console +3 -3
- data/lib/simple_segment/batch.rb +1 -1
- data/lib/simple_segment/client.rb +1 -2
- data/lib/simple_segment/configuration.rb +8 -2
- data/lib/simple_segment/logging.rb +16 -0
- data/lib/simple_segment/operations/alias.rb +4 -3
- data/lib/simple_segment/operations/group.rb +4 -3
- data/lib/simple_segment/operations/identify.rb +2 -2
- data/lib/simple_segment/operations/operation.rb +2 -3
- data/lib/simple_segment/operations/page.rb +2 -2
- data/lib/simple_segment/operations/track.rb +4 -3
- data/lib/simple_segment/request.rb +23 -11
- data/lib/simple_segment/utils.rb +2 -2
- data/lib/simple_segment/version.rb +1 -1
- data/simple_segment.gemspec +16 -15
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fcf149ce05352ac16b248268daee8b2ec22256b9
|
4
|
+
data.tar.gz: 3ca99ff617ebe0e2d88afb594ba6036d8ef7f813
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 757e1a05e10fa4698041b2dd0ac2ca704e3cb65838350378116cd88b1831ada96c23864d51d502149b44b71873f80b1caba5b4b116220f95fce71251a116b308
|
7
|
+
data.tar.gz: 7a657b9e44f601081cfc372b52e5721508fcc15afb1b063f944122caa5e9f7732b35b5074cdb88697e9f710e9cf63ab75097dfce4730124498ae4813e81f1817
|
data/.gitignore
CHANGED
data/.hound.yml
ADDED
data/.rubocop.yml
ADDED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ SimpleSegment allows for manual control of when and how the events are sent to S
|
|
8
8
|
|
9
9
|
## Status
|
10
10
|
|
11
|
-
|
11
|
+
The gem supports all existing functionality of analytics-ruby:
|
12
12
|
|
13
13
|
- `analytics.track(...)`
|
14
14
|
- `analytics.identify(...)`
|
@@ -16,12 +16,10 @@ SimpleSegment allows for manual control of when and how the events are sent to S
|
|
16
16
|
- `analytics.page(...)`
|
17
17
|
- `analytics.alias(...)`
|
18
18
|
- `analytics.flush` (no op for backwards compatibility with the official gem)
|
19
|
-
- Ability to manually batch events with `analytics.batch`
|
20
19
|
|
21
|
-
|
22
|
-
|
23
|
-
The plan is to be an drop in replacement for the official gem, so all the APIs will stay the same whenever possible.
|
20
|
+
In addition it offers the ability to manually batch events with [analytics.batch](#batching).
|
24
21
|
|
22
|
+
The plan is to be an drop in replacement for the official gem, so if you find inconsistencies with `analytics-ruby` feel free to file an issue.
|
25
23
|
|
26
24
|
## Installation
|
27
25
|
|
@@ -55,16 +53,12 @@ analytics = SimpleSegment::Client.new({
|
|
55
53
|
Use it as you would use `analytics-ruby`:
|
56
54
|
|
57
55
|
```ruby
|
58
|
-
analytics.track(
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
}
|
63
|
-
)
|
56
|
+
analytics.track({
|
57
|
+
user_id: user.id,
|
58
|
+
event: 'Created Account'
|
59
|
+
})
|
64
60
|
```
|
65
61
|
|
66
|
-
If you find inconsistencies with `analytics-ruby` feel free to file an issue.
|
67
|
-
|
68
62
|
### Batching
|
69
63
|
|
70
64
|
You can manually batch events with `analytics.batch`:
|
@@ -80,6 +74,27 @@ analytics.batch do |batch|
|
|
80
74
|
end
|
81
75
|
```
|
82
76
|
|
77
|
+
### Stub API calls
|
78
|
+
|
79
|
+
You can stub your API calls avoiding unecessary requests in development and automated test environments (backwards compatible with the official gem):
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
analytics = SimpleSegment::Client.new(
|
83
|
+
write_key: 'YOUR_WRITE_KEY',
|
84
|
+
stub: true
|
85
|
+
)
|
86
|
+
```
|
87
|
+
|
88
|
+
### Configurable Logger
|
89
|
+
When used in stubbed mode all calls are logged to STDOUT, this can be changed by providing a custom logger object:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
analytics = SimpleSegment::Client.new(
|
93
|
+
write_key: 'YOUR_WRITE_KEY',
|
94
|
+
logger: Rails.logger
|
95
|
+
)
|
96
|
+
```
|
97
|
+
|
83
98
|
## Development
|
84
99
|
|
85
100
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'simple_segment'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
8
8
|
|
9
|
-
require
|
9
|
+
require 'pry'
|
10
10
|
Pry.start
|
data/lib/simple_segment/batch.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
|
+
require 'simple_segment/logging'
|
2
|
+
|
1
3
|
module SimpleSegment
|
2
4
|
class Configuration
|
3
5
|
include SimpleSegment::Utils
|
6
|
+
include SimpleSegment::Logging
|
4
7
|
|
5
|
-
attr_reader :write_key, :on_error
|
8
|
+
attr_reader :write_key, :on_error, :stub, :logger
|
6
9
|
|
7
10
|
def initialize(settings = {})
|
8
11
|
symbolized_settings = symbolize_keys(settings)
|
9
12
|
@write_key = symbolized_settings[:write_key]
|
10
13
|
@on_error = symbolized_settings[:on_error] || proc {}
|
11
|
-
|
14
|
+
@stub = symbolized_settings[:stub]
|
15
|
+
@logger = default_logger(symbolized_settings[:logger])
|
16
|
+
raise ArgumentError, 'Missing required option :write_key' \
|
17
|
+
unless @write_key
|
12
18
|
end
|
13
19
|
end
|
14
20
|
end
|
@@ -6,11 +6,12 @@ module SimpleSegment
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def build_payload
|
9
|
-
raise ArgumentError, 'previous_id must be present'
|
9
|
+
raise ArgumentError, 'previous_id must be present' \
|
10
|
+
unless options[:previous_id]
|
10
11
|
|
11
|
-
base_payload.merge(
|
12
|
+
base_payload.merge(
|
12
13
|
previousId: options[:previous_id]
|
13
|
-
|
14
|
+
)
|
14
15
|
end
|
15
16
|
end
|
16
17
|
end
|
@@ -6,12 +6,13 @@ module SimpleSegment
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def build_payload
|
9
|
-
raise ArgumentError, 'group_id must be present'
|
9
|
+
raise ArgumentError, 'group_id must be present' \
|
10
|
+
unless options[:group_id]
|
10
11
|
|
11
|
-
base_payload.merge(
|
12
|
+
base_payload.merge(
|
12
13
|
traits: options[:traits],
|
13
14
|
groupId: options[:group_id]
|
14
|
-
|
15
|
+
)
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
@@ -36,9 +36,8 @@ module SimpleSegment
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def check_identity!
|
39
|
-
|
40
|
-
|
41
|
-
end
|
39
|
+
raise ArgumentError, 'user_id or anonymous_id must be present' \
|
40
|
+
unless options[:user_id] || options[:anonymous_id]
|
42
41
|
end
|
43
42
|
|
44
43
|
def timestamp(timestamp)
|
@@ -6,12 +6,13 @@ module SimpleSegment
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def build_payload
|
9
|
-
raise ArgumentError, 'event name must be present'
|
9
|
+
raise ArgumentError, 'event name must be present' \
|
10
|
+
unless options[:event]
|
10
11
|
|
11
|
-
base_payload.merge(
|
12
|
+
base_payload.merge(
|
12
13
|
event: options[:event],
|
13
14
|
properties: options[:properties]
|
14
|
-
|
15
|
+
)
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
@@ -6,27 +6,39 @@ module SimpleSegment
|
|
6
6
|
'accept' => 'application/json'
|
7
7
|
}.freeze
|
8
8
|
|
9
|
-
attr_reader :write_key, :error_handler
|
9
|
+
attr_reader :write_key, :error_handler, :stub, :logger
|
10
10
|
|
11
11
|
def initialize(client)
|
12
12
|
@write_key = client.config.write_key
|
13
13
|
@error_handler = client.config.on_error
|
14
|
+
@stub = client.config.stub
|
15
|
+
@logger = client.config.logger
|
14
16
|
end
|
15
17
|
|
16
18
|
def post(path, payload, headers: DEFAULT_HEADERS)
|
17
|
-
response
|
19
|
+
response = nil
|
20
|
+
status_code = nil
|
21
|
+
response_body = nil
|
22
|
+
|
18
23
|
uri = URI(BASE_URL)
|
19
24
|
payload = JSON.generate(payload)
|
25
|
+
if stub
|
26
|
+
logger.debug "stubbed request to \
|
27
|
+
#{path}: write key = #{write_key}, \
|
28
|
+
payload = #{payload}"
|
20
29
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
{ status: 200, error: nil }
|
31
|
+
else
|
32
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
33
|
+
request = Net::HTTP::Post.new(path, headers)
|
34
|
+
request.basic_auth write_key, nil
|
35
|
+
http.request(request, payload).tap do |res|
|
36
|
+
status_code = res.code
|
37
|
+
response_body = res.body
|
38
|
+
response = res
|
39
|
+
response.value
|
40
|
+
end
|
41
|
+
end
|
30
42
|
end
|
31
43
|
rescue StandardError => e
|
32
44
|
error_handler.call(status_code, response_body, e, response)
|
data/lib/simple_segment/utils.rb
CHANGED
data/simple_segment.gemspec
CHANGED
@@ -4,25 +4,26 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'simple_segment/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'simple_segment'
|
8
8
|
spec.version = SimpleSegment::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Mikhail Topolskiy']
|
10
|
+
spec.email = ['mikhail.topolskiy@gmail.com']
|
11
11
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
12
|
+
spec.summary = 'A simple synchronous API client for segment.io.'
|
13
|
+
spec.description = 'SimpleSegment allows for manual control of when and how the events are sent to Segment.'
|
14
|
+
spec.homepage = 'https://github.com/whatthewhat/simple_segment'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
|
17
17
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
-
spec.bindir =
|
18
|
+
spec.bindir = 'exe'
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ['lib']
|
21
21
|
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
27
|
-
spec.add_development_dependency
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.11'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
25
|
+
spec.add_development_dependency 'webmock', '~> 1.24'
|
26
|
+
spec.add_development_dependency 'timecop', '~> 0.8.0'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.47.0'
|
28
|
+
spec.add_development_dependency 'pry'
|
28
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_segment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Topolskiy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-03-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.8.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.47.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.47.0
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: pry
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -103,7 +117,9 @@ extensions: []
|
|
103
117
|
extra_rdoc_files: []
|
104
118
|
files:
|
105
119
|
- ".gitignore"
|
120
|
+
- ".hound.yml"
|
106
121
|
- ".rspec"
|
122
|
+
- ".rubocop.yml"
|
107
123
|
- ".travis.yml"
|
108
124
|
- Gemfile
|
109
125
|
- LICENSE.txt
|
@@ -115,6 +131,7 @@ files:
|
|
115
131
|
- lib/simple_segment/batch.rb
|
116
132
|
- lib/simple_segment/client.rb
|
117
133
|
- lib/simple_segment/configuration.rb
|
134
|
+
- lib/simple_segment/logging.rb
|
118
135
|
- lib/simple_segment/operations.rb
|
119
136
|
- lib/simple_segment/operations/alias.rb
|
120
137
|
- lib/simple_segment/operations/group.rb
|
@@ -146,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
163
|
version: '0'
|
147
164
|
requirements: []
|
148
165
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.6.
|
166
|
+
rubygems_version: 2.6.8
|
150
167
|
signing_key:
|
151
168
|
specification_version: 4
|
152
169
|
summary: A simple synchronous API client for segment.io.
|