Snippets

Fri Apr 18
Wed Mar 12
Tue Feb 12

List all undeliverable messages in your exim queue

sudo script/exipick -bpru -show-vars=recipients | grep recipients | cut -d “’” -f 2 | sort | uniq

Suppress Ruby warnings at runtime, without interpreter flags

def without_warnings
  ov = $VERBOSE
  $VERBOSE = nil
  ret = yield
  $VERBOSE = ov
  ret
end

Sat Sep 29

Rendering from script/console

It turns out Rails has a nice little undocumented trick for rendering things from script/console or script/runner:

 app.get(“/path/to/render”)

 my_response_body = app.response.body

 If you want to render something you don’t have an action for, just define it on the controller right there:

class MyController

  def  my_render

     render :partial => “shared/foo”

  end

end

app.get(“/my/my_render”)

Wed Sep 26
Fri Sep 14

To install the postgres gem on debian

sudo apt-get install ruby1.8-dev

sudo apt-get install postgresql-dev libpq-dev

cd /var/lib/gems/1.8/gems/postgres-0.7.1

sudo ruby extconf.rb —with-pgsql-lib-dir=/usr/lib/postgresql/8.1/lib/ —with-pgsql-include-dir=/usr/include/postgresql/

sudo make

sudo make install

Thu Jul 19

Ruby on Rails HTTP based authorization: 

  def get_auth_data
    user, pass, authdata = ”, ”, nil
    # mod rewrite, normal, apache
    [‘X-HTTP_AUTHORIZATION’, ‘HTTP_AUTHORIZATION’, ‘Authorization’].each do |key|
      # extract authorisation credentials
      if request.env.has_key? key
        authdata = @request.env[key].to_s.split
      end
    end
    
    # at the moment we only support basic authentication
    if authdata and authdata[0] == ‘Basic’
      user, pass = Base64.decode64(authdata[1]).split(‘:’)[0..1]
    end
    return [user, pass]
  end

  def admin_required(realm=’Admin Password’, errormessage=”Couldn’t authenticate you”)
    username, passwd = get_auth_data
    user = User.authenticate(username, passwd)
    if user and user.admin?
      @user = user
    else
      # bad user/pass, or not authorized
      @response.headers[“Status”] = “Unauthorized”
      @response.headers[“WWW-Authenticate”] = “Basic realm="#{realm}"”
      render :text => errormessage, :status => 401
      return false
    end
  end

Tue Jul 3
Sat Jun 23

Getting NetConnection.Connect.Failed every time you try to connect from a NetConnection?

Make sure you can run telnet serveraddress 1935 first, so that you know that the server is actually listening.

Make sure you have the security settings to allow the NetConnection to work. Run nc -l -p 1935 to listen on 1935 and try connecting to localhost. If you see a bunch of binary crap, it’s working.

If you’re using ActionScript 3 (AS3) and you’re using Wowza or an older version of FMS, the issue could be that you have the wrong objectEncoding set.

You can change the objectEncoding to AMF0 and that might fix it.