From e159286a835d2099cb6d3636d52aac5eef7b5888 Mon Sep 17 00:00:00 2001 From: Guilherme Taschetto Date: Thu, 19 Jan 2023 10:57:11 -0300 Subject: [PATCH 1/2] Rename a generic `Segment` namespace to a more specific one --- README.md | 8 ++--- analytics-ruby.gemspec | 2 +- bin/analytics | 2 +- lib/segment/analytics.rb | 8 ++--- lib/segment/analytics/backoff_policy.rb | 4 +-- lib/segment/analytics/client.rb | 6 ++-- lib/segment/analytics/defaults.rb | 2 +- lib/segment/analytics/field_parser.rb | 6 ++-- lib/segment/analytics/logging.rb | 4 +-- lib/segment/analytics/message_batch.rb | 6 ++-- lib/segment/analytics/response.rb | 2 +- lib/segment/analytics/test_queue.rb | 2 +- lib/segment/analytics/transport.rb | 10 +++--- lib/segment/analytics/utils.rb | 2 +- lib/segment/analytics/version.rb | 2 +- lib/segment/analytics/worker.rb | 8 ++--- spec/isolated/json_example.rb | 2 +- spec/segment/analytics/backoff_policy_spec.rb | 2 +- spec/segment/analytics/client_spec.rb | 2 +- spec/segment/analytics/message_batch_spec.rb | 2 +- spec/segment/analytics/response_spec.rb | 2 +- spec/segment/analytics/test_queue_spec.rb | 2 +- spec/segment/analytics/transport_spec.rb | 4 +-- spec/segment/analytics/worker_spec.rb | 34 +++++++++---------- spec/segment/analytics_spec.rb | 14 ++++---- spec/spec_helper.rb | 2 +- 26 files changed, 70 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index e6cdb6bd..b173b34d 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ gem install 'analytics-ruby' Create an instance of the Analytics object: ```ruby -analytics = Segment::Analytics.new(write_key: 'YOUR_WRITE_KEY') +analytics = SegmentIO::Analytics.new(write_key: 'YOUR_WRITE_KEY') ``` Identify the user for the people section, see more [here](https://segment.com/docs/libraries/ruby/#identify). @@ -92,14 +92,14 @@ Documentation is available at [segment.com/docs/sources/server/ruby](https://seg ### Test Queue -You can use the `test: true` option to Segment::Analytics.new to cause all requests to be saved to a test queue until manually reset. All events will process as specified by the configuration, and they will also be stored in a separate queue for inspection during testing. +You can use the `test: true` option to SegmentIO::Analytics.new to cause all requests to be saved to a test queue until manually reset. All events will process as specified by the configuration, and they will also be stored in a separate queue for inspection during testing. A test queue can be used as follows: ```ruby -client = Segment::Analytics.new(test: true) +client = SegmentIO::Analytics.new(test: true) -client.test_queue # => # +client.test_queue # => # client.track(user_id: 'foo', event: 'bar') diff --git a/analytics-ruby.gemspec b/analytics-ruby.gemspec index 60d9155d..2d804009 100644 --- a/analytics-ruby.gemspec +++ b/analytics-ruby.gemspec @@ -2,7 +2,7 @@ require File.expand_path('../lib/segment/analytics/version', __FILE__) Gem::Specification.new do |spec| spec.name = 'analytics-ruby' - spec.version = Segment::Analytics::VERSION + spec.version = SegmentIO::Analytics::VERSION spec.files = Dir.glob("{lib,bin}/**/*") spec.require_paths = ['lib'] spec.bindir = 'bin' diff --git a/bin/analytics b/bin/analytics index 86f35114..2d53e89e 100755 --- a/bin/analytics +++ b/bin/analytics @@ -42,7 +42,7 @@ command :send do |c| c.option '--previousId=', String, 'the previous id' c.action do |args, options| - Analytics = Segment::Analytics.new({ + Analytics = SegmentIO::Analytics.new({ write_key: options.writeKey, on_error: Proc.new { |status, msg| print msg } }) diff --git a/lib/segment/analytics.rb b/lib/segment/analytics.rb index ca83353c..4a2a70c7 100644 --- a/lib/segment/analytics.rb +++ b/lib/segment/analytics.rb @@ -9,18 +9,18 @@ require 'segment/analytics/logging' require 'segment/analytics/test_queue' -module Segment +module SegmentIO class Analytics - # Initializes a new instance of {Segment::Analytics::Client}, to which all + # Initializes a new instance of {SegmentIO::Analytics::Client}, to which all # method calls are proxied. # # @param options includes options that are passed down to - # {Segment::Analytics::Client#initialize} + # {SegmentIO::Analytics::Client#initialize} # @option options [Boolean] :stub (false) If true, requests don't hit the # server and are stubbed to be successful. def initialize(options = {}) Transport.stub = options[:stub] if options.has_key?(:stub) - @client = Segment::Analytics::Client.new options + @client = SegmentIO::Analytics::Client.new options end def method_missing(message, *args, &block) diff --git a/lib/segment/analytics/backoff_policy.rb b/lib/segment/analytics/backoff_policy.rb index 394a2c15..e17f253d 100644 --- a/lib/segment/analytics/backoff_policy.rb +++ b/lib/segment/analytics/backoff_policy.rb @@ -1,9 +1,9 @@ require 'segment/analytics/defaults' -module Segment +module SegmentIO class Analytics class BackoffPolicy - include Segment::Analytics::Defaults::BackoffPolicy + include SegmentIO::Analytics::Defaults::BackoffPolicy # @param [Hash] opts # @option opts [Numeric] :min_timeout_ms The minimum backoff timeout diff --git a/lib/segment/analytics/client.rb b/lib/segment/analytics/client.rb index 9e626d9b..40e32ce5 100644 --- a/lib/segment/analytics/client.rb +++ b/lib/segment/analytics/client.rb @@ -6,11 +6,11 @@ require 'segment/analytics/utils' require 'segment/analytics/worker' -module Segment +module SegmentIO class Analytics class Client - include Segment::Analytics::Utils - include Segment::Analytics::Logging + include SegmentIO::Analytics::Utils + include SegmentIO::Analytics::Logging # @param [Hash] opts # @option opts [String] :write_key Your project's write_key diff --git a/lib/segment/analytics/defaults.rb b/lib/segment/analytics/defaults.rb index 85623466..c44ab6a3 100644 --- a/lib/segment/analytics/defaults.rb +++ b/lib/segment/analytics/defaults.rb @@ -1,4 +1,4 @@ -module Segment +module SegmentIO class Analytics module Defaults module Request diff --git a/lib/segment/analytics/field_parser.rb b/lib/segment/analytics/field_parser.rb index 602f4bbb..1f83a759 100644 --- a/lib/segment/analytics/field_parser.rb +++ b/lib/segment/analytics/field_parser.rb @@ -1,11 +1,11 @@ -module Segment +module SegmentIO class Analytics # Handles parsing fields according to the Segment Spec # # @see https://segment.com/docs/spec/ class FieldParser class << self - include Segment::Analytics::Utils + include SegmentIO::Analytics::Utils # In addition to the common fields, track accepts: # @@ -171,7 +171,7 @@ def check_timestamp!(timestamp) end def add_context!(context) - context[:library] = { :name => 'analytics-ruby', :version => Segment::Analytics::VERSION.to_s } + context[:library] = { :name => 'analytics-ruby', :version => SegmentIO::Analytics::VERSION.to_s } end # private: Ensures that a string is non-empty diff --git a/lib/segment/analytics/logging.rb b/lib/segment/analytics/logging.rb index f7aaa636..65304e9a 100644 --- a/lib/segment/analytics/logging.rb +++ b/lib/segment/analytics/logging.rb @@ -1,6 +1,6 @@ require 'logger' -module Segment +module SegmentIO class Analytics # Wraps an existing logger and adds a prefix to all messages class PrefixedLogger @@ -35,7 +35,7 @@ def logger Rails.logger else logger = Logger.new STDOUT - logger.progname = 'Segment::Analytics' + logger.progname = 'SegmentIO::Analytics' logger end @logger = PrefixedLogger.new(base_logger, '[analytics-ruby]') diff --git a/lib/segment/analytics/message_batch.rb b/lib/segment/analytics/message_batch.rb index 1522463f..4406f472 100644 --- a/lib/segment/analytics/message_batch.rb +++ b/lib/segment/analytics/message_batch.rb @@ -1,15 +1,15 @@ require 'forwardable' require 'segment/analytics/logging' -module Segment +module SegmentIO class Analytics # A batch of `Message`s to be sent to the API class MessageBatch class JSONGenerationError < StandardError; end extend Forwardable - include Segment::Analytics::Logging - include Segment::Analytics::Defaults::MessageBatch + include SegmentIO::Analytics::Logging + include SegmentIO::Analytics::Defaults::MessageBatch def initialize(max_message_count) @messages = [] diff --git a/lib/segment/analytics/response.rb b/lib/segment/analytics/response.rb index 7306ac0a..43c56ac0 100644 --- a/lib/segment/analytics/response.rb +++ b/lib/segment/analytics/response.rb @@ -1,4 +1,4 @@ -module Segment +module SegmentIO class Analytics class Response attr_reader :status, :error diff --git a/lib/segment/analytics/test_queue.rb b/lib/segment/analytics/test_queue.rb index af31558e..ed9f29b8 100644 --- a/lib/segment/analytics/test_queue.rb +++ b/lib/segment/analytics/test_queue.rb @@ -1,4 +1,4 @@ -module Segment +module SegmentIO class Analytics class TestQueue attr_reader :messages diff --git a/lib/segment/analytics/transport.rb b/lib/segment/analytics/transport.rb index 59697bea..9dc937fc 100644 --- a/lib/segment/analytics/transport.rb +++ b/lib/segment/analytics/transport.rb @@ -7,12 +7,12 @@ require 'net/https' require 'json' -module Segment +module SegmentIO class Analytics class Transport - include Segment::Analytics::Defaults::Request - include Segment::Analytics::Utils - include Segment::Analytics::Logging + include SegmentIO::Analytics::Defaults::Request + include SegmentIO::Analytics::Utils + include SegmentIO::Analytics::Logging def initialize(options = {}) options[:host] ||= HOST @@ -22,7 +22,7 @@ def initialize(options = {}) @path = options[:path] || PATH @retries = options[:retries] || RETRIES @backoff_policy = - options[:backoff_policy] || Segment::Analytics::BackoffPolicy.new + options[:backoff_policy] || SegmentIO::Analytics::BackoffPolicy.new http = Net::HTTP.new(options[:host], options[:port]) http.use_ssl = options[:ssl] diff --git a/lib/segment/analytics/utils.rb b/lib/segment/analytics/utils.rb index 2c2d86a8..dd28b9df 100644 --- a/lib/segment/analytics/utils.rb +++ b/lib/segment/analytics/utils.rb @@ -1,6 +1,6 @@ require 'securerandom' -module Segment +module SegmentIO class Analytics module Utils extend self diff --git a/lib/segment/analytics/version.rb b/lib/segment/analytics/version.rb index c292749d..5a3c1d31 100644 --- a/lib/segment/analytics/version.rb +++ b/lib/segment/analytics/version.rb @@ -1,4 +1,4 @@ -module Segment +module SegmentIO class Analytics VERSION = '2.4.2' end diff --git a/lib/segment/analytics/worker.rb b/lib/segment/analytics/worker.rb index 7e73fae7..7f55ae89 100644 --- a/lib/segment/analytics/worker.rb +++ b/lib/segment/analytics/worker.rb @@ -3,12 +3,12 @@ require 'segment/analytics/transport' require 'segment/analytics/utils' -module Segment +module SegmentIO class Analytics class Worker - include Segment::Analytics::Utils - include Segment::Analytics::Defaults - include Segment::Analytics::Logging + include SegmentIO::Analytics::Utils + include SegmentIO::Analytics::Defaults + include SegmentIO::Analytics::Logging # public: Creates a new worker # diff --git a/spec/isolated/json_example.rb b/spec/isolated/json_example.rb index c693b555..6118307d 100644 --- a/spec/isolated/json_example.rb +++ b/spec/isolated/json_example.rb @@ -1,6 +1,6 @@ RSpec.shared_examples 'message_batch_json' do it 'MessageBatch generates proper JSON' do - batch = Segment::Analytics::MessageBatch.new(100) + batch = SegmentIO::Analytics::MessageBatch.new(100) batch << { 'a' => 'b' } batch << { 'c' => 'd' } diff --git a/spec/segment/analytics/backoff_policy_spec.rb b/spec/segment/analytics/backoff_policy_spec.rb index 75dc9fce..8f482bd7 100644 --- a/spec/segment/analytics/backoff_policy_spec.rb +++ b/spec/segment/analytics/backoff_policy_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe BackoffPolicy do describe '#initialize' do diff --git a/spec/segment/analytics/client_spec.rb b/spec/segment/analytics/client_spec.rb index 0358bb2d..fd570d50 100644 --- a/spec/segment/analytics/client_spec.rb +++ b/spec/segment/analytics/client_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe Client do let(:client) do diff --git a/spec/segment/analytics/message_batch_spec.rb b/spec/segment/analytics/message_batch_spec.rb index 4b7b66fc..082511df 100644 --- a/spec/segment/analytics/message_batch_spec.rb +++ b/spec/segment/analytics/message_batch_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe MessageBatch do subject { described_class.new(100) } diff --git a/spec/segment/analytics/response_spec.rb b/spec/segment/analytics/response_spec.rb index 7e099718..72042447 100644 --- a/spec/segment/analytics/response_spec.rb +++ b/spec/segment/analytics/response_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe Response do describe '#status' do diff --git a/spec/segment/analytics/test_queue_spec.rb b/spec/segment/analytics/test_queue_spec.rb index c97431d8..8efbf731 100644 --- a/spec/segment/analytics/test_queue_spec.rb +++ b/spec/segment/analytics/test_queue_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe TestQueue do let(:test_queue) { described_class.new } diff --git a/spec/segment/analytics/transport_spec.rb b/spec/segment/analytics/transport_spec.rb index 56b551fe..c43f4cf1 100644 --- a/spec/segment/analytics/transport_spec.rb +++ b/spec/segment/analytics/transport_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe Transport do before do @@ -48,7 +48,7 @@ class Analytics it 'sets a default backoff policy' do backoff_policy = subject.instance_variable_get(:@backoff_policy) - expect(backoff_policy).to be_a(Segment::Analytics::BackoffPolicy) + expect(backoff_policy).to be_a(SegmentIO::Analytics::BackoffPolicy) end it 'initializes a new Net::HTTP with default host and port' do diff --git a/spec/segment/analytics/worker_spec.rb b/spec/segment/analytics/worker_spec.rb index 0ca4d497..688c8727 100644 --- a/spec/segment/analytics/worker_spec.rb +++ b/spec/segment/analytics/worker_spec.rb @@ -1,16 +1,16 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe Worker do before do - Segment::Analytics::Transport.stub = true + SegmentIO::Analytics::Transport.stub = true end describe '#init' do it 'accepts string keys' do queue = Queue.new - worker = Segment::Analytics::Worker.new(queue, + worker = SegmentIO::Analytics::Worker.new(queue, 'secret', 'batch_size' => 100) batch = worker.instance_variable_get(:@batch) @@ -20,36 +20,36 @@ class Analytics describe '#run' do before :all do - Segment::Analytics::Defaults::Request::BACKOFF = 0.1 + SegmentIO::Analytics::Defaults::Request::BACKOFF = 0.1 end after :all do - Segment::Analytics::Defaults::Request::BACKOFF = 30.0 + SegmentIO::Analytics::Defaults::Request::BACKOFF = 30.0 end it 'does not error if the request fails' do expect do - Segment::Analytics::Transport + SegmentIO::Analytics::Transport .any_instance .stub(:send) - .and_return(Segment::Analytics::Response.new(-1, 'Unknown error')) + .and_return(SegmentIO::Analytics::Response.new(-1, 'Unknown error')) queue = Queue.new queue << {} - worker = Segment::Analytics::Worker.new(queue, 'secret') + worker = SegmentIO::Analytics::Worker.new(queue, 'secret') worker.run expect(queue).to be_empty - Segment::Analytics::Transport.any_instance.unstub(:send) + SegmentIO::Analytics::Transport.any_instance.unstub(:send) end.to_not raise_error end it 'executes the error handler if the request is invalid' do - Segment::Analytics::Transport + SegmentIO::Analytics::Transport .any_instance .stub(:send) - .and_return(Segment::Analytics::Response.new(400, 'Some error')) + .and_return(SegmentIO::Analytics::Response.new(400, 'Some error')) status = error = nil on_error = proc do |yielded_status, yielded_error| @@ -67,7 +67,7 @@ class Analytics sleep 0.1 # First give thread time to spin-up. sleep 0.01 while worker.is_requesting? - Segment::Analytics::Transport.any_instance.unstub(:send) + SegmentIO::Analytics::Transport.any_instance.unstub(:send) expect(queue).to be_empty expect(status).to eq(400) @@ -118,22 +118,22 @@ def bad_obj.to_json(*_args) describe '#is_requesting?' do it 'does not return true if there isn\'t a current batch' do queue = Queue.new - worker = Segment::Analytics::Worker.new(queue, 'testsecret') + worker = SegmentIO::Analytics::Worker.new(queue, 'testsecret') expect(worker.is_requesting?).to eq(false) end it 'returns true if there is a current batch' do - Segment::Analytics::Transport + SegmentIO::Analytics::Transport .any_instance .stub(:send) { sleep(0.2) - Segment::Analytics::Response.new(200, 'Success') + SegmentIO::Analytics::Response.new(200, 'Success') } queue = Queue.new queue << Requested::TRACK - worker = Segment::Analytics::Worker.new(queue, 'testsecret') + worker = SegmentIO::Analytics::Worker.new(queue, 'testsecret') worker_thread = Thread.new { worker.run } eventually { expect(worker.is_requesting?).to eq(true) } @@ -141,7 +141,7 @@ def bad_obj.to_json(*_args) worker_thread.join expect(worker.is_requesting?).to eq(false) - Segment::Analytics::Transport.any_instance.unstub(:send) + SegmentIO::Analytics::Transport.any_instance.unstub(:send) end end end diff --git a/spec/segment/analytics_spec.rb b/spec/segment/analytics_spec.rb index bf4dbedb..eda0597e 100644 --- a/spec/segment/analytics_spec.rb +++ b/spec/segment/analytics_spec.rb @@ -1,9 +1,9 @@ require 'spec_helper' -module Segment +module SegmentIO class Analytics describe Analytics do - let(:analytics) { Segment::Analytics.new :write_key => WRITE_KEY, :stub => true } + let(:analytics) { SegmentIO::Analytics.new :write_key => WRITE_KEY, :stub => true } describe '#track' do it 'errors without an event' do @@ -117,13 +117,13 @@ class Analytics end describe '#respond_to?' do - it 'responds to all public instance methods of Segment::Analytics::Client' do - expect(analytics).to respond_to(*Segment::Analytics::Client.public_instance_methods(false)) + it 'responds to all public instance methods of SegmentIO::Analytics::Client' do + expect(analytics).to respond_to(*SegmentIO::Analytics::Client.public_instance_methods(false)) end end describe '#method' do - Segment::Analytics::Client.public_instance_methods(false).each do |public_method| + SegmentIO::Analytics::Client.public_instance_methods(false).each do |public_method| it "returns a Method object with '#{public_method}' as argument" do expect(analytics.method(public_method).class).to eq(Method) end @@ -132,7 +132,7 @@ class Analytics describe '#test_queue' do context 'when not in mode' do - let(:analytics) { Segment::Analytics.new :write_key => WRITE_KEY, :stub => true, :test => true } + let(:analytics) { SegmentIO::Analytics.new :write_key => WRITE_KEY, :stub => true, :test => true } it 'returns TestQueue' do expect(analytics.test_queue).to be_a(TestQueue) @@ -146,7 +146,7 @@ class Analytics end context 'when not in test mode' do - let(:analytics) { Segment::Analytics.new :write_key => WRITE_KEY, :stub => true, :test => false } + let(:analytics) { SegmentIO::Analytics.new :write_key => WRITE_KEY, :stub => true, :test => false } it 'errors when not in test mode' do expect(analytics.instance_variable_get(:@test)).to be_falsey diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a0f92476..fa40a299 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,7 @@ # Setting timezone for ActiveSupport::TimeWithZone to UTC Time.zone = 'UTC' -module Segment +module SegmentIO class Analytics WRITE_KEY = 'testsecret' From 26c7d9e4f9c947ba33219feee11bb8aa0323dd95 Mon Sep 17 00:00:00 2001 From: Guilherme Taschetto Date: Thu, 19 Jan 2023 11:03:52 -0300 Subject: [PATCH 2/2] Fix Rubocop offenses --- spec/segment/analytics/worker_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/segment/analytics/worker_spec.rb b/spec/segment/analytics/worker_spec.rb index 688c8727..7bf4c7c7 100644 --- a/spec/segment/analytics/worker_spec.rb +++ b/spec/segment/analytics/worker_spec.rb @@ -11,8 +11,8 @@ class Analytics it 'accepts string keys' do queue = Queue.new worker = SegmentIO::Analytics::Worker.new(queue, - 'secret', - 'batch_size' => 100) + 'secret', + 'batch_size' => 100) batch = worker.instance_variable_get(:@batch) expect(batch.instance_variable_get(:@max_message_count)).to eq(100) end