Using ruby to delete mail in the Postfix mail queue

From time to time I find myself having to manually remove certain queued emails from the Postfix ‘mailq’, which sometimes can be hassle. For that matter I created a simple ruby script that lets me pass a regular expression argument for the emails to be matched against and therefore removing the matched emails from the Postfix mailq.


#!/usr/local/bin/ruby
#
# postdel.rb
# - Deletes emails queued in the Postfix mail queue
#

postqueue_bin = '/usr/sbin/postqueue'
postsuper_bin = '/usr/sbin/postsuper'

[postqueue_bin, postsuper_bin].each do |file|
  unless File.executable?(file)
    puts "The file #{file} doesn't exist or can't be executed." 
    exit
  end
end

unless regex = ARGV[0]
  puts "No email address provided (regex style, i.e. b\\(.*\\)@domain\\(.*\\)\\(com|net\\))" 
  exit
end
Read more...

Processing uploads sent through a REST web service

There was an interesting question posted on the Rails mailing list today about “image upload through a REST web service”. After replying to the post I decided to write up a small tutorial on how this can be accomplished.

Lets start our thin/mongrel/webrick webserver:


thin start

Next create the uploads_controller:


ruby script/generate controller uploads

Now we edit the uploads_controller to read the content sent and store it to a file:


class UploadsController < ApplicationController 
  def create 
    # You can also use request.raw_post below where it says request.stdinput
    File.open("assets/photo.jpg", "wb") {|f| f << request.stdinput.read}
    render :text => "Upload sent\n" 
  end 
end

Now lets use CURL to send an image to the uploads_controller to simulate a partner posting data using a REST web service:


# Sending data via CURL 
cat photo.jpg | curl -X POST -H 'Content-type: image/jpeg' --data-binary @- "http://localhost:3000/uploads/create" 

And that’s it, you’re now ready to accept file uploads from a REST web service.

acts_as_solr is now on GitHub

The acts_as_solr plugin has moved from Subversion to Git to allow a faster and easy way of collaborating.

If you haven’t already done so, check out acts_as_solr on GitHub at http://github.com/railsfreaks/acts_as_solr

Introducing the "uses_connection" Rails plugin

This is a fairly simple but handy plugin that lets you specify which database connection to use on a per-model basis. It is useful when you have multiple applications sharing the same data.

Before uses_connection
The scenario below shows three separate applications having a common table among them. Lets say the ‘zip_codes’ table need to be updated every month with inserts and deletes, before installing the uses_connection plugin you will have to update the 3 tables separately, unless you have mysql round-robin enabled or another method for synchronizing the data (which most of the people don’t).


After installng uses_connection
The problem mentioned above is solved when using this plugin as you can see on the graph below. It allows you to connect all of the applications to a single database, therefore when updates are needed you only have to insert/delete data from one table instead of three.

Read more...

Capistrano recipe for compressing css and js files when you deploy

Alright so you may be asking yourself “Why do I want to compress my css and js files?”. The main answer to that would be: To speed-up the loading time and page size of your website.

In this tutorial we’ll be using Yahoo’s YUI Compressor for the compression of .css and .js files. Keep in mind that you’ll need to have Java installed to run the compressor.

Step 1: Getting the compressor and adding it to your project
- Get the latest version of Yahoo’s YUI Compressor here
- Extract the contents to a temporary directory, we’ll use ”/tmp” in this example
- Move the file ”/tmp/yuicompressor-x.x.x/build/yuicompressor-x.x.x.jar” to your “RAILS_ROOT/lib”


# I'm using version 2.3.5 in this example
cd my_rails_project
mv /tmp/yuicompressor-2.3.5/build/yuicompressor-2.3.5.jar lib/yui-compressor.jar
Read more...

Extending core classes in Ruby

A few days ago a friend of mine had a question about how to extend Ruby core classes and put them to work inside a Rails project he was working on. Well, turns out for his happiness it is a piece of cake.

Say you wanted to have a simple way of converting ‘true’ and ‘false’ values into a more humanized way of reading it, for example ‘yes’ and ‘no’.

First create a file “core_extensions.rb” inside your RAILS_ROOT/lib directory and lets start by extending the TrueClass, which is the parent class of the ‘true’ boolean value :


class TrueClass
  def to_human
    "Yes" 
  end
end

Next we’ll extend the FalseClass, which is the parent class of the ‘false’ boolean value:


class FalseClass
  def to_human
    "No" 
  end
end

Next open your config/environments.rb and include:


require 'core_extensions'

and that’s pretty much it. Now to display this more humanized value simply do:


# Simple example to return "Yes" instead of 'true'
(1 == 1).to_human
=> "Yes" 

# Simple example to return "No" instead of 'false'
(2 == 1).to_human
=> "No"