Send RSS feed data to email





5
Date Submitted Mon. Oct. 30th, 2006 6:30 AM
Revision 1 of 1
Scripter SCoon
Tags Email | http | Mail | RSS | Ruby
Comments 0 comments
This snippet illustrates following:

1. Using http request to get the data
2. Parsing RSS feeds
3. Sending email
require 'net/http'
require 'net/smtp'
require 'rss/1.0'


class BookmarkInfo

  attr_reader :title, :description

  def initialize(title, description)
    @title = title
    @description = description
  end
end

class Bookmark

  attr_reader :link, :infos

  def initialize (link)
    @link = link
    @infos = []
  end

  def add(info)
    @infos.push info
  end
 
end

class BookmarkReader

  def initialize(user)
    @user = user
    @bookmarkByLink = {}
  end

  def proxy(host,port,user,password)
    @proxyHost = host
    @proxyPort = port
    @proxyUser = user
    @proxyPassword = password
  end

  def read
    Net::HTTP::Proxy(
      @proxyHost, @proxyPort, @proxyUser, @proxyPassword
    ).start('del.icio.us', 80) { |http|
      response = http.get("/rss/subscriptions/#{@user}")
      rss = RSS::Parser.parse(response.body, true)
      rss.items.each { |item|
        bookmark = @bookmarkByLink[item.link]
        if bookmark.nil?
          bookmark = Bookmark.new(item.link)
          @bookmarkByLink[item.link] = bookmark
        end
        bookmark.add(BookmarkInfo.new(item.title, item.description))
      }
    }
  end

  def export
    temp = ''
    @bookmarkByLink.each { |link, bookmark|
      temp += bookmark.link + "\n"
      bookmark.infos.each { |info|
        temp += "  #{info.title}\n"
        temp += "    #{info.description}" if info.description
      }
    }
    temp
  end
 
end

class MailSender

  def initialize(host,port)
    @host = host
    @port = port
  end

  def send(from,to,subject,text)
    msgstr = <<END_OF_MESSAGE
From: #{from}
To: #{to}
Subject: #{subject}
Message-Id: #{from}#{rand}

#{text}
END_OF_MESSAGE
  Net::SMTP.start(@host, @port) do |smtp|
    smtp.send_message msgstr,
    from,
    to
  end
   
  end
end

reader = BookmarkReader.new('scoon')
reader.proxy('your-proxy-host', 3128, 'proxy-login', 'pwd')
reader.read
summary = reader.export

sender = MailSender.new('your-smtp-host', 25)
sender.send('john.doe@....', 'john.doe@....', 'Links', summary)

Vladislav Zlobin

Comments

There are currently no comments for this snippet.

Voting

Votes Down