Skip to content

Instantly share code, notes, and snippets.

@eqbal
Created December 6, 2016 12:54
Show Gist options
  • Save eqbal/d28c21a52f826c1e3bb586d92209bab8 to your computer and use it in GitHub Desktop.
Save eqbal/d28c21a52f826c1e3bb586d92209bab8 to your computer and use it in GitHub Desktop.
class Entry < ActiveRecord::Base
STATUS = {
weather: %w(Sunny Rainy Windy Dry),
landform: %w(Beach Cliff Desert Flat)
}
belongs_to :user
validates_presence_of :distance, :time_period, :date_time
validates :status_weather, inclusion: { in: STATUS[:weather] }
validates :status_landform, inclusion: { in: STATUS[:landform] }
validates_numericality_of :distance, :time_period
after_create :compare_speed_and_notify_user
def week
date = date_time.split(' ')[0]
Date.strptime(date, '%m/%d/%Y').strftime('%W')
end
def speed
distance / (time_period.to_f / 60)
end
private
def compare_speed_and_notify_user
entries_avg_speed = (Entry.all.sum(&:speed) / Entry.count).round(2)
if speed > entries_avg_speed
msg = 'You are doing great. Keep it up, superman. :)'
else
msg = 'Most of the users are faster than you. Try harder, dude! :('
end
NexmoClient.send_message(
from: 'Toptal',
to: user.mobile,
text: msg
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment