Class: Snackhack2::PhoneNumber
- Inherits:
-
Object
- Object
- Snackhack2::PhoneNumber
- Defined in:
- lib/snackhack2/phone_number.rb
Instance Attribute Summary collapse
-
#save_file ⇒ Object
Returns the value of attribute save_file.
-
#site ⇒ Object
Returns the value of attribute site.
Instance Method Summary collapse
-
#initialize(save_file: true) ⇒ PhoneNumber
constructor
A new instance of PhoneNumber.
- #run ⇒ Object
- #spider ⇒ Object
Constructor Details
#initialize(save_file: true) ⇒ PhoneNumber
Returns a new instance of PhoneNumber.
9 10 11 12 |
# File 'lib/snackhack2/phone_number.rb', line 9 def initialize(save_file: true) @site = site @save_file = save_file end |
Instance Attribute Details
#save_file ⇒ Object
Returns the value of attribute save_file.
7 8 9 |
# File 'lib/snackhack2/phone_number.rb', line 7 def save_file @save_file end |
#site ⇒ Object
Returns the value of attribute site.
7 8 9 |
# File 'lib/snackhack2/phone_number.rb', line 7 def site @site end |
Instance Method Details
#run ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/snackhack2/phone_number.rb', line 16 def run found_numbers = [] unless @site.include?("https://") @site = "https://#{@site}" else @site = site end http = Snackhack2.get(@site).body case http when (/((\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4})/) ph_1 = http.scan(/((\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4})/).flatten found_numbers << ph_1 when /^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/ ph_2 = http.scan(/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/).flatten found_numbers << ph_2 when (/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/) ph_3 = http.scan(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/).flatten found_numbers << ph_3 when (/^\+((?:9[679]|8[035789]|6[789]|5[90]|42|3[578]|2[1-689])|9[0-58]|8[1246]|6[0-6]|5[1-8]|4[013-9]|3[0-469]|2[70]|7|1)(?:\W*\d){0,13}\d$/).flatten ph_4 = http.scan((/^\+((?:9[679]|8[035789]|6[789]|5[90]|42|3[578]|2[1-689])|9[0-58]|8[1246]|6[0-6]|5[1-8]|4[013-9]|3[0-469]|2[70]|7|1)(?:\W*\d){0,13}\d$/)) found_numbers << ph_4 when (/^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/) ph_5 = http.scan(/^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/) found_numbers << ph_5 when (/^\+?[1-9][0-9]{7,14}$/) ph_6 = http.scan(/^\+?[1-9][0-9]{7,14}$/) found_numbers << ph_6 when (/^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/) ph_7 = http.scan(/^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/) found_numbers << ph_7 end numbers = found_numbers.flatten.compact if @save_file Snackhack2.file_save(@site, 'phone_numbers', numbers.join("\n")) else return numbers end end |
#spider ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/snackhack2/phone_number.rb', line 54 def spider found_numbers = [] Spidr.start_at(@site, max_depth: 4) do |agent| agent.every_page do |page| body = page.to_s # regex to extract phone numbers case body when (/((\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4})/) ph_1 = http.scan(/((\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4})/).flatten found_numbers << ph_1 when /^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/ ph_2 = http.scan(/^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/).flatten found_numbers << ph_2 when (/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/) ph_3 = http.scan(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/).flatten found_numbers << ph_3 when (/^\+((?:9[679]|8[035789]|6[789]|5[90]|42|3[578]|2[1-689])|9[0-58]|8[1246]|6[0-6]|5[1-8]|4[013-9]|3[0-469]|2[70]|7|1)(?:\W*\d){0,13}\d$/).flatten ph_4 = http.scan((/^\+((?:9[679]|8[035789]|6[789]|5[90]|42|3[578]|2[1-689])|9[0-58]|8[1246]|6[0-6]|5[1-8]|4[013-9]|3[0-469]|2[70]|7|1)(?:\W*\d){0,13}\d$/)) found_numbers << ph_4 when (/^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/) ph_5 = http.scan(/^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$/) found_numbers << ph_5 when (/^\+?[1-9][0-9]{7,14}$/) ph_6 = http.scan(/^\+?[1-9][0-9]{7,14}$/) found_numbers << ph_6 when (/^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/) ph_7 = http.scan(/^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/) found_numbers << ph_7 end end end unless phone_numbers.empty? if @save_file Snackhack2.file_save(@site, 'phonenumbers', phone_numbers.join("\n")) else return phone_numbers end end end |