simple_segment 0.1.0.pre → 0.1.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -8
- data/bin/console +2 -6
- data/lib/simple_segment/batch.rb +50 -0
- data/lib/simple_segment/client.rb +7 -0
- data/lib/simple_segment/configuration.rb +2 -1
- data/lib/simple_segment/operations/alias.rb +0 -2
- data/lib/simple_segment/operations/group.rb +0 -2
- data/lib/simple_segment/operations/identify.rb +0 -2
- data/lib/simple_segment/operations/page.rb +0 -2
- data/lib/simple_segment/operations/track.rb +1 -3
- data/lib/simple_segment/request.rb +13 -4
- data/lib/simple_segment/version.rb +1 -1
- data/simple_segment.gemspec +1 -0
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5979b1b25f12af923da26348d23edba401da59d
|
4
|
+
data.tar.gz: 5e5fb48ad7401d4f940074d82f6588f4efea2b84
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 528412610b190fa09d3db9ddfb7401b69752e475d699229315d260538b774dc2a0449f52870fc0a5ed1dc75d232684c44076c406e2ca50e16c1207cbe49c6501
|
7
|
+
data.tar.gz: 0b7c89ab4123685489cf5dbd79bf3650ddb58c70b63c36fa754a762047172f8dd39def607f3e68a0417b7276d0015adf0c7b2214bd1b93cf4ecb5ea996db746b
|
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# SimpleSegment
|
2
2
|
|
3
|
-
**Warning** The project is in development and is not ready for production use yet!
|
4
|
-
|
5
3
|
[![Build Status](https://travis-ci.org/whatthewhat/simple_segment.svg?branch=master)](https://travis-ci.org/whatthewhat/simple_segment)
|
6
4
|
|
7
5
|
A simple synchronous Ruby API client for [segment.io](segment.io).
|
@@ -18,15 +16,13 @@ SimpleSegment allows for manual control of when and how the events are sent to S
|
|
18
16
|
- `analytics.page(...)`
|
19
17
|
- `analytics.alias(...)`
|
20
18
|
- `analytics.flush` (no op for backwards compatibility with the official gem)
|
19
|
+
- Ability to manually batch events with `analytics.batch`
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
- Ability to manually batch events, https://segment.com/docs/libraries/http/#import
|
25
|
-
- Configurable network error handling
|
26
|
-
- Configurable logging
|
21
|
+
[List of planned features](https://github.com/whatthewhat/simple_segment/issues?q=is%3Aissue+is%3Aopen+label%3Afeature)
|
27
22
|
|
28
23
|
The plan is to be an drop in replacement for the official gem, so all the APIs will stay the same whenever possible.
|
29
24
|
|
25
|
+
|
30
26
|
## Installation
|
31
27
|
|
32
28
|
Add this line to your application's Gemfile:
|
@@ -49,7 +45,10 @@ Create a client instance:
|
|
49
45
|
|
50
46
|
```ruby
|
51
47
|
analytics = SimpleSegment::Client.new({
|
52
|
-
write_key: 'YOUR_WRITE_KEY'
|
48
|
+
write_key: 'YOUR_WRITE_KEY', # required
|
49
|
+
on_error: proc { |error_code, error_body, response, exception|
|
50
|
+
# defaults to an empty proc
|
51
|
+
}
|
53
52
|
})
|
54
53
|
```
|
55
54
|
|
@@ -66,6 +65,21 @@ analytics.track(
|
|
66
65
|
|
67
66
|
If you find inconsistencies with `analytics-ruby` feel free to file an issue.
|
68
67
|
|
68
|
+
### Batching
|
69
|
+
|
70
|
+
You can manually batch events with `analytics.batch`:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
analytics.batch do |batch|
|
74
|
+
batch.context = {...} # shared context for all events
|
75
|
+
batch.integrations = {...} # shared integrations hash for all events
|
76
|
+
batch.identify(...)
|
77
|
+
batch.track(...)
|
78
|
+
batch.track(...)
|
79
|
+
...
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
69
83
|
## Development
|
70
84
|
|
71
85
|
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/bin/console
CHANGED
@@ -6,9 +6,5 @@ require "simple_segment"
|
|
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
|
-
|
10
|
-
|
11
|
-
# Pry.start
|
12
|
-
|
13
|
-
require "irb"
|
14
|
-
IRB.start
|
9
|
+
require "pry"
|
10
|
+
Pry.start
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module SimpleSegment
|
2
|
+
class Batch
|
3
|
+
include SimpleSegment::Utils
|
4
|
+
|
5
|
+
attr_reader :config, :payload
|
6
|
+
attr_accessor :context, :integrations
|
7
|
+
|
8
|
+
def initialize(config)
|
9
|
+
@config = config
|
10
|
+
@payload = { batch: [] }
|
11
|
+
@context = {}
|
12
|
+
@integrations = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def identify(options)
|
16
|
+
add(Operations::Identify, options, __method__)
|
17
|
+
end
|
18
|
+
|
19
|
+
def track(options)
|
20
|
+
add(Operations::Track, options, __method__)
|
21
|
+
end
|
22
|
+
|
23
|
+
def page(options)
|
24
|
+
add(Operations::Page, options, __method__)
|
25
|
+
end
|
26
|
+
|
27
|
+
def group(options)
|
28
|
+
add(Operations::Group, options, __method__)
|
29
|
+
end
|
30
|
+
|
31
|
+
def commit
|
32
|
+
if payload[:batch].length == 0
|
33
|
+
raise ArgumentError, 'A batch must contain at least one action'
|
34
|
+
end
|
35
|
+
payload[:context] = context
|
36
|
+
payload[:integrations] = integrations
|
37
|
+
|
38
|
+
Request.new('/v1/import', config).post(payload)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def add(operation_class, options, action)
|
44
|
+
operation = operation_class.new(symbolize_keys(options), config)
|
45
|
+
operation_payload = operation.build_payload
|
46
|
+
operation_payload[:action] = action
|
47
|
+
payload[:batch] << operation_payload
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'simple_segment/utils'
|
2
2
|
require 'simple_segment/configuration'
|
3
3
|
require 'simple_segment/operations'
|
4
|
+
require 'simple_segment/batch'
|
4
5
|
|
5
6
|
module SimpleSegment
|
6
7
|
class Client
|
@@ -71,6 +72,12 @@ module SimpleSegment
|
|
71
72
|
Operations::Alias.new(symbolize_keys(options), config).call
|
72
73
|
end
|
73
74
|
|
75
|
+
def batch
|
76
|
+
batch = Batch.new(config)
|
77
|
+
yield(batch)
|
78
|
+
batch.commit
|
79
|
+
end
|
80
|
+
|
74
81
|
# A no op, added for backwards compatibility with `analytics-ruby`
|
75
82
|
def flush
|
76
83
|
end
|
@@ -2,11 +2,12 @@ module SimpleSegment
|
|
2
2
|
class Configuration
|
3
3
|
include SimpleSegment::Utils
|
4
4
|
|
5
|
-
attr_reader :write_key
|
5
|
+
attr_reader :write_key, :on_error
|
6
6
|
|
7
7
|
def initialize(settings = {})
|
8
8
|
symbolized_settings = symbolize_keys(settings)
|
9
9
|
@write_key = symbolized_settings[:write_key]
|
10
|
+
@on_error = symbolized_settings[:on_error] || proc {}
|
10
11
|
raise ArgumentError, 'Missing required option :write_key' unless @write_key
|
11
12
|
end
|
12
13
|
end
|
@@ -5,9 +5,7 @@ module SimpleSegment
|
|
5
5
|
Request.new('/v1/track', config).post(build_payload)
|
6
6
|
end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
def build_payload(current_time = Time.now)
|
8
|
+
def build_payload
|
11
9
|
raise ArgumentError, 'event name must be present' unless options[:event]
|
12
10
|
|
13
11
|
base_payload.merge({
|
@@ -6,22 +6,31 @@ module SimpleSegment
|
|
6
6
|
'accept' => 'application/json'
|
7
7
|
}.freeze
|
8
8
|
|
9
|
-
attr_reader :path, :write_key
|
9
|
+
attr_reader :path, :write_key, :error_handler
|
10
10
|
|
11
11
|
def initialize(path, config)
|
12
12
|
@path = path
|
13
13
|
@write_key = config.write_key
|
14
|
+
@error_handler = config.on_error
|
14
15
|
end
|
15
16
|
|
16
17
|
def post(payload, headers: DEFAULT_HEADERS)
|
18
|
+
response, status_code, response_body = nil, nil, nil
|
17
19
|
uri = URI(BASE_URL)
|
20
|
+
payload = JSON.generate(payload)
|
21
|
+
|
18
22
|
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
|
19
|
-
payload = JSON.generate(payload)
|
20
23
|
request = Net::HTTP::Post.new(path, headers)
|
21
24
|
request.basic_auth write_key, nil
|
22
|
-
|
23
|
-
|
25
|
+
http.request(request, payload).tap { |res|
|
26
|
+
status_code = res.code
|
27
|
+
response_body = res.body
|
28
|
+
response = res
|
29
|
+
response.value
|
30
|
+
}
|
24
31
|
end
|
32
|
+
rescue StandardError => e
|
33
|
+
error_handler.call(status_code, response_body, e, response)
|
25
34
|
end
|
26
35
|
end
|
27
36
|
end
|
data/simple_segment.gemspec
CHANGED
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.1.0.
|
4
|
+
version: 0.1.0.pre2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikhail Topolskiy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-12 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: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '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'
|
83
97
|
description: SimpleSegment allows for manual control of when and how the events are
|
84
98
|
sent to Segment.
|
85
99
|
email:
|
@@ -98,6 +112,7 @@ files:
|
|
98
112
|
- bin/console
|
99
113
|
- bin/setup
|
100
114
|
- lib/simple_segment.rb
|
115
|
+
- lib/simple_segment/batch.rb
|
101
116
|
- lib/simple_segment/client.rb
|
102
117
|
- lib/simple_segment/configuration.rb
|
103
118
|
- lib/simple_segment/operations.rb
|