Skip to content

Instantly share code, notes, and snippets.

@dannymcc
Created March 2, 2014 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannymcc/9311982 to your computer and use it in GitHub Desktop.
Save dannymcc/9311982 to your computer and use it in GitHub Desktop.
# SMS voucher system
def voucher
p = params
twilio_number = "+441257733073"
# Removes the +44 and replaces it with 0
number = p["From"].gsub("+44", "0")
# Splits "wormer 1" and "wormer1"
body = p["Body"].split(/(\d+)/)
# Finds the client
# .gsub(" ", "") -- removes any spaces
# .sub(/(\d{5})(\d{6})/, "\\1 \\2") -- parses the number with a space like 01234 567890
@client = Client.all.select{|c| c.Mobile_Number =~ /#{number.gsub(" ", "")}/}.first || Client.all.select{|c| c.Mobile_Number =~ /#{number.sub(/(\d{5})(\d{6})/, "\\1 \\2")}/}.first
# If there isn't a client found
unless @client
# The number doesn't exist in the database so send the client a message saying so.
message = TWILIO_CLIENT.account.sms.messages.create(
from: twilio_number, to: number,
body: "Sorry, we don't recognise your number. Please call us on 01257 262448 so we can add your number to your account. Thanks, Hillcrest Animal Hospital"
)
end
# If the client can't request a voucher
if @client && !@client.can_request_voucher?
if @client.vouchers.where(claimed: false).count > 0
# Send a message when the client has an existing voucher that hasn't been claimed
message = TWILIO_CLIENT.account.sms.messages.create(
from: twilio_number, to: number,
body: "It appears you currently have a voucher that hasn't been used yet. You can only have one voucher at a time."
)
elsif DateTime.now < @client.vouchers.last.expires_at
# Send a message when the client can't request a voucher within 3 months
message = TWILIO_CLIENT.account.sms.messages.create(
from: twilio_number, to: number,
body: "Sorry, you cannot claim more than 1 voucher in any 3 month period."
)
end
end
# If there is a client and they can request a voucher
if @client && @client.can_request_voucher?
# Find animals that belong to any of the healthplans
# And only show alive animals
# HS - Silver Healthplan
# HG - Gold Healthplan
# HP - Platinum Healthplan
@animals = @client.animals.where(status: ["HS", "HG", "HP"]).where(dead: "Alive")
# If there is a pet name after "WORMER"
if body.second.present?
if body.second.to_i != 0
# Creates a hash using the animals_hash on the client model
hash = @client.animals_hash(@animals)
# Pick the animal
@animal = hash[body.second.to_i]
# Can the client request a voucher for the selected animal?
if @client.can_request_voucher_for_animal?(@animal)
# Yes so create and send the voucher!
send_voucher(twilio_number, number) if @animal
else
# Send the client a message saying they can't request one
message = TWILIO_CLIENT.account.sms.messages.create(
from: twilio_number, to: number,
body: "Sorry, you cannot claim more than 1 voucher in any 3 month period."
)
end
end
# If the first string is "WORMER". "Wormer" and "wormer" will be converted to uppercase.
elsif body.first.strip.upcase == "WORMER"
# The client exists in the database and has an animal so find the animals that have a healthplan
if @animals.count == 0
# The client has no animals that are on a healthplan
message = TWILIO_CLIENT.account.sms.messages.create(
from: twilio_number, to: number,
body: "We've noticed that none of your animals are currently on our Healthplan. If you think this is incorrect, please contact us."
)
elsif @animals.count > 1
# Creates the hash using a method on the client model
hash = @client.animals_hash(@animals)
# Returns the hash as "1 for Max, 2 for Rio, 3 for Someone"
names_joined = hash.map{ |k, v| "#{k} for #{v.AnimalName}" }.join(", ")
# If a Client has more than 1 animal, send a message to them with a request to include the number of the pet
message = TWILIO_CLIENT.account.sms.messages.create(
from: twilio_number, to: number,
body: "We've noticed that you have more than one pet on a healthplan. Please reply with WORMER followed by #{names_joined}"
)
elsif @animals.count == 1
# If not, use the first animal, create a voucher and sent it to the client
@animal = @animals.first
send_voucher(twilio_number, number)
end
end
end
render text: "Success"
end
private
# This sends the voucher
def send_voucher(twilio_number, number)
@voucher = Voucher.create(client_id: @client.ClientKey, animal_id: @animal.id)
# Send an SMS with the voucher code
message = TWILIO_CLIENT.account.sms.messages.create(
from: twilio_number, to: number,
body: "Your voucher for #{@animal.AnimalName}'s flea/wormer is #{@voucher.code}. Please present this code at any of our branches."
)
# # Send an SMS saying about the animals weight
# message = TWILIO_CLIENT.account.sms.messages.create(
# from: twilio_number, to: number,
# body: "The last recorded weight of #{@animal.AnimalName} is #{@animal.Weight}kgs. If you think this is wrong then please let us know #{@animal.Sex == 'Male' ? 'his' : 'her'} weight."
# )
end
# Renders nothing if the From param isn't found
def check_from
render nothing: true if !params["From"]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment