Skip to content

Commit

Permalink
refactor: Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jylamont committed Feb 24, 2025
1 parent b0ef065 commit db57f51
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 34 deletions.
7 changes: 3 additions & 4 deletions lib/vero/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ def reset!
def update_attributes(attributes = {})
return unless attributes.is_a?(Hash)

Vero::Config::ACCEPTED_ATTRIBUTES.each do |symbol|
method_name = :"#{symbol}="
send(method_name, attributes[symbol]) if attributes.key?(symbol) && respond_to?(method_name)
end
Vero::Config::ACCEPTED_ATTRIBUTES
.select { |attr| attributes.key?(attr) }
.each { |attr| public_send(:"#{attr}=", attributes[attr]) }
end
end
3 changes: 2 additions & 1 deletion lib/vero/sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ def self.senders
end

def self.call(api_class, sender_strategy, domain, options)
senders[sender_strategy].new.call(api_class, domain, options)
sender = senders[sender_strategy].new
sender.call(api_class, domain, options)
rescue => e
Vero::App.log(new, "method: #{api_class.name}, options: #{JSON.dump(options)}, error: #{e.message}")
raise e
Expand Down
26 changes: 23 additions & 3 deletions lib/vero/senders/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@

class Vero::Senders::Base
def call(api_class, domain, options)
response = api_class.perform(domain, options)
api_class = get_api_class(api_class)

Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: job performed")
response
resp = enqueue_work(api_class, domain, options)
Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: #{log_message}")

resp
end

def enqueue_work(api_class, domain, options)
api_class.perform(domain, options)
end

def log_message
"job performed"
end

def get_api_class(klass_name)
return klass_name unless klass_name.is_a?(String)

if Object.const_defined?(klass_name)
Object.const_get(klass_name)
else
raise ArgumentError, "Invalid API class name: #{klass_name}"
end
end
end
17 changes: 10 additions & 7 deletions lib/vero/senders/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

require "delayed_job"

class Vero::Senders::DelayedJob
def call(api_class, domain, options)
response = ::Delayed::Job.enqueue api_class.new(domain, options)

Vero::App.log(self, "method: #{api_class.name}, options: #{JSON.dump(options)}, response: delayed job queued")
response
class Vero::Senders::DelayedJob < Vero::Senders::Base
def enqueue_work(api_class, domain, options)
::Delayed::Job.enqueue api_class.new(domain, options)
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'"
if e.message == "Could not find table 'delayed_jobs'"
raise "To send requests asynchronously, you must configure delayed_job. Run `rails generate delayed_job:active_record` then `rake db:migrate`."
end

raise e
end

def log_message
"delayed job queued"
end
end
8 changes: 6 additions & 2 deletions lib/vero/senders/resque.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Vero::Senders::Resque
def call(api_class, domain, options)
class Vero::Senders::Resque < Vero::Senders::Base
def enqueue_work(api_class, domain, options)
::Resque.enqueue(::Vero::ResqueWorker, api_class.to_s, domain, options)
end

def log_message
"resque job queued"
end
end
12 changes: 6 additions & 6 deletions lib/vero/senders/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,9 +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: #{JSON.dump(options)}, response: sidekiq job queued")
class Vero::Senders::Sidekiq < Vero::Senders::Base
def enqueue_work(api_class, domain, options)
::Vero::SidekiqWorker.perform_async(api_class.to_s, domain, options)
end

response
def log_message
"sidekiq job queued"
end
end
8 changes: 6 additions & 2 deletions lib/vero/senders/sucker_punch.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
class Vero::Senders::SuckerPunch
def call(api_class, domain, options)
class Vero::Senders::SuckerPunch < Vero::Senders::Base
def enqueue_work(api_class, domain, options)
::Vero::SuckerPunchWorker.perform_async(api_class, domain, options)
end

def log_message
"sucker punch job queued"
end
end
26 changes: 17 additions & 9 deletions lib/vero/trackable/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,33 @@ def reset_trackable_map!
end

def to_vero
klass = self.class
symbols, other = klass.trackable_map.partition { |i| i.is_a?(Symbol) }
trackable_attrs, other = self.class.trackable_map.partition { |i| i.is_a?(Symbol) }

result = symbols.each_with_object({}) do |symbol, hash|
t = respond_to?(symbol) ? send(symbol) : nil
hash[symbol] = t unless t.nil?
result = trackable_attrs.each_with_object({}) do |attr, hash|
value = public_send(attr) if respond_to?(attr)
hash[attr] = value unless value.nil?
end

if other.is_a?(Array) && !other.empty?
other.select! { |i| i.is_a?(Hash) && i.key?(:extras) }

other.each do |h|
symbol = h[:extras]
t = respond_to?(symbol, true) ? send(symbol) : nil
result.merge!(t) if t.is_a?(Hash)
attr = h[:extras]

# `extras` methods can be private
if respond_to?(attr, true)
value = send(attr)
result.merge!(value) if value.is_a?(Hash)
end
end
end

result[:email] = result.delete(:email_address) if result.key?(:email_address)
if result.key?(:email_address)
result[:email] = result.delete(:email_address)
end

result[:_user_type] = self.class.name

result
end

Expand Down

0 comments on commit db57f51

Please sign in to comment.