Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top