Skip to content

Instantly share code, notes, and snippets.

@TimSmith714
Last active May 25, 2016 13:20
Show Gist options
  • Save TimSmith714/324ac2ed6bcfa6acd4aa15208f539a9c to your computer and use it in GitHub Desktop.
Save TimSmith714/324ac2ed6bcfa6acd4aa15208f539a9c to your computer and use it in GitHub Desktop.
Get the status of some lines and email
require 'nokogiri'
require 'date'
require 'open-uri'
require 'mail'
options = { :address => 'STMPSERVER',
:port => 'PORT',
:user_name => 'USERNAME',
:password => 'PASSWORD',
:authentication => 'plain',
:enable_starttls_auto => true
}
lineCodes = ['.bm', '.ge','.re','.me']
goodLines = []
problemLines = []
subjectLine = ''
body = ''
#Get the Southern Homepage
page = Nokogiri::HTML(open('http://www.southernrailway.com'))
# Build the lists of good and problem lines
lineCodes.each do | line |
if page.css(line + ' + span').text == ' Good service '
goodLines.push(line)
else
problemLines.push(line)
end
end
# Build Subject Line
if problemLines.length == 0
subjectLine = "All Southern Lines running fine"
else
subjectLine = "Train problems on: "
problemLines.each do |problem|
if problem == problemLines.last
subjectLine.concat(page.css(problem).text.to_s)
else
subjectLine.concat(page.css(problem).text.to_s)
subjectLine.concat(', ')
end
end
end
#puts subjectLine
## HTML BODY ##
htmlbody = "<html><body>"
if problemLines.length > 0
htmlbody.concat("<h3>There are problems on the following lines:</h3><ul>")
problemLines.each do |problem|
htmlbody.concat("<li>")
htmlbody.concat(page.css(problem).text)
htmlbody.concat(": ")
htmlbody.concat(page.css(problem + " + span").text)
htmlbody.concat("</li>")
end
htmlbody.concat("</ul>")
end
if goodLines.length > 0
htmlbody.concat("<h3>There is a good service on the following lines:</h3><ul>")
goodLines.each do |good|
htmlbody.concat("<li>")
htmlbody.concat(page.css(good).text)
htmlbody.concat("</li>")
end
htmlbody.concat("</ul>")
end
htmlbody.concat("<p>More information on the <a href='http://www.southernrailway.com'>Southern Railway website</a></p></body></html>")
#puts htmlbody
Mail.defaults do
delivery_method :smtp, options
end
Mail.deliver do
to 'RECIPIENTADDRESS'
from 'SENDERADDRESS'
subject subjectLine
html_part do
content_type 'text/html'
body htmlbody
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment