Ruby/Rails Parsing Emails -
i'm using following parse emails:
def parse_emails(emails) valid_emails, invalid_emails = [], [] unless emails.nil? emails.split(/, ?/).each |full_email| unless full_email.blank? if full_email.index(/\<.+\>/) email = full_email.match(/\<.*\>/)[0].gsub(/[\<\>]/, "").strip else email = full_email.strip end email = email.delete("<").delete(">") email_address = emailveracity::address.new(email) if email_address.valid? valid_emails << email else invalid_emails << email end end end end return valid_emails, invalid_emails end
the problem i'm having given email like:
bob smith <bob@smith.com>
the code above delete bob smith , returning bob@smith.
but want hash of fname, lname, email. fname , lname optional email not.
what type of ruby object use , how create such record in code above?
thanks
i've coded work if have entry like: john bob smith doe <bob@smith.com>
it retrieve:
{:email => "bob@smith.com", :fname => "john", :lname => "bob smith doe" }
def parse_emails(emails) valid_emails, invalid_emails = [], [] unless emails.nil? emails.split(/, ?/).each |full_email| unless full_email.blank? if index = full_email.index(/\<.+\>/) email = full_email.match(/\<.*\>/)[0].gsub(/[\<\>]/, "").strip name = full_email[0..index-1].split(" ") fname = name.first lname = name[1..name.size] * " " else email = full_email.strip #your choice, string be... mail, name? end email = email.delete("<").delete(">") email_address = emailveracity::address.new(email) if email_address.valid? valid_emails << { :email => email, :lname => lname, :fname => fname} else invalid_emails << { :email => email, :lname => lname, :fname => fname} end end end end return valid_emails, invalid_emails end
Comments
Post a Comment