Skip to content

Instantly share code, notes, and snippets.

View streetcarmonkey's full-sized avatar

Ray streetcarmonkey

  • Melbourne, Australia
View GitHub Profile
@streetcarmonkey
streetcarmonkey / get_element_class_name.rb
Created May 30, 2015 08:17
Ruby Nokogiri: Get class attribute of a TR element (or any other node for that matter)
require 'nokogiri'
html = %q{
<table>
<tr class="even_class">
<td></td>
</tr>
<tr class="odd_class">
<td></td>
</tr>
@streetcarmonkey
streetcarmonkey / test.rb
Last active August 29, 2015 14:22
Ruby Nokogiri: Return a parent attribute value based on a sub element attribute match.
xml = "
<levels>
<lvl attr = 'x'>
<lvl2>
<lvl3 n = 'unique_value 1A'>'</lvl3>
</lvl2>
</lvl1>
<lvl attr = 'y' another_attr = 'z'>
<lvl2>
@streetcarmonkey
streetcarmonkey / test.rb
Last active August 29, 2015 14:22
Ruby Nokogiri: returning node sets by sub elements or nested text strings
xml = '<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id = "1">
<title lang="en">Harry Potter</title>
<price>29.99</price>
Harry Potter is great!
</book>
<book id = "2">
<title lang="en">Learning XML</title>
@streetcarmonkey
streetcarmonkey / nested_hash_sort.rb
Created April 27, 2015 00:23
How to sort a hash by nested hash values in Ruby
# Courtesy of http://www.rubyinside.com/how-to/ruby-sort-hash
# I added the reverse (descending) sort part at the bottom.
# Sample Data
people = {
:fred => { :name => "Fred", :age => 23 },
:joan => { :name => "Joan", :age => 18 },
:pete => { :name => "Pete", :age => 54 }
}
@streetcarmonkey
streetcarmonkey / all_nodes.rb
Last active August 29, 2015 14:12
How to get all nodes or elements in a html or xml document using Ruby, Nokogiri and xpath.
require 'rubygems'
require 'nokogiri'
xml = <<EOF
<Guide>
<Master>
<Part>12345</Part>
<Sub>
<Name>A</Name>
</Sub>
@streetcarmonkey
streetcarmonkey / simple_whois_email_extractor.rb
Created December 24, 2014 05:03
Simple Ruby whois email parser, no gems used.
RE = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/
def simple_whois_email_extractor(domain = 'DISNEY.COM')
puts "Getting whois info for #{domain.downcase}"
emails = Array.new
IO.popen("whois #{domain.downcase}") do |io|
string = ''
while (line = io.gets) do
string += line
end
@streetcarmonkey
streetcarmonkey / check_whois_for_emails_and_domain_expiry.rb
Last active August 29, 2015 14:12
Ruby script using whois gem to check whois for emails and domain expiry details
require 'whois'
def check_whois_for_emails_and_domain_expiry(domain = 'microsoft.com')
puts "Getting whois info for #{domain}"
emails = Hash.new
registration_details = Hash.new
r = Whois.whois(domain)
if r.technical_contact.nil? then emails[:technical_contact] = '' else emails[:technical_contact] = r.technical_contact['email'].downcase end
@streetcarmonkey
streetcarmonkey / email_extract.rb
Last active August 29, 2015 14:12
Ruby script to extract email addresses from a string using a regular expression.
@string = '<p><a href="http://plop.com">plop</a><br /><a mailto:ray@plop.com>ray@plop.com</a><br />cathy@plop.com.au</p>'
RE = /[\w.!#\$%+-]+@[\w-]+(?:\.[\w-]+)+/
def email_extract(doc = @string)
puts "Attempting to extract emails from a string"
emails = Array.new
doc.downcase.scan(RE).each do | email | emails.push email end
emails.uniq!
return emails
end
emails = email_extract #'<p><a href="http://notplop.com">not plop</a><br /><a mailto:raymond@notplop.com>ray@notplop.com</a><br />cathy@notplop.com.au</p>'