My endpoints action takes in a string (originating from an sms).
The string can either consist of:
- A business tag within the string which will identify a business
- An employees badge number (1000-9999) and a score out of 10 within the string
Examples:
- '4353 9'
- '4353 gets a score out of 9'
- '9 4353'
- 'score 9 employee 4353'
- 'bizx thank you'
- 'thank you bizx'
I cannot control user input. I have o have both types of strings accepted due to cost factors.
I have the following code to try extract the badge number score value or the business tag value.
def endpoints
if badge_number = get_badge_number(params[:text])
if score = get_score(params[:text])
//do something
end
elsif business_tag = get_business_tag(params[:text])
// do stuff
end
end
def get_badge_number(msg)
msg.find do |badge_number|
Employee.exists?(badge_number: badge_number.to_i)
end
end
def get_score(msg)
msg.find do |score|
(1..10).include? score.to_i
end
end
def get_business_tag(msg)
msg.find do |tag|
Business.exists?(tag: tag)
end
end
As you can see it's turned in to a bit of a mess.
I really need help to efficiently extract the relevant values.