From 40ef8d8c9de35510b5fe061c8d7dd0a5f64f6997 Mon Sep 17 00:00:00 2001 From: James Lamont <958588+jylamont@users.noreply.github.com> Date: Mon, 6 Jan 2025 19:15:58 -0500 Subject: [PATCH] refactor: Refactor existing code --- Gemfile | 1 + lib/vero/api/base.rb | 2 +- lib/vero/api/workers/base_api.rb | 16 +++++--- lib/vero/api/workers/events/track_api.rb | 4 +- lib/vero/api/workers/users/delete_api.rb | 4 +- lib/vero/api/workers/users/edit_api.rb | 4 +- lib/vero/api/workers/users/edit_tags_api.rb | 4 +- lib/vero/api/workers/users/reidentify_api.rb | 4 +- lib/vero/api/workers/users/resubscribe_api.rb | 4 +- lib/vero/api/workers/users/track_api.rb | 4 +- lib/vero/api/workers/users/unsubscribe_api.rb | 4 +- lib/vero/config.rb | 10 ++--- lib/vero/context.rb | 4 +- lib/vero/resque_worker.rb | 5 +-- lib/vero/sender.rb | 35 ++++++++-------- lib/vero/senders/base.rb | 4 +- lib/vero/senders/delayed_job.rb | 4 +- lib/vero/senders/sidekiq.rb | 4 +- lib/vero/sidekiq_worker.rb | 1 + lib/vero/sucker_punch_worker.rb | 14 +++---- spec/lib/api/events/track_api_spec.rb | 15 +++++-- spec/lib/api/users/delete_api_spec.rb | 9 ++++- spec/lib/api/users/edit_api_spec.rb | 17 ++++++-- spec/lib/api/users/edit_tags_api_spec.rb | 16 ++++++-- spec/lib/api/users/reidentify_spec.rb | 17 ++++++-- spec/lib/api/users/resubscribe_api_spec.rb | 12 +++--- spec/lib/api/users/track_api_spec.rb | 17 ++++++-- spec/lib/api/users/unsubscribe_api_spec.rb | 14 +++++-- spec/lib/api_spec.rb | 12 +++--- spec/lib/trackable_spec.rb | 40 +++++++++---------- spec/spec_helper.rb | 4 ++ spec/support/user_support.rb | 16 ++++---- 32 files changed, 192 insertions(+), 129 deletions(-) diff --git a/Gemfile b/Gemfile index e84232f..b6e85f0 100644 --- a/Gemfile +++ b/Gemfile @@ -17,4 +17,5 @@ end group :test do gem "rspec" + gem "webmock" end diff --git a/lib/vero/api/base.rb b/lib/vero/api/base.rb index ba35b44..3aedd1e 100644 --- a/lib/vero/api/base.rb +++ b/lib/vero/api/base.rb @@ -16,7 +16,7 @@ def run_api(api_klass, options) validate_configured! options.merge!(config.request_params) - Vero::Sender.send(api_klass, config.async, config.domain, options) + Vero::Sender.call(api_klass, config.async, config.domain, options) end protected diff --git a/lib/vero/api/workers/base_api.rb b/lib/vero/api/workers/base_api.rb index 7482236..cf13531 100644 --- a/lib/vero/api/workers/base_api.rb +++ b/lib/vero/api/workers/base_api.rb @@ -7,8 +7,7 @@ class Vero::Api::Workers::BaseAPI attr_reader :options def self.perform(domain, options) - caller = new(domain, options) - caller.perform + new(domain, options).perform end def initialize(domain, options) @@ -46,6 +45,15 @@ def validate! end def request + if request_method == :get + RestClient.get(url) + else + RestClient.send(request_method, url, request_params_as_json, request_content_type) + end + end + + def request_method + raise NotImplementedError, "#{self.class.name}#request_method should be overridden" end def request_content_type @@ -57,8 +65,6 @@ def request_params_as_json end def options_with_symbolized_keys(val) - val.each_with_object({}) do |(k, v), h| - h[k.to_sym] = v - end + val.transform_keys(&:to_sym) end end diff --git a/lib/vero/api/workers/events/track_api.rb b/lib/vero/api/workers/events/track_api.rb index 001d74e..03610d8 100644 --- a/lib/vero/api/workers/events/track_api.rb +++ b/lib/vero/api/workers/events/track_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/events/track.json" end - def request - RestClient.post(url, request_params_as_json, request_content_type) + def request_method + :post end def validate! diff --git a/lib/vero/api/workers/users/delete_api.rb b/lib/vero/api/workers/users/delete_api.rb index 552496d..3e393a4 100644 --- a/lib/vero/api/workers/users/delete_api.rb +++ b/lib/vero/api/workers/users/delete_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/users/delete.json" end - def request - RestClient.post(url, @options) + def request_method + :post end def validate! diff --git a/lib/vero/api/workers/users/edit_api.rb b/lib/vero/api/workers/users/edit_api.rb index 508bae9..e1deb79 100644 --- a/lib/vero/api/workers/users/edit_api.rb +++ b/lib/vero/api/workers/users/edit_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/users/edit.json" end - def request - RestClient.put(url, request_params_as_json, request_content_type) + def request_method + :put end def validate! diff --git a/lib/vero/api/workers/users/edit_tags_api.rb b/lib/vero/api/workers/users/edit_tags_api.rb index 64e6eba..cf16bb2 100644 --- a/lib/vero/api/workers/users/edit_tags_api.rb +++ b/lib/vero/api/workers/users/edit_tags_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/users/tags/edit.json" end - def request - RestClient.put(url, request_params_as_json, request_content_type) + def request_method + :put end def validate! diff --git a/lib/vero/api/workers/users/reidentify_api.rb b/lib/vero/api/workers/users/reidentify_api.rb index cec0edd..ec4da2f 100644 --- a/lib/vero/api/workers/users/reidentify_api.rb +++ b/lib/vero/api/workers/users/reidentify_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/users/reidentify.json" end - def request - RestClient.put(url, request_params_as_json, request_content_type) + def request_method + :put end def validate! diff --git a/lib/vero/api/workers/users/resubscribe_api.rb b/lib/vero/api/workers/users/resubscribe_api.rb index 11c4d17..32a1f8f 100644 --- a/lib/vero/api/workers/users/resubscribe_api.rb +++ b/lib/vero/api/workers/users/resubscribe_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/users/resubscribe.json" end - def request - RestClient.post(url, @options) + def request_method + :post end def validate! diff --git a/lib/vero/api/workers/users/track_api.rb b/lib/vero/api/workers/users/track_api.rb index fff7650..05e6804 100644 --- a/lib/vero/api/workers/users/track_api.rb +++ b/lib/vero/api/workers/users/track_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/users/track.json" end - def request - RestClient.post(url, request_params_as_json, request_content_type) + def request_method + :post end def validate! diff --git a/lib/vero/api/workers/users/unsubscribe_api.rb b/lib/vero/api/workers/users/unsubscribe_api.rb index b3f293b..10dc449 100644 --- a/lib/vero/api/workers/users/unsubscribe_api.rb +++ b/lib/vero/api/workers/users/unsubscribe_api.rb @@ -5,8 +5,8 @@ def url "#{@domain}/api/v2/users/unsubscribe.json" end - def request - RestClient.post(url, @options) + def request_method + :post end def validate! diff --git a/lib/vero/config.rb b/lib/vero/config.rb index d7ee2c7..c8d447f 100644 --- a/lib/vero/config.rb +++ b/lib/vero/config.rb @@ -4,9 +4,7 @@ class Vero::Config attr_writer :domain attr_accessor :tracking_api_key, :development_mode, :async, :disabled, :logging - def self.available_attributes - %i[tracking_api_key development_mode async disabled logging domain] - end + ACCEPTED_ATTRIBUTES = %i[tracking_api_key development_mode async disabled logging domain] def initialize reset! @@ -51,9 +49,9 @@ def reset! def update_attributes(attributes = {}) return unless attributes.is_a?(Hash) - Vero::Config.available_attributes.each do |symbol| - method_name = "#{symbol}=" - send(method_name, attributes[symbol]) if respond_to?(method_name) && attributes.key?(symbol) + Vero::Config::ACCEPTED_ATTRIBUTES.each do |symbol| + method_name = :"#{symbol}=" + send(method_name, attributes[symbol]) if attributes.key?(symbol) && respond_to?(method_name) end end end diff --git a/lib/vero/context.rb b/lib/vero/context.rb index d0f2801..0532667 100644 --- a/lib/vero/context.rb +++ b/lib/vero/context.rb @@ -13,8 +13,8 @@ def initialize(object = {}) @config = object.config @subject = object.subject else - object = Vero::Config.available_attributes.each_with_object({}) do |symbol, hash| - hash[symbol] = object.respond_to?(symbol) ? object.send(symbol) : nil + object = Vero::Config::ACCEPTED_ATTRIBUTES.each_with_object({}) do |symbol, hash| + hash[symbol] = object.send(symbol) if object.respond_to?(symbol) end end return unless object.is_a?(Hash) diff --git a/lib/vero/resque_worker.rb b/lib/vero/resque_worker.rb index 6e8149a..7c7f2d1 100644 --- a/lib/vero/resque_worker.rb +++ b/lib/vero/resque_worker.rb @@ -6,11 +6,10 @@ class Vero::ResqueWorker @queue = :vero def self.perform(api_class, domain, options) - new_options = options.each_with_object({}) do |(k, v), o| - o[k.to_sym] = v - end + new_options = options.transform_keys(&:to_sym) api_class.constantize.new(domain, new_options).perform + Vero::App.log(self, "method: #{api_class}, options: #{options.to_json}, response: resque job queued") end end diff --git a/lib/vero/sender.rb b/lib/vero/sender.rb index 839ed01..b221ffd 100644 --- a/lib/vero/sender.rb +++ b/lib/vero/sender.rb @@ -1,29 +1,30 @@ # frozen_string_literal: true module Vero - class SenderLookup - def [](key) - klass_name = key.to_s.split("_").map(&:capitalize).join - - if Vero::Senders.const_defined?(klass_name) - Vero::Senders.const_get(klass_name) - else - Vero::Senders::Base - end - end - end - class Sender def self.senders - @senders ||= Vero::SenderLookup.new + @senders ||= Vero::Sender::Lookup.new end - def self.send(api_class, sender_strategy, domain, options) - senders[sender_strategy].new.call(api_class, domain, options) + def self.call(api_class, sender_strategy, domain, options) + senders[sender_strategy] + .new + .call(api_class, domain, options) rescue => e - options_s = JSON.dump(options) - Vero::App.log(new, "method: #{api_class.name}, options: #{options_s}, error: #{e.message}") + Vero::App.log(new, "method: #{api_class.name}, options: #{JSON.dump(options)}, error: #{e.message}") raise e end + + class Lookup + def [](key) + klass_name = key.to_s.split("_").map(&:capitalize).join + + if Vero::Senders.const_defined?(klass_name) + Vero::Senders.const_get(klass_name) + else + Vero::Senders::Base + end + end + end end end diff --git a/lib/vero/senders/base.rb b/lib/vero/senders/base.rb index 217d7f7..5147a9d 100644 --- a/lib/vero/senders/base.rb +++ b/lib/vero/senders/base.rb @@ -3,8 +3,8 @@ class Vero::Senders::Base def call(api_class, domain, options) response = api_class.perform(domain, options) - options_s = JSON.dump(options) - Vero::App.log(self, "method: #{api_class.name}, options: #{options_s}, response: job performed") + + Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: job performed") response end end diff --git a/lib/vero/senders/delayed_job.rb b/lib/vero/senders/delayed_job.rb index 8e35f66..ef488e8 100644 --- a/lib/vero/senders/delayed_job.rb +++ b/lib/vero/senders/delayed_job.rb @@ -5,8 +5,8 @@ class Vero::Senders::DelayedJob def call(api_class, domain, options) response = ::Delayed::Job.enqueue api_class.new(domain, options) - options_s = JSON.dump(options) - Vero::App.log(self, "method: #{api_class.name}, options: #{options_s}, response: delayed job queued") + + Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: delayed job queued") response rescue => e raise "To send ratings asynchronously, you must configure delayed_job. Run `rails generate delayed_job:active_record` then `rake db:migrate`." if e.message == "Could not find table 'delayed_jobs'" diff --git a/lib/vero/senders/sidekiq.rb b/lib/vero/senders/sidekiq.rb index 87482c9..aa50651 100644 --- a/lib/vero/senders/sidekiq.rb +++ b/lib/vero/senders/sidekiq.rb @@ -1,7 +1,9 @@ class Vero::Senders::Sidekiq def call(api_class, domain, options) response = ::Vero::SidekiqWorker.perform_async(api_class.to_s, domain, options) - Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: sidekiq job queued") + + Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: sidekiq job queued") + response end end diff --git a/lib/vero/sidekiq_worker.rb b/lib/vero/sidekiq_worker.rb index 827155d..ee3ed61 100644 --- a/lib/vero/sidekiq_worker.rb +++ b/lib/vero/sidekiq_worker.rb @@ -7,6 +7,7 @@ class Vero::SidekiqWorker def perform(api_class, domain, options) api_class.constantize.new(domain, options).perform + Vero::App.log(self, "method: #{api_class}, options: #{options.to_json}, response: sidekiq job queued") end end diff --git a/lib/vero/sucker_punch_worker.rb b/lib/vero/sucker_punch_worker.rb index 0a6914d..32b496e 100644 --- a/lib/vero/sucker_punch_worker.rb +++ b/lib/vero/sucker_punch_worker.rb @@ -6,14 +6,12 @@ class Vero::SuckerPunchWorker include SuckerPunch::Job def perform(api_class, domain, options) - new_options = {} - options.each { |k, v| new_options[k.to_sym] = v } + new_options = options.transform_keys(&:to_sym) - begin - api_class.new(domain, new_options).perform - Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: job performed") - rescue => e - Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: #{e.message}") - end + api_class.new(domain, new_options).perform + + Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: job performed") + rescue => e + Vero::App.log(self, "method: #{api_class.name}, options: #{options.to_json}, response: #{e.message}") end end diff --git a/spec/lib/api/events/track_api_spec.rb b/spec/lib/api/events/track_api_spec.rb index c52a9a2..424f742 100644 --- a/spec/lib/api/events/track_api_spec.rb +++ b/spec/lib/api/events/track_api_spec.rb @@ -47,11 +47,18 @@ end end - describe :request do - it "should send a JSON request to the Vero API" do - expect(RestClient).to receive(:post).with("https://api.getvero.com/api/v2/events/track.json", {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"}.to_json, {content_type: :json, accept: :json}) - allow(RestClient).to receive(:post).and_return(200) + describe "request" do + it "should send a request to the Vero API" do + stub = stub_request(:post, "https://api.getvero.com/api/v2/events/track.json") + .with( + body: {auth_token: "abcd", identity: {email: "test@test.com"}, event_name: "test_event"}.to_json, + headers: {"Content-Type" => "application/json", "Accept" => "application/json"} + ) + .to_return(status: 200) + subject.send(:request) + + expect(stub).to have_been_requested end end end diff --git a/spec/lib/api/users/delete_api_spec.rb b/spec/lib/api/users/delete_api_spec.rb index c0a9d17..4617cc6 100644 --- a/spec/lib/api/users/delete_api_spec.rb +++ b/spec/lib/api/users/delete_api_spec.rb @@ -25,10 +25,15 @@ end end - describe :request do + describe "request" do it "should send a request to the Vero API" do - expect(RestClient).to receive(:post).with("https://api.getvero.com/api/v2/users/delete.json", {auth_token: "abcd", id: "1234"}).and_return(200) + stub = stub_request(:post, "https://api.getvero.com/api/v2/users/delete.json") + .with(body: {auth_token: "abcd", id: "1234"}) + .to_return(status: 200) + subject.send(:request) + + expect(stub).to have_been_requested end end end diff --git a/spec/lib/api/users/edit_api_spec.rb b/spec/lib/api/users/edit_api_spec.rb index 088062b..c896409 100644 --- a/spec/lib/api/users/edit_api_spec.rb +++ b/spec/lib/api/users/edit_api_spec.rb @@ -17,17 +17,26 @@ end end - describe :request do + describe "request" do it "should send a request to the Vero API" do - expect(RestClient).to receive(:put).with("https://api.getvero.com/api/v2/users/edit.json", {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}.to_json, {content_type: :json, accept: :json}) - allow(RestClient).to receive(:put).and_return(200) + stub = stub_request(:put, "https://api.getvero.com/api/v2/users/edit.json") + .with( + body: {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}.to_json, + headers: {"Content-Type" => "application/json", "Accept" => "application/json"} + ) + .to_return(status: 200) + subject.send(:request) + + expect(stub).to have_been_requested end end describe "integration test" do it "should not raise any errors" do - allow(RestClient).to receive(:put).and_return(200) + stub_request(:put, "https://api.getvero.com/api/v2/users/edit.json") + .to_return(status: 200) + expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/edit_tags_api_spec.rb b/spec/lib/api/users/edit_tags_api_spec.rb index 7ecc756..cc4dc44 100644 --- a/spec/lib/api/users/edit_tags_api_spec.rb +++ b/spec/lib/api/users/edit_tags_api_spec.rb @@ -55,17 +55,25 @@ end end - describe :request do + describe "request" do it "should send a request to the Vero API" do - expect(RestClient).to receive(:put).with("https://api.getvero.com/api/v2/users/tags/edit.json", {auth_token: "abcd", email: "test@test.com", add: ["test"]}.to_json, {content_type: :json, accept: :json}) - allow(RestClient).to receive(:put).and_return(200) + stub = stub_request(:put, "https://api.getvero.com/api/v2/users/tags/edit.json") + .with( + body: {auth_token: "abcd", email: "test@test.com", add: ["test"]}.to_json, + headers: {"Content-Type" => "application/json", "Accept" => "application/json"} + ) + .to_return(status: 200) + subject.send(:request) + + expect(stub).to have_been_requested end end describe "integration test" do it "should not raise any errors" do - allow(RestClient).to receive(:put).and_return(200) + stub_request(:put, "https://api.getvero.com/api/v2/users/tags/edit.json") + .to_return(status: 200) expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/reidentify_spec.rb b/spec/lib/api/users/reidentify_spec.rb index 15481ae..aa48178 100644 --- a/spec/lib/api/users/reidentify_spec.rb +++ b/spec/lib/api/users/reidentify_spec.rb @@ -27,17 +27,26 @@ end end - describe :request do + describe "request" do it "should send a request to the Vero API" do - expect(RestClient).to receive(:put).with("https://api.getvero.com/api/v2/users/reidentify.json", {auth_token: "abcd", id: "test@test.com", new_id: "test2@test.com"}.to_json, {content_type: :json, accept: :json}) - allow(RestClient).to receive(:put).and_return(200) + stub = stub_request(:put, "https://api.getvero.com/api/v2/users/reidentify.json") + .with( + body: {auth_token: "abcd", id: "test@test.com", new_id: "test2@test.com"}.to_json, + headers: {"Accept" => "application/json", "Content-Type" => "application/json"} + ) + .to_return(status: 200) + subject.send(:request) + + expect(stub).to have_been_requested end end describe "integration test" do it "should not raise any errors" do - allow(RestClient).to receive(:put).and_return(200) + stub_request(:put, "https://api.getvero.com/api/v2/users/reidentify.json") + .to_return(status: 200) + expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/resubscribe_api_spec.rb b/spec/lib/api/users/resubscribe_api_spec.rb index a8a8a0a..0e72ee3 100644 --- a/spec/lib/api/users/resubscribe_api_spec.rb +++ b/spec/lib/api/users/resubscribe_api_spec.rb @@ -21,15 +21,15 @@ end end - describe :request do + describe "request" do it "should send a request to the Vero API" do - expect(RestClient).to( - receive(:post) - .with("https://api.getvero.com/api/v2/users/resubscribe.json", {auth_token: "abcd", id: "1234"}) - ) - allow(RestClient).to receive(:post).and_return(200) + stub = stub_request(:post, "https://api.getvero.com/api/v2/users/resubscribe.json") + .with(body: {auth_token: "abcd", id: "1234"}) + .to_return(status: 200) subject.send(:request) + + expect(stub).to have_been_requested end end end diff --git a/spec/lib/api/users/track_api_spec.rb b/spec/lib/api/users/track_api_spec.rb index 8b8dc8a..2f2a3b8 100644 --- a/spec/lib/api/users/track_api_spec.rb +++ b/spec/lib/api/users/track_api_spec.rb @@ -49,17 +49,26 @@ end end - describe :request do + describe "request" do it "should send a request to the Vero API" do - expect(RestClient).to receive(:post).with("https://api.getvero.com/api/v2/users/track.json", {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com"}.to_json, {content_type: :json, accept: :json}) - allow(RestClient).to receive(:post).and_return(200) + stub = stub_request(:post, "https://api.getvero.com/api/v2/users/track.json") + .with( + body: {auth_token: "abcd", identity: {email: "test@test.com"}, email: "test@test.com"}.to_json, + headers: {"Content-Type" => "application/json", "Accept" => "application/json"} + ) + .to_return(status: 200) + subject.send(:request) + + expect(stub).to have_been_requested end end describe "integration test" do it "should not raise any errors" do - allow(RestClient).to receive(:post).and_return(200) + stub_request(:post, "https://api.getvero.com/api/v2/users/track.json") + .to_return(status: 200) + expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api/users/unsubscribe_api_spec.rb b/spec/lib/api/users/unsubscribe_api_spec.rb index 749903d..ee69aee 100644 --- a/spec/lib/api/users/unsubscribe_api_spec.rb +++ b/spec/lib/api/users/unsubscribe_api_spec.rb @@ -17,17 +17,23 @@ end end - describe :request do + describe "request" do it "should send a request to the Vero API" do - expect(RestClient).to receive(:post).with("https://api.getvero.com/api/v2/users/unsubscribe.json", {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}) - allow(RestClient).to receive(:post).and_return(200) + stub = stub_request(:post, "https://api.getvero.com/api/v2/users/unsubscribe.json") + .with(body: {auth_token: "abcd", email: "test@test.com", changes: {email: "test@test.com"}}) + .to_return(status: 200) + subject.send(:request) + + expect(stub).to have_been_requested end end describe "integration test" do it "should not raise any errors" do - allow(RestClient).to receive(:post).and_return(200) + stub_request(:post, "https://api.getvero.com/api/v2/users/unsubscribe.json") + .to_return(status: 200) + expect { subject.perform }.to_not raise_error end end diff --git a/spec/lib/api_spec.rb b/spec/lib/api_spec.rb index cf46c4e..c6fa93a 100644 --- a/spec/lib/api_spec.rb +++ b/spec/lib/api_spec.rb @@ -17,7 +17,7 @@ allow(Vero::App).to receive(:default_context).and_return(mock_context) - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Events::TrackAPI, true, "https://api.getvero.com", expected) + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Events::TrackAPI, true, "https://api.getvero.com", expected) subject.track!(input) end @@ -41,7 +41,7 @@ let(:input) { {email: "james@getvero.com", data: {age: 25}} } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::TrackAPI, true, "https://api.getvero.com", expected) + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::TrackAPI, true, "https://api.getvero.com", expected) subject.track!(input) end end @@ -52,7 +52,7 @@ let(:input) { {email: "james@getvero.com", changes: {age: 25}} } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditAPI, true, "https://api.getvero.com", expected) + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::EditAPI, true, "https://api.getvero.com", expected) subject.edit_user!(input) end end @@ -63,7 +63,7 @@ let(:input) { {add: ["boom"], remove: ["tish"]} } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::EditTagsAPI, true, "https://api.getvero.com", expected) + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::EditTagsAPI, true, "https://api.getvero.com", expected) subject.edit_user_tags!(input) end end @@ -74,7 +74,7 @@ let(:input) { {email: "james@getvero"} } specify do - expect(Vero::Sender).to receive(:send).with(Vero::Api::Workers::Users::UnsubscribeAPI, true, "https://api.getvero.com", expected) + expect(Vero::Sender).to receive(:call).with(Vero::Api::Workers::Users::UnsubscribeAPI, true, "https://api.getvero.com", expected) subject.unsubscribe!(input) end end @@ -86,7 +86,7 @@ specify do expect(Vero::Sender).to( - receive(:send) + receive(:call) .with(Vero::Api::Workers::Users::ResubscribeAPI, true, "https://api.getvero.com", expected) ) subject.resubscribe!(input) diff --git a/spec/lib/trackable_spec.rb b/spec/lib/trackable_spec.rb index 76f7300..52cec12 100644 --- a/spec/lib/trackable_spec.rb +++ b/spec/lib/trackable_spec.rb @@ -17,7 +17,7 @@ def vero_context(user, logging = true, async = false, disabled = true) @request_params = { event_name: "test_event", tracking_api_key: "YWJjZDEyMzQ6ZWZnaDU2Nzg=", - identity: {email: "durkster@gmail.com", age: 20, _user_type: "User"}, + identity: {email: "user@getvero.com", age: 20, _user_type: "User"}, data: {test: 1}, development_mode: true } @@ -49,7 +49,7 @@ def vero_context(user, logging = true, async = false, disabled = true) before do @request_params = { event_name: "test_event", - identity: {email: "durkster@gmail.com", age: 20, _user_type: "User"}, + identity: {email: "user@getvero.com", age: 20, _user_type: "User"}, data: {test: 1}, extras: {} } @@ -94,8 +94,8 @@ def vero_context(user, logging = true, async = false, disabled = true) before do @request_params = { id: nil, - email: "durkster@gmail.com", - data: {email: "durkster@gmail.com", age: 20, _user_type: "User"} + email: "user@getvero.com", + data: {email: "user@getvero.com", age: 20, _user_type: "User"} } @url = "https://api.getvero.com/api/v2/users/track.json" end @@ -126,8 +126,8 @@ def vero_context(user, logging = true, async = false, disabled = true) before do @request_params = { id: nil, - email: "durkster@gmail.com", - changes: {email: "durkster@gmail.com", age: 20, _user_type: "User"} + email: "user@getvero.com", + changes: {email: "user@getvero.com", age: 20, _user_type: "User"} } @url = "https://api.getvero.com/api/v2/users/edit.json" end @@ -160,7 +160,7 @@ def vero_context(user, logging = true, async = false, disabled = true) before do @request_params = { id: nil, - email: "durkster@gmail.com", + email: "user@getvero.com", add: [], remove: [] } @@ -195,7 +195,7 @@ def vero_context(user, logging = true, async = false, disabled = true) before do @request_params = { id: nil, - email: "durkster@gmail.com" + email: "user@getvero.com" } @url = "https://api.getvero.com/api/v2/users/unsubscribe.json" end @@ -253,46 +253,46 @@ def vero_context(user, logging = true, async = false, disabled = true) it "should return a hash of all values mapped by trackable" do user = User.new - expect(user.to_vero).to eq({email: "durkster@gmail.com", age: 20, _user_type: "User"}) + expect(user.to_vero).to eq({email: "user@getvero.com", age: 20, _user_type: "User"}) user = UserWithoutEmail.new - expect(user.to_vero).to eq({email: "durkster@gmail.com", age: 20, _user_type: "UserWithoutEmail"}) + expect(user.to_vero).to eq({email: "user@getvero.com", age: 20, _user_type: "UserWithoutEmail"}) user = UserWithEmailAddress.new - expect(user.to_vero).to eq({email: "durkster@gmail.com", age: 20, _user_type: "UserWithEmailAddress"}) + expect(user.to_vero).to eq({email: "user@getvero.com", age: 20, _user_type: "UserWithEmailAddress"}) user = UserWithoutInterface.new - expect(user.to_vero).to eq({email: "durkster@gmail.com", age: 20, _user_type: "UserWithoutInterface"}) + expect(user.to_vero).to eq({email: "user@getvero.com", age: 20, _user_type: "UserWithoutInterface"}) user = UserWithNilAttributes.new - expect(user.to_vero).to eq({email: "durkster@gmail.com", _user_type: "UserWithNilAttributes"}) + expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithNilAttributes"}) end it "should take into account any defined extras" do user = UserWithExtras.new user.properties = nil - expect(user.to_vero).to eq({email: "durkster@gmail.com", _user_type: "UserWithExtras"}) + expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithExtras"}) user.properties = "test" - expect(user.to_vero).to eq({email: "durkster@gmail.com", _user_type: "UserWithExtras"}) + expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithExtras"}) user.properties = {} - expect(user.to_vero).to eq({email: "durkster@gmail.com", _user_type: "UserWithExtras"}) + expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithExtras"}) user.properties = { age: 20, gender: "female" } - expect(user.to_vero).to eq({email: "durkster@gmail.com", age: 20, gender: "female", _user_type: "UserWithExtras"}) + expect(user.to_vero).to eq({email: "user@getvero.com", age: 20, gender: "female", _user_type: "UserWithExtras"}) user = UserWithPrivateExtras.new - expect(user.to_vero).to eq({email: "durkster@gmail.com", age: 26, _user_type: "UserWithPrivateExtras"}) + expect(user.to_vero).to eq({email: "user@getvero.com", age: 26, _user_type: "UserWithPrivateExtras"}) end it "should allow extras to be provided instead :id or :email" do user = UserWithOnlyExtras.new user.properties = {email: user.email} - expect(user.to_vero).to eq({email: "durkster@gmail.com", _user_type: "UserWithOnlyExtras"}) + expect(user.to_vero).to eq({email: "user@getvero.com", _user_type: "UserWithOnlyExtras"}) end end @@ -310,7 +310,7 @@ def vero_context(user, logging = true, async = false, disabled = true) request_params = { event_name: "test_event", tracking_api_key: "YWJjZDEyMzQ6ZWZnaDU2Nzg=", - identity: {email: "durkster@gmail.com", age: 20, _user_type: "UserWithoutInterface"}, + identity: {email: "user@getvero.com", age: 20, _user_type: "UserWithoutInterface"}, data: {test: 1}, development_mode: true } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 05119f1..87104fe 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,6 +2,8 @@ require "rubygems" require "bundler/setup" +require "rspec" +require "webmock/rspec" require "vero" @@ -13,6 +15,8 @@ end config.raise_errors_for_deprecations! + + WebMock.disable_net_connect! end def stub_env(new_env, &block) diff --git a/spec/support/user_support.rb b/spec/support/user_support.rb index 72bb951..66daa8b 100644 --- a/spec/support/user_support.rb +++ b/spec/support/user_support.rb @@ -7,7 +7,7 @@ class User trackable :email, :age def email - "durkster@gmail.com" + "user@getvero.com" end def age @@ -24,7 +24,7 @@ def email end def primary_contact - "durkster@gmail.com" + "user@getvero.com" end def age @@ -37,7 +37,7 @@ class UserWithEmailAddress trackable :email_address, :age def email_address - "durkster@gmail.com" + "user@getvero.com" end def age @@ -50,7 +50,7 @@ class UserWithoutInterface trackable :email_address, :age def email_address - "durkster@gmail.com" + "user@getvero.com" end def age @@ -67,7 +67,7 @@ class UserWithNilAttributes trackable :email_address, :age def email_address - "durkster@gmail.com" + "user@getvero.com" end def age @@ -82,7 +82,7 @@ class UserWithExtras attr_accessor :properties def email - "durkster@gmail.com" + "user@getvero.com" end end @@ -91,7 +91,7 @@ class UserWithPrivateExtras trackable :email, {extras: :properties} def email - "durkster@gmail.com" + "user@getvero.com" end private @@ -108,6 +108,6 @@ class UserWithOnlyExtras attr_accessor :properties def email - "durkster@gmail.com" + "user@getvero.com" end end