simple_segment 0.1.0.pre

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: 894ef7cd8308bd97baa1ca0e58112a2977f9bef6
4
+ data.tar.gz: d1c3fd40058bf4dacf28e28db2489f791d85ae3d
5
+ SHA512:
6
+ metadata.gz: 40be6532c6e71cbc81adeae4bfd943b059b027310e4f7458cf9b8b4723a03f5317cb5c2ab0b74f187fadfe344536b066935ea0c8dded88e5210b1eb68e124aba
7
+ data.tar.gz: 3d13dd7218a30366db53054cffae79ff5895e93ff9e8c34ad5570abf1d0b51337b9d259beac4619d2744032f2f30b6a1cc841fbb77facd92a0e7672d30085a62
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.8
4
+ - 2.2.4
5
+ - 2.3.0
6
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in simple_segment.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Mikhail Topolskiy
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # SimpleSegment
2
+
3
+ **Warning** The project is in development and is not ready for production use yet!
4
+
5
+ [![Build Status](https://travis-ci.org/whatthewhat/simple_segment.svg?branch=master)](https://travis-ci.org/whatthewhat/simple_segment)
6
+
7
+ A simple synchronous Ruby API client for [segment.io](segment.io).
8
+
9
+ SimpleSegment allows for manual control of when and how the events are sent to Segment. This can be useful if you want to leverage an existing queueing system like Sidekiq or Resque for sending events or need to send events synchronously. If this is not the case you will be better off using the [official segment gem](https://github.com/segmentio/analytics-ruby) that handles queuing for you.
10
+
11
+ ## Status
12
+
13
+ ### Implemented
14
+
15
+ - `analytics.track(...)`
16
+ - `analytics.identify(...)`
17
+ - `analytics.group(...)`
18
+ - `analytics.page(...)`
19
+ - `analytics.alias(...)`
20
+ - `analytics.flush` (no op for backwards compatibility with the official gem)
21
+
22
+ ### Planned
23
+
24
+ - Ability to manually batch events, https://segment.com/docs/libraries/http/#import
25
+ - Configurable network error handling
26
+ - Configurable logging
27
+
28
+ The plan is to be an drop in replacement for the official gem, so all the APIs will stay the same whenever possible.
29
+
30
+ ## Installation
31
+
32
+ Add this line to your application's Gemfile:
33
+
34
+ ```ruby
35
+ gem 'simple_segment'
36
+ ```
37
+
38
+ And then execute:
39
+
40
+ $ bundle
41
+
42
+ Or install it yourself as:
43
+
44
+ $ gem install simple_segment
45
+
46
+ ## Usage
47
+
48
+ Create a client instance:
49
+
50
+ ```ruby
51
+ analytics = SimpleSegment::Client.new({
52
+ write_key: 'YOUR_WRITE_KEY'
53
+ })
54
+ ```
55
+
56
+ Use it as you would use `analytics-ruby`:
57
+
58
+ ```ruby
59
+ analytics.track(
60
+ {
61
+ user_id: user.id,
62
+ event: 'Created Account'
63
+ }
64
+ )
65
+ ```
66
+
67
+ If you find inconsistencies with `analytics-ruby` feel free to file an issue.
68
+
69
+ ## Development
70
+
71
+ 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.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/whatthewhat/simple_segment.
78
+
79
+
80
+ ## License
81
+
82
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "simple_segment"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,78 @@
1
+ require 'simple_segment/utils'
2
+ require 'simple_segment/configuration'
3
+ require 'simple_segment/operations'
4
+
5
+ module SimpleSegment
6
+ class Client
7
+ include SimpleSegment::Utils
8
+
9
+ attr_reader :config
10
+
11
+ def initialize(options = {})
12
+ @config = Configuration.new(options)
13
+ end
14
+
15
+ # @param [Hash] options
16
+ # @option :user_id
17
+ # @option :anonymous_id
18
+ # @option :traits [Hash]
19
+ # @option :context [Hash]
20
+ # @option :integrations [Hash]
21
+ # @option :timestamp [#iso8601] (Time.now)
22
+ def identify(options)
23
+ Operations::Identify.new(symbolize_keys(options), config).call
24
+ end
25
+
26
+ # @param [Hash] options
27
+ # @option :event [String] required
28
+ # @option :user_id
29
+ # @option :anonymous_id
30
+ # @option :properties [Hash]
31
+ # @option :context [Hash]
32
+ # @option :integrations [Hash]
33
+ # @option :timestamp [#iso8601] (Time.now)
34
+ def track(options)
35
+ Operations::Track.new(symbolize_keys(options), config).call
36
+ end
37
+
38
+ # @param [Hash] options
39
+ # @option :user_id
40
+ # @option :anonymous_id
41
+ # @option :name [String]
42
+ # @option :properties [Hash]
43
+ # @option :context [Hash]
44
+ # @option :integrations [Hash]
45
+ # @option :timestamp [#iso8601] (Time.now)
46
+ def page(options)
47
+ Operations::Page.new(symbolize_keys(options), config).call
48
+ end
49
+
50
+ # @param [Hash] options
51
+ # @option :user_id
52
+ # @option :anonymous_id
53
+ # @option :group_id required
54
+ # @option :traits [Hash]
55
+ # @option :context [Hash]
56
+ # @option :integrations [Hash]
57
+ # @option :timestamp [#iso8601] (Time.now)
58
+ def group(options)
59
+ Operations::Group.new(symbolize_keys(options), config).call
60
+ end
61
+
62
+ # @param [Hash] options
63
+ # @option :user_id
64
+ # @option :anonymous_id
65
+ # @option :previous_id required
66
+ # @option :traits [Hash]
67
+ # @option :context [Hash]
68
+ # @option :integrations [Hash]
69
+ # @option :timestamp [#iso8601] (Time.now)
70
+ def alias(options)
71
+ Operations::Alias.new(symbolize_keys(options), config).call
72
+ end
73
+
74
+ # A no op, added for backwards compatibility with `analytics-ruby`
75
+ def flush
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,13 @@
1
+ module SimpleSegment
2
+ class Configuration
3
+ include SimpleSegment::Utils
4
+
5
+ attr_reader :write_key
6
+
7
+ def initialize(settings = {})
8
+ symbolized_settings = symbolize_keys(settings)
9
+ @write_key = symbolized_settings[:write_key]
10
+ raise ArgumentError, 'Missing required option :write_key' unless @write_key
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ module SimpleSegment
2
+ module Operations
3
+ class Alias < Operation
4
+ def call
5
+ Request.new('/v1/alias', config).post(build_payload)
6
+ end
7
+
8
+ private
9
+
10
+ def build_payload
11
+ raise ArgumentError, 'previous_id must be present' unless options[:previous_id]
12
+
13
+ base_payload.merge({
14
+ previousId: options[:previous_id]
15
+ })
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module SimpleSegment
2
+ module Operations
3
+ class Group < Operation
4
+ def call
5
+ Request.new('/v1/group', config).post(build_payload)
6
+ end
7
+
8
+ private
9
+
10
+ def build_payload
11
+ raise ArgumentError, 'group_id must be present' unless options[:group_id]
12
+
13
+ base_payload.merge({
14
+ traits: options[:traits],
15
+ groupId: options[:group_id]
16
+ })
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ module SimpleSegment
2
+ module Operations
3
+ class Identify < Operation
4
+ def call
5
+ Request.new('/v1/identify', config).post(build_payload)
6
+ end
7
+
8
+ private
9
+
10
+ def build_payload
11
+ base_payload.merge({
12
+ traits: options[:traits]
13
+ })
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,45 @@
1
+ module SimpleSegment
2
+ module Operations
3
+ class Operation
4
+ DEFAULT_CONTEXT = {
5
+ library: {
6
+ name: 'simple_segment',
7
+ version: SimpleSegment::VERSION
8
+ }
9
+ }.freeze
10
+
11
+ attr_reader :options, :config
12
+
13
+ def initialize(options = {}, config)
14
+ @options = options
15
+ @config = config
16
+ end
17
+
18
+ def call
19
+ raise 'Must be implemented in a subclass'
20
+ end
21
+
22
+ private
23
+
24
+ def base_payload
25
+ check_identity!
26
+ current_time = Time.now
27
+
28
+ {
29
+ userId: options[:user_id],
30
+ anonymousId: options[:anonymous_id],
31
+ context: DEFAULT_CONTEXT.merge(options[:context].to_h),
32
+ integrations: options[:integrations],
33
+ timestamp: options.fetch(:timestamp, current_time).iso8601,
34
+ sentAt: current_time.iso8601
35
+ }
36
+ end
37
+
38
+ def check_identity!
39
+ unless options[:user_id] || options[:anonymous_id]
40
+ raise ArgumentError, 'user_id or anonymous_id must be present'
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,18 @@
1
+ module SimpleSegment
2
+ module Operations
3
+ class Page < Operation
4
+ def call
5
+ Request.new('/v1/page', config).post(build_payload)
6
+ end
7
+
8
+ private
9
+
10
+ def build_payload
11
+ base_payload.merge({
12
+ name: options[:name],
13
+ properties: options[:properties]
14
+ })
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,20 @@
1
+ module SimpleSegment
2
+ module Operations
3
+ class Track < Operation
4
+ def call
5
+ Request.new('/v1/track', config).post(build_payload)
6
+ end
7
+
8
+ private
9
+
10
+ def build_payload(current_time = Time.now)
11
+ raise ArgumentError, 'event name must be present' unless options[:event]
12
+
13
+ base_payload.merge({
14
+ event: options[:event],
15
+ properties: options[:properties]
16
+ })
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ require 'simple_segment/request'
2
+ require 'simple_segment/operations/operation'
3
+ require 'simple_segment/operations/identify'
4
+ require 'simple_segment/operations/track'
5
+ require 'simple_segment/operations/page'
6
+ require 'simple_segment/operations/group'
7
+ require 'simple_segment/operations/alias'
8
+
9
+ module SimpleSegment
10
+ module Operations
11
+ end
12
+ end
@@ -0,0 +1,27 @@
1
+ module SimpleSegment
2
+ class Request
3
+ BASE_URL = 'https://api.segment.io'.freeze
4
+ DEFAULT_HEADERS = {
5
+ 'Content-Type' => 'application/json',
6
+ 'accept' => 'application/json'
7
+ }.freeze
8
+
9
+ attr_reader :path, :write_key
10
+
11
+ def initialize(path, config)
12
+ @path = path
13
+ @write_key = config.write_key
14
+ end
15
+
16
+ def post(payload, headers: DEFAULT_HEADERS)
17
+ uri = URI(BASE_URL)
18
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
19
+ payload = JSON.generate(payload)
20
+ request = Net::HTTP::Post.new(path, headers)
21
+ request.basic_auth write_key, nil
22
+
23
+ http.request(request, payload)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ module SimpleSegment
2
+ module Utils
3
+ def symbolize_keys(hash)
4
+ hash.each_with_object({}) { |(key, value), result|
5
+ result[key.to_sym] = value
6
+ }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleSegment
2
+ VERSION = '0.1.0.pre'
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'simple_segment/version'
4
+ require 'simple_segment/client'
5
+
6
+ module SimpleSegment
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_segment/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_segment"
8
+ spec.version = SimpleSegment::VERSION
9
+ spec.authors = ["Mikhail Topolskiy"]
10
+ spec.email = ["mikhail.topolskiy@gmail.com"]
11
+
12
+ spec.summary = %q{A simple synchronous API client for segment.io.}
13
+ spec.description = %q{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
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
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
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_segment
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Mikhail Topolskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.24'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.24'
69
+ - !ruby/object:Gem::Dependency
70
+ name: timecop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.8.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.8.0
83
+ description: SimpleSegment allows for manual control of when and how the events are
84
+ sent to Segment.
85
+ email:
86
+ - mikhail.topolskiy@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/simple_segment.rb
101
+ - lib/simple_segment/client.rb
102
+ - lib/simple_segment/configuration.rb
103
+ - lib/simple_segment/operations.rb
104
+ - lib/simple_segment/operations/alias.rb
105
+ - lib/simple_segment/operations/group.rb
106
+ - lib/simple_segment/operations/identify.rb
107
+ - lib/simple_segment/operations/operation.rb
108
+ - lib/simple_segment/operations/page.rb
109
+ - lib/simple_segment/operations/track.rb
110
+ - lib/simple_segment/request.rb
111
+ - lib/simple_segment/utils.rb
112
+ - lib/simple_segment/version.rb
113
+ - simple_segment.gemspec
114
+ homepage: https://github.com/whatthewhat/simple_segment
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.3.1
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.6.2
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A simple synchronous API client for segment.io.
138
+ test_files: []