Skip to content

Instantly share code, notes, and snippets.

@kevgathuku
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevgathuku/702327991d67c1f3c4e4 to your computer and use it in GitHub Desktop.
Save kevgathuku/702327991d67c1f3c4e4 to your computer and use it in GitHub Desktop.
A simple Ruby Script that sends emails of the UK Top 40 singles charts
#!/usr/bin/env ruby
require "http"
require 'json'
require 'mail'
class Charts
attr_accessor :final
def initialize(number=10)
@singles = HTTP.get("http://ben-major.co.uk/labs/top40/api/singles/").to_s
@number = number
@final = parse_singles
end
#singles = HTTP.get("http://ben-major.co.uk/labs/top40/api/singles/").to_s
#singles = File.read('singles.json')
def parse_singles
output = ""
sjson = JSON.parse(@singles)
if @number > 40
@number = 40
end
sjson['entries'].each do |title|
#Get the number of specified songs in the Chart
unless title['position'].to_i > @number
output += "#{title['position']}. #{title['title']} - #{title['artist']}\n"
end
end
return output
end
end
class Messenger
attr_reader :content
def initialize(charts)
@content = charts.final.to_s
Mail.defaults do
delivery_method :smtp, {
:port => 587,
:address => "smtp.mandrillapp.com",
:user_name => ENV['MANDRILL_USERNAME'],
:password => ENV['MANDRILL_PASSWORD']
}
end
end
def send_plain_email
mail = Mail.new do
from 'kev@kevgathuku.com'
to 'kevgathuku@gmail.com'
subject 'Top 40 Charts Test email'
end
header = "Here is your Top 40 Chart for the week \n"
mail.body = header + @content
return mail
end
end
if __FILE__ == $0
top = Charts.new
#puts top.final.to_s
mailer = Messenger.new(top)
#puts mailer.send_plain_email.to_s
mailer.send_plain_email.deliver
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment