0

I am trying to save a rake task into my database to I can display the content in my view.

Here's the task:

desc "Fetch Digital Trends"
task :fetch_trends => :environment do
require 'nokogiri'
  require 'httparty'
  url = "http://www.digitaltrends.com/computing/"
    doc = Nokogiri::HTML(open(url))
    doc.css(".post").each do |item|
    title = item.at_css(".content h3").text
    link = item.at_css(".content h3 a")[:href]
    puts "#{title} - #{link}"
  end
end

It is possible to make this task run every hour to update the content?

Dan Mitchell
  • 844
  • 2
  • 15
  • 34

2 Answers2

2

You can use cron. Look at https://github.com/javan/whenever (Cron jobs in Ruby)

Alexander Kobelev
  • 1,379
  • 8
  • 14
0

You can do it in two ways.

Method 1:

  1. Read from database.

  2. Write to a file in lib/tasks/name.rake

  3. load the file

  4. Execute the rake task

Method 2:

  1. Load the string from the database

  2. Use eval

usha
  • 28,973
  • 5
  • 72
  • 93
  • Sorry Vimsha, I do not understand what you mean. I have the above task that just needs to save into a table within my database every time the task is run. – Dan Mitchell Jun 11 '13 at 00:18