Skip to content

Instantly share code, notes, and snippets.

@perezpaya
Last active March 28, 2016 23:58
Show Gist options
  • Save perezpaya/d5adb3054b917e97c391 to your computer and use it in GitHub Desktop.
Save perezpaya/d5adb3054b917e97c391 to your computer and use it in GitHub Desktop.
class CompanyMarketUpdatesService
def initialize(notification_job: NotificationJob.new)
@notification_job = notification_job
end
def update(params)
Company.find_by_stock_symbol(params[:stock_symbol])&.tap do |company|
should_notify = should_notify_stock_price_reduction?(company.stock_price, params[:stock_price])
company.update(params)
notify_stock_price_reduction(company) if should_notify
end
end
private
def should_notify_stock_price_reduction?(previous_stock_price, current_stock_price)
previous_stock_price > current_stock_price
end
def notify_stock_price_reduction(company)
@notification_job.perform(message: "Stock price for #{company.stock_symbol} down to #{company.stock_price}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment