biharimunda

linkedin_ques

Jul 11th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.41 KB | None | 0 0
  1. require 'open-uri'
  2. require 'nokogiri'
  3.  
  4.  
  5. def is_phone(num)
  6.     return false if num.length < 1
  7.     bool_num = /(?:\s+|)((0|(?:(\+|)91))(?:\s|-)*(?:(?:\d(?:\s|-)*\d{9})|(?:\d{2}(?:\s|-)*\d{8})|(?:\d{3}(?:\s|-)*\d{7}))|\d{10})(?:\s+|)/.match(num)
  8.     if bool_num != nil
  9.         return true
  10.     else
  11.         return false
  12.     end
  13. end
  14.  
  15. def is_email?(str)
  16.     # To check if a given string is an email address
  17.     return false if str.length < 1
  18.     bool = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/.match(str)
  19.     if bool != nil
  20.         return true
  21.     else
  22.         return false
  23.     end
  24. end
  25. parsed_page = Nokogiri::HTML(open("http://www.airindia.in/contact-details.htm")).text
  26. parsed_page =  parsed_page.split(' ')
  27.  
  28. #################### To store all numbers emails ######################
  29. all_emails = []
  30. all_numbers = []
  31. ############################################################
  32.  
  33.  
  34. parsed_page.each do |pa|
  35.     if is_email?(pa)
  36.         all_emails << pa
  37.    
  38.     elsif is_phone(pa)
  39.         all_numbers << pa
  40.     end
  41. end
  42.  
  43. puts all_emails
  44. puts all_numbers
  45.  
  46. ''' Returned Result
  47. call.del@airindia.in
  48. call.bom@airindia.in
  49. traveldocs@airindia.in
  50. eCommerce@airindia.in
  51. flying-returns.ai@iclployalty.com
  52. airindiaretros.ai@iclployalty.com
  53. starallianceretros.ai@iclployalty.com
  54. silveredge.ai@iclployalty.com
  55. goldenedge.ai@iclployalty.com
  56. maharajahclub.ai@iclployalty.com
  57. +91-22-25818515
  58. +91-44-66921455.
  59. 011-24624074
  60. 011-24624075
  61. '''
Add Comment
Please, Sign In to add comment