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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 00dfcaba81c53815a6cc8f837b9fe8e8a832abb7
4
- data.tar.gz: 7c2f9c23df74413679b4dce401d7f0f8d1cfb2a6
3
+ metadata.gz: fcf149ce05352ac16b248268daee8b2ec22256b9
4
+ data.tar.gz: 3ca99ff617ebe0e2d88afb594ba6036d8ef7f813
5
5
  SHA512:
6
- metadata.gz: 46211a605c9107ff3b960278c3d10227e0cf1388c25a4ad10551709cc7e88a3ab04bd559c4c9a04e840cde5e943c8d1423511bbc557cdd41469b3467b0892243
7
- data.tar.gz: 5015cffb0a7100bba692ae9023bc083c49dcd7993c4b41ee1cbdccdba7a515f32d9eb4b6ec88530535eeca71ba29f6eb29fdfef69886c5f1cd51cbf061b6c7db
6
+ metadata.gz: 757e1a05e10fa4698041b2dd0ac2ca704e3cb65838350378116cd88b1831ada96c23864d51d502149b44b71873f80b1caba5b4b116220f95fce71251a116b308
7
+ data.tar.gz: 7a657b9e44f601081cfc372b52e5721508fcc15afb1b063f944122caa5e9f7732b35b5074cdb88697e9f710e9cf63ab75097dfce4730124498ae4813e81f1817
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.gem
11
+ .rbenv-gemsets
data/.hound.yml ADDED
@@ -0,0 +1,2 @@
1
+ ruby:
2
+ config_file: .rubocop.yml
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ Metrics/BlockLength:
2
+ Exclude:
3
+ - '**/*_spec.rb'
4
+
5
+ Metrics/LineLength:
6
+ Exclude:
7
+ - 'simple_segment.gemspec'
8
+
9
+ Style/Documentation:
10
+ Enabled: false
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
- ### Implemented
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
- [List of planned features](https://github.com/whatthewhat/simple_segment/issues?q=is%3Aissue+is%3Aopen+label%3Afeature)
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
- user_id: user.id,
61
- event: 'Created Account'
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
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "simple_segment"
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 "pry"
9
+ require 'pry'
10
10
  Pry.start
@@ -42,7 +42,7 @@ module SimpleSegment
42
42
  end
43
43
 
44
44
  def commit
45
- if payload[:batch].length == 0
45
+ if payload[:batch].length.zero?
46
46
  raise ArgumentError, 'A batch must contain at least one action'
47
47
  end
48
48
 
@@ -88,7 +88,6 @@ module SimpleSegment
88
88
  end
89
89
 
90
90
  # A no op, added for backwards compatibility with `analytics-ruby`
91
- def flush
92
- end
91
+ def flush; end
93
92
  end
94
93
  end
@@ -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
- raise ArgumentError, 'Missing required option :write_key' unless @write_key
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
@@ -0,0 +1,16 @@
1
+ require 'logger'
2
+ module SimpleSegment
3
+ module Logging
4
+ def self.included(klass)
5
+ klass.extend(self)
6
+ end
7
+
8
+ def default_logger(logger_option)
9
+ if logger_option
10
+ logger_option
11
+ else
12
+ Logger.new STDOUT
13
+ end
14
+ end
15
+ end
16
+ 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' unless options[:previous_id]
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' unless options[:group_id]
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
@@ -6,9 +6,9 @@ module SimpleSegment
6
6
  end
7
7
 
8
8
  def build_payload
9
- base_payload.merge({
9
+ base_payload.merge(
10
10
  traits: options[:traits]
11
- })
11
+ )
12
12
  end
13
13
  end
14
14
  end
@@ -36,9 +36,8 @@ module SimpleSegment
36
36
  end
37
37
 
38
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
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,10 +6,10 @@ module SimpleSegment
6
6
  end
7
7
 
8
8
  def build_payload
9
- base_payload.merge({
9
+ base_payload.merge(
10
10
  name: options[:name],
11
11
  properties: options[:properties]
12
- })
12
+ )
13
13
  end
14
14
  end
15
15
  end
@@ -6,12 +6,13 @@ module SimpleSegment
6
6
  end
7
7
 
8
8
  def build_payload
9
- raise ArgumentError, 'event name must be present' unless options[:event]
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, status_code, response_body = nil, nil, nil
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
- Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
22
- request = Net::HTTP::Post.new(path, headers)
23
- request.basic_auth write_key, nil
24
- http.request(request, payload).tap { |res|
25
- status_code = res.code
26
- response_body = res.body
27
- response = res
28
- response.value
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)
@@ -5,9 +5,9 @@ module SimpleSegment
5
5
  end
6
6
 
7
7
  def symbolize_keys(hash)
8
- hash.each_with_object({}) { |(key, value), result|
8
+ hash.each_with_object({}) do |(key, value), result|
9
9
  result[key.to_sym] = value
10
- }
10
+ end
11
11
  end
12
12
  end
13
13
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleSegment
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'.freeze
3
3
  end
@@ -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 = "simple_segment"
7
+ spec.name = 'simple_segment'
8
8
  spec.version = SimpleSegment::VERSION
9
- spec.authors = ["Mikhail Topolskiy"]
10
- spec.email = ["mikhail.topolskiy@gmail.com"]
9
+ spec.authors = ['Mikhail Topolskiy']
10
+ spec.email = ['mikhail.topolskiy@gmail.com']
11
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"
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 = "exe"
18
+ spec.bindir = 'exe'
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
21
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
- spec.add_development_dependency "pry"
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.1.1
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: 2016-10-10 00:00:00.000000000 Z
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.2
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.