Register now and start sharing your code snippets.
-->

Gmail checker plugin for wmii.

Ruby posted 2 months ago by marko

I take no credit for this. The original URL is http://dmy999.com/src/gmail-check.rb. The reason it is here is that I don’t want to lose it just in case the original site is taken down.

   1  #
   2  # gmail check plug in
   3  # by Derek Young
   4  #
   5  # periodically read gmail's atom feed for new messages.
   6  # if one is found, display the username and subject of
   7  # the message.
   8  #
   9  # The applet wakes up after a configurable period to check (by default
  10  # 3 minutes).  You can also cause it to check immediately by pressing
  11  # the key binding for the gmail-check binding (MODKEY-g by default).
  12  #
  13  # place this file in ~/.wmii-3/plugins and
  14  # add the following to your wmiirc-config.rb file to enable:
  15  # 
  16  #  plugin_config["dmy999@gmail.com:gmail"]["username"] = 'my user name'
  17  #  plugin_config["dmy999@gmail.com:gmail"]["password"] = 'my password'
  18  #  plugin_config["dmy999@gmail.com:gmail"]["interval"] = 3
  19  #  use_bar_applet "dmy999@gmail.com:gmail", 50
  20  #  use_binding "dmy999@gmail.com:gmail-check"
  21  #
  22  # atom parsing based on code from Evan Martin
  23  #  http://neugierig.org/software/misc/gmail-notifier
  24  #
  25  # Copyright Derek Young, 2007
  26  # Use as you wish but please give credit.
  27  #
  28  
  29  Plugin.define "dmy999@gmail.com" do
  30    author '"Derek Young" <dmy999@gmail.com>'
  31  
  32    def_settings "gmail/interval" do |wmii|
  33      wmii.plugin_config["dmy999@gmail.com:gmail"]["interval"] = 3 * 60
  34    end
  35  
  36    CERTPATH = '/etc/ssl/certs'
  37  
  38    # return title, email, author name of first new message
  39    def gmail_check(username, password)
  40      req = Net::HTTP::Get.new '/mail/feed/atom'
  41      req.basic_auth(username, password)
  42  
  43      http = Net::HTTP.new('mail.google.com', 443)
  44      http.use_ssl = true
  45      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  46      http.ca_path = CERTPATH
  47  
  48      res = http.request req
  49  
  50      doc = REXML::Document.new res.body
  51      entries = doc.root.get_elements('/feed/entry')
  52      return nil if entries.length == 0
  53      title = entries[0].elements['title'].text
  54      email = entries[0].elements['author/email'].text
  55      name = entries[0].elements['author/name'].text
  56      [ title, email, name ]
  57    end
  58  
  59    bar_applet("gmail", 100) do |wmii, bar|
  60      require 'net/https'
  61      require 'rexml/document'
  62  
  63      interval = wmii.plugin_config["dmy999@gmail.com:gmail"]["interval"]
  64      username = wmii.plugin_config["dmy999@gmail.com:gmail"]["username"]
  65      password = wmii.plugin_config["dmy999@gmail.com:gmail"]["password"]
  66  
  67      @gmail_check_thread = Thread.new do
  68        loop do
  69          bar.data = "checking..."
  70          first = gmail_check(username, password)
  71          if first
  72            title, email, name = first
  73            user, domain = email.split('@')
  74            bar.data = "#{user}:#{title}"
  75          else
  76            bar.data = 'no mail'
  77          end
  78          sleep (interval * 60)
  79        end
  80      end
  81    end
  82  
  83    binding("gmail-check", "MODKEY-g") do |wmii,|
  84      LOGGER.info "gmail check requested"
  85      @gmail_check_thread.run if @gmail_check_thread
  86    end
  87  
  88  end
  89  

Tagged wmii, gmail, google mail, email checker

How to test Rails routing with RSpec

Ruby posted 2 months ago by christian

   1  route_for(:controller => 'categories', :action => 'show', :permalink => ['one', 'two', 'three']).should.equal '/one/two/three'
   2  route_for(:controller => 'categories', :action => 'index').should.equal '/'
   3        
   4  params_from(:get, "/").should.equal {:controller => "categories", :action => "index"}
   5  params_from(:get, "/something/").should.equal {:controller => "categories", :action => "show", :permalink => ["something"]}

The test_spec_on_rails plugin also allow you to test routing and much more, see the docs for more information.

Tagged testing, rspec, routing, routes

How to fix "Only get, head, post, put, and delete requests are allowed."

Ruby posted 2 months ago by christian

I’m getting this once in a while in development mode after changing the routes configuration:

   1  ActionController::MethodNotAllowed
   2  
   3  Only get, head, post, put, and delete requests are allowed.

The solution for me has been to restart the server.

Tagged bug, rails, problem, fix

How to SEO optimize your Rails URLs and routes

Ruby posted 2 months ago by christian

My idea for achieving optimal content crawlability and SEO optimized URLs is to use permalinks instead of ids and the default Rails routes. The permalinks can contain whatever you decide is optimal from a SEO point of view.

As an example, let’s take a recipe site that has a recipe at http://xxx/recipes/asia/china/beijing-duck.html.

First let’s configure the .html extension to be handled by the RecipesController:

   1  map.connect 'recipes/*permalink.html', :controller => 'recipes', :action => 'show'

In the code we use the URI , which is the permalink of the recipe, to retrieve the recipe from the database:

   1  class RecipesController
   2    def show
   3      @product = Recipe.find_by_permalink(request.path)
   4    end
   5  end

To handle the categories and subcategories, we use the following route:

   1  map.connect 'recipes/*permalink/', :controller => 'categories', :action => 'show'

And create the CategoriesController:

   1  class CategoriesController
   2    def show
   3      @category = Category.find_by_permalink(request.path)
   4    end
   5  end

Now what’s left is for you to figure out how to generate the permalinks… I recommend having a look at permalink_fu.

Tagged seo, rails, routes, permalink, crawlability

Allowing URL's in a route.

Ruby posted 3 months ago by marko

I wanted to create a route that could accept an URL as a parameter. The problem was that dots and slashes were interpreted as separators for the route. Luckily I managed to find this post that explained how it could be bypassed.

   1  map.connect ':scale/:text.:format', :controller => 'barcode', :requirements => { :text => /.*/ }

With this route set up I now could catch parameters like this: http://localhost:3000/200×200/http://aktagon.com.png, where 200×200 is :scale, http://aktagon.com is :text and .png is :format.

Tagged rails routes, url in route