Hpricot scraping in ruby

Include gems/library required before getting startedrequire 'hpricot'require 'net/http'require 'rio'# Pass website url to be scrapedurl = "www.funonrails.com"# Define filename to store file locallyfile = "temp.html"# Save page locallyrio(url) < rio (file)# Open page through hpricotdoc = Hpricot(open(file))Apply hpricot library to get right contentsdoc.at("div.pageTitle")doc/"div.pageTitle"doc.search("div.entry")doc//"div.pageTitle"Hpricot API Reference click here

Passing commandline parameter (arguments) to ruby file using optparser

Ruby file accepts from command prompt in the form of array.Passing parametersruby input.rb TOI DH TimesNewAccessing parameters # input.rb p ARGV # => ["TOI", "DH", "TimesNew"] p ARGV[0] # => "TOI" p ARGV[1] # => "DH"Optparser : parses commandline options in more effective way using OptParser class.and we can access options as hashed parameters.Passing parametersruby … Continue reading Passing commandline parameter (arguments) to ruby file using optparser

How to get all associted models of rails model

I have Site Model and i wanted to find all associated models of Site model which are having belongs_to relationship.After doing lot headache, here is the solution i foundhas_many models of Site Model Site.reflect_on_all_associations.map{|mac| mac.class_name if mac.macro==:has_many}.compact=> ["Layout", "User", "SubmenuLink", "Snippet", "Asset"]belongs_to model of Site ModelSite.reflect_on_all_associations.map{|mac| mac.class_name if mac.macro==:belongs_to}.compact=> ["User", "Page", "User"]To get all associationsSite.reflect_on_all_associations.map{|mac| … Continue reading How to get all associted models of rails model