Skip to content

Commit

Permalink
refactor: Refactor existing code
Browse files Browse the repository at this point in the history
  • Loading branch information
jylamont committed Jan 7, 2025
1 parent 8650a42 commit 40ef8d8
Show file tree
Hide file tree
Showing 32 changed files with 192 additions and 129 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ end

group :test do
gem "rspec"
gem "webmock"
end
2 changes: 1 addition & 1 deletion lib/vero/api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 11 additions & 5 deletions lib/vero/api/workers/base_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions lib/vero/api/workers/events/track_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/api/workers/users/delete_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/api/workers/users/edit_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/api/workers/users/edit_tags_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/api/workers/users/reidentify_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/api/workers/users/resubscribe_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/api/workers/users/track_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 2 additions & 2 deletions lib/vero/api/workers/users/unsubscribe_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
10 changes: 4 additions & 6 deletions lib/vero/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/vero/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions lib/vero/resque_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 18 additions & 17 deletions lib/vero/sender.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions lib/vero/senders/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/vero/senders/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
Expand Down
4 changes: 3 additions & 1 deletion lib/vero/senders/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/vero/sidekiq_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 6 additions & 8 deletions lib/vero/sucker_punch_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 11 additions & 4 deletions spec/lib/api/events/track_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]"}, 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: "[email protected]"}, 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
Expand Down
9 changes: 7 additions & 2 deletions spec/lib/api/users/delete_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 13 additions & 4 deletions spec/lib/api/users/edit_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]", changes: {email: "[email protected]"}}.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: "[email protected]", changes: {email: "[email protected]"}}.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
Expand Down
16 changes: 12 additions & 4 deletions spec/lib/api/users/edit_tags_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]", 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: "[email protected]", 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
Expand Down
Loading

0 comments on commit 40ef8d8

Please sign in to comment.