目前最常用的方法是利用Unix自身的cron功能来实现定时任务,但是cron的语法比较麻烦(个人感觉),今天为了执行一个定时任务发现了一个很好的RubyGem可以让你用Ruby的方式定义定时任务,这就是Whenever

Whenever可以让你写出类似下面的定时任务:

 set :cron_log, "/path/to/my/cron_log.log"

 every 2.hours do
   command "/usr/bin/some_great_command"
   runner "MyModel.some_method"
   rake "some:great:rake:task"
 end

赶快去Github看看文档和代码吧http://github.com/javan/whenever

There are a number of ways of parsing an RSS feed in Ruby, but one of the best is a gem called Feedzirra. The main advantage of Feedzirra is its speed; it parses feeds very quickly, but it is also useful as it can parse many different types of feed.

To install Feedzirra we first need to make sure that

1
http://gems.github.com

is in our list of gem sources. If not we’ll need to add it.

gem sources -a http://gems.github.com

Now we can install the gem:

sudo gem install pauldix-feedzirra

Notice: When I was installing the gem, there are several libs needed, including libcurl4-gnutls-dev libcurl3-gnutls libxslt1-dev, you may not meet this issue, it’s not a big deal, just install what it rely on. Several dependencies will also be installed alongside the gem. Once everything’s installed we’ll need to add a reference to the gem in our application’s /config/environment.rb file.

config.gem "pauldix-feedzirra", :lib => "feedzirra", :source => "http://gems.github.com"

That’s it. We’re ready to start parsing RSS feeds in our application.

Getting start:
Assume that, you’ve got a model to store the feed content which is fetched by the parser, so the model should be like :

class FeedEntry < ActiveRecord::Base
  def self.update_from_feed(feed_url)
    feed = Feedzirra::Feed.fetch_and_parse(feed_url)
    add_entries(feed.entries)
  end

  private
  def self.add_entries(entries)
    entries.each do |entry|
      unless exists? :guid => entry.id
        create!(
          :name         => entry.title,
          :summary      => entry.summary,
          :url          => entry.url,
          :published_at => entry.published,
          :guid         => entry.id
        )
      end
    end
  end
end

A more detailed manual is here!

I found a cool gem called “cheat”, it’s pretty handy and convenient when you wanna see a brief help/introduction of some “stuff” in command line, cheat out the website for more wiki-versioned cheat sheet.

Get started:
$ sudo gem install cheat
$ cheat strftime

A magnificent cheat sheet for Ruby’s strftime method will be printed to your terminal.

To get some help on cheat itself:
$ cheat cheat

How meta.

Cheat sheets are basically wiki pages accessible from the command line. You can browse, add, or edit cheat sheets. Try to keep them concise. For a style guide, check out the cheat cheat sheet.

To access a cheat sheet, simply pass the program the desired sheet’s name:
$ cheat

Reference : CLICK HERE!

Quote from Gary’s weekly code review post, thanks to Gary Zhang.

all advises on Ruby on Rails code:

* Lesson 1. Move code from Controller to Model
1. Move finder to named_scope
2. Use model association
3. Use scope access
4. Add model virtual attribute
5. Use model callback
6. Replace Complex Creation with Factory Method
7. Move Model Logic into the Model
8. model.collection_model_ids (many-to-many)
9. Nested Model Forms (one-to-one)
10. Nested Model Forms (one-to-many)

* Lesson 2. RESTful Conventions
1. Overuse route customizations
2. Needless deep nesting
3. Not use default route

* Lesson 3. Model
1. Keep Finders on Their Own Model
2. Love named_scope # same as Move finder to named_scope
3. the Law of Demeter
4. DRY: metaprogramming
5. Extract into Module
6. Extract to composed class
7. Use Observer

* Lesson 4. Migration
1. Isolating Seed Data
2. Always add DB index

* Lesson 5. Controller
1. Use before_filter
2. DRY Controller

* Lesson 6. View
1. Move code into controller
2. Move code into model
3. Move code into helper
4. Replace instance variable with local variable
5. Use Form Builder
6. Organize Helper files

I think all these advises are very useful for our farther refactor on our system.

There is a very cool gem to check code according these advises(Sources : Rails Best Practices)

install:
sudo gem install rails_best_practices –source http://gemcutter.org

usage:
in rails rails app, run “rails_best_practices .” (don’t forget the parameter: “.”)

Not try this tool yet, but it looks really handy.

© 2011 Refactoring Thoughts Suffusion theme by Sayontan Sinha
普人特福的博客cnzz&51la for wordpress,cnzz for wordpress,51la for wordpress