While learning how to use Nokogiri in Ruby,I got this idea,what if I can automate these commands which I write in bash for Nokogiri? Is there any way or method which I can use to automate the call?
For example: As I was trying to grab some data from one site I wrote:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
PAGE_URL = "http://hackerstreet.in"
page = Nokogiri::HTML(open(PAGE_URL))
links = page.css("a")
puts links.length
puts links[0].text
puts links[0]["href"]
And, to execute it, I have to enter this command at the command line:
$ ruby any.rb > any.html
What shall I do in order to run the same from a Web-App.
If anyone can help on this issue then that would be great.
Insert the code into a library of your rails app as a method. Usually the library is placed in lib/ folder from root of Rails app. Then call the defined method directly from a controller, the call :respond
method to handle the request from a browser, and output result of the method work in a view. If the grab procedure take a lot of tike use asyncronous operations, for example with event-machine gem.
The simplest app is the following:
app/controllers/your_controller.rb
def index
result = WebGrab.grab "http://hackerstreet.in"
render text: result.inspect # just renders text, replace it as a call to render a view
end
lib/webgrab.rb
require 'nokogiri'
require 'open-uri'
module WebGrab
def self.grab uri
page = Nokogiri::HTML( open uri )
links = page.css("a")
[ links.length, puts links[0].text, links[0]["href"] ]
end
end