Skip to content

Instantly share code, notes, and snippets.

@bkenny
Created May 14, 2012 14:48
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 bkenny/2694397 to your computer and use it in GitHub Desktop.
Save bkenny/2694397 to your computer and use it in GitHub Desktop.
Import MiniCorp Blog into Mongo DB
require 'rubygems'
require 'mongo'
require 'net/http'
require 'active_support/core_ext'
require 'crack'
require 'uri'
require 'hashie'
def convert(feed)
db = Mongo::Connection.new.db("blogs")
coll = db.collection("minicorp")
url = Net::HTTP.get_response(URI.parse(feed)).body
converted_to_json = Crack::XML.parse(url)
@hashie = Hashie::Mash.new(converted_to_json)
blog_title = Hash["blog_title", @hashie.rss.channel.title]
blog_description = Hash["blog_title", @hashie.rss.channel.description]
link = Hash["blog_title", @hashie.rss.channel.link]
coll.insert(ActiveSupport::JSON.decode(blog_title.to_json))
coll.insert(ActiveSupport::JSON.decode(blog_description.to_json))
coll.insert(ActiveSupport::JSON.decode(link.to_json))
@hashie.rss.channel.item.each do |post|
post_hash = Hash[[ ["post_title", post["title"]], ["post_content", post["content:encoded"]],
["post_link", post["link"]], ["post_category", post["category"]], ["post_published", post["pubDate"]]]]
coll.insert(ActiveSupport::JSON.decode(post_hash.to_json))
end
end
convert("http://minicorp.ie/blog/feed/")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment