Displaying articles with tag

How to check if a file exists on a remote server (HEAD request)

Posted by joakimk, Thu May 21 14:43:00 UTC 2009

Here is some code to check if a file exists on a remote server by using a HEAD request. It can optionally check if the content type is what you expected (if you for example want to only allow “audio/mpeg” links). Note that this particular implementation only works with http urls.

require 'net/http'

def exists?(file)
  begin
    # Support for both good and bad URI's
    uri = URI.parse(URI.escape(URI.unescape(file)))
    response = nil
     Net::HTTP.start(uri.host, uri.port) {|http|
       response = http.head(uri.path)
     }
     # .. response.content_type == "audio/mpeg"
     response.code == "200"
  rescue
    false
  end
end

puts exists?('http://djspike.se/files/albums/6/31/DJ_SPIKE%20-%20Tight%20(demo).mp3')

I also found a plugin with similar goals but that actually downloads the entire target file instead of only getting the header (not a good idea when dealing with media files): http://www.igvita.com/2006/09/07/validating-url-in-ruby-on-rails/

0 comments | Filed Under: | Tags:

How to disable Globalize's ActiveRecord logging

Posted by joakimk, Wed May 20 09:56:00 UTC 2009

Here is how you disable the sometimes excessive logging that Globalize does:
# Disables Globalize's ActiveRecord logging
module Globalize
 class DbViewTranslator
   alias_method :orig_fetch_view_translation, :fetch_view_translation   
   def fetch_view_translation(key, language, idx, namespace = nil, original_caller = nil)
     ActiveRecord::Base.silence do
       orig_fetch_view_translation(key, language, idx, namespace, original_caller)
     end
   end
 end
end

This tip is originally from http://www.artweb-design.de/2007/9/16/disable-globalize-viewtranslation-sql-logging, but I had to adapt it a bit to work with my globalize version.

If this does not work with your version of globalize, then just check how it’s implemented in “lib/globalize/localization/db_view_translator.rb” and adapt the patch.

0 comments | Filed Under: | Tags:

Using Ruby EE in development

Posted by joakimk, Tue May 19 08:43:00 UTC 2009

It’s common knowledge that using Ruby Enterprise Edition on your server is a good idea, but how many of us use it on our development boxes?

I recently started using Ruby EE in my development environment and it has decreased the time it takes to run my specs with about 20% as compared to Ruby 1.8.6.

0 comments | Filed Under: | Tags:

Script to show CruiseControl status with lava lamps

Posted by joakimk, Fri Apr 03 18:11:00 UTC 2009


We've been using this script at work to show the build status using lava lamps =).

To control the lamps we use a TellStick which is a transmitter that plugs into USB and lets us control wireless remote switches.

http://gist.github.com/89810:

0 comments | Filed Under: | Tags:

Acts as state machine not saving to the database?

Posted by joakimk, Tue Jan 20 18:18:00 UTC 2009

Just wasted a bit of time figuring out why my tests ran just fine, while trying to enter a state when actually using the app failed. Turns out that because I do not touch the database in my state machine specs I had no chance of discovering that I had written “aasm_event :foo! do” instead of “aasm_event :foo do” (see that extra “!”?)... D’oh. Hope this can be of help to anyone else that accidentally adds an ! to a aasm_event :).

0 comments | Filed Under: | Tags:

Increasing the maximum build log size for CruiseControl.rb

Posted by joakimk, Thu Jan 15 18:12:00 UTC 2009

I used the following to allow logs larger than 100KB in CruiseControl.rb:

 # Number of times more logging than the default 
 MORE_LOGGING = 5

 Build.class_eval do

   def contents_for_display(file)
     return '' unless File.file?(file) && File.readable?(file)
     if File.size(file) < 100 * 1024 * MORE_LOGGING
       File.read(file)
     else
       contents = File.read(file, 100 * MORE_LOGGING)
       "#{file} is over #{100 * MORE_LOGGING} kbytes - too big to
        display in the dashboard, output is truncated\n\n\n#{contents}"
     end
   end

 end

0 comments | Filed Under: | Tags:

Temporaraly disable Kernel.warn (stderr)

Posted by joakimk, Thu Dec 18 14:02:00 UTC 2008

If you call some code that print warnings to stderr (like Kernel.warn) that can be safetly ignored, you can use this trick to temporaraly turn off stderr:

  $stderr = File.open('/dev/null', 'w')
  code_that_prints_to_stderr
  $stderr = STDERR

Or you cound wrap it in a block:

  def disable_warnings
    $stderr = File.open('/dev/null', 'w')
    yield
    $stderr = STDERR
  end

  disable_warnings do
    code_that_prints_to_stderr
  end

Update: Just learned that if you’re in the context of a rails app you can do:

  silence_warnings do
    code_that_prints_to_stderr
  end

0 comments | Filed Under: | Tags:

Testing filter_parameter_logging using RSpec

Posted by joakimk, Wed Oct 29 17:27:00 UTC 2008

One way of speccing parameter filtering.

Controller:
    filter_parameter_logging :password
Spec:
    it "should filter password" do
      before_filter, after_filter = {}, {}
      before_filter['user'] = { 'username'=>'foo', 'password'=>'bar' }
      after_filter['user'] = { 'username'=>'foo', 'password'=>'[FILTERED]' }      
      controller.filter_parameters(before_filter).should == after_filter
    end

0 comments | Filed Under: | Tags:

Merb client for hoptoad

Posted by joakimk, Tue Aug 12 13:13:00 UTC 2008

I’ve written a basic merb client for hoptoad (an online exception logger), you can find it at: http://github.com/joakimk/hoptoad_notifier_merb

0 comments | Filed Under: | Tags: