-3

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.

Abhinay
  • 1,796
  • 4
  • 28
  • 52
  • 1
    `$ ruby any.rb > any.html` isn't that an automation? – Малъ Скрылевъ Jan 02 '14 at 13:28
  • @majioa I didn't mean that,I have mentioned it like I don't want to write it manually , Is there any method using which I don't have to write it? – Abhinay Jan 02 '14 at 13:35
  • what exactly you don't wish to write? – Малъ Скрылевъ Jan 02 '14 at 13:42
  • and this very rude here, If someone wants to learn new things then what is problem in here to ask for some help from those who know this better..please If you can't answer any question then please don't down-vote the question just because you think its not right.. – Abhinay Jan 02 '14 at 13:44
  • @majioa I don't want to write the command , I have clearly mentioned it, Is is possible or not? – Abhinay Jan 02 '14 at 13:45
  • ok, you don't want to write a command, but by which matter do you want PC know what you want to do? I guess you shell to talk/do something in order to pc grab that page. – Малъ Скрылевъ Jan 02 '14 at 13:48
  • 1
    @majioa No, actually I want to execute it but only when and whenever I click on a particular link on my web-app..So, here is what I am trying to do, I am working on a web-app where I will provide an option to the Users by using which they can see a particular data from another web-sites and for this I can not run this command "ruby any.rb > any.html" on Bash everytime...So, i hope now you have understood it clearly – Abhinay Jan 02 '14 at 13:54
  • ok, which type of web app you wish to make? it is rails/sinatra app or what? – Малъ Скрылевъ Jan 02 '14 at 13:56
  • @majioa Yes, Its a Rails app..Will you help in achieving this Goal.I really want to do this.and also Is it possible to automate command?. – Abhinay Jan 02 '14 at 13:59
  • Agreed this is a poorly researched question. Programming *IS* automating, there are ways to execute a command from a web-page if that command exists on the *server* using Rails, Sinatra, or even CGI. I'd STRONGLY recommend playing with Sinatra as it will help explain and demonstrate how the browser <--> HTTPD/server <--> underlying-code chain works. – the Tin Man Jan 02 '14 at 17:02
  • 1
    @the Tin Man Thanks for Improving my question.I know i haven't researched a lot before posting this question and I'll remember this before posting any question next time. :) – Abhinay Jan 02 '14 at 17:12

1 Answers1

1

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
Малъ Скрылевъ
  • 16,187
  • 5
  • 56
  • 69
  • Can you explain a little bit more..as I am not an expert in this...thanks a lot for this initiative :) – Abhinay Jan 02 '14 at 14:13
  • @AKS i've updated the answer, please refer to rails docs on how to make a controller's code further. – Малъ Скрылевъ Jan 02 '14 at 14:21
  • I want to know one more thing, Is "open-uri" will automatically grab the content of the link and will convert it into string format which i am referring to just by mentioning the module name ? and another thing is Thanks soo much..I will come back to ask you few more doubts if occurs.First I will learn the whole procedure and implement what you have said and posted..thank you :) and also Happy new Year :) – Abhinay Jan 02 '14 at 14:45
  • not automatically, but on-call. when you call `open uri`, it opens remote link. you too happy new year.. =) – Малъ Скрылевъ Jan 02 '14 at 15:00