Mon, 09 Jul 2007
Exception handling
./lib/mymodule.rb
module MyModule

  class Error < RuntimeError; end
  class ConnectionError < Error; end

  def connect
    
    if something_failed?
      raise ConnectionError, "connection failed due to..."
    end
  end
end
somewhere in controller
  include MyModule

  def do_connect
    begin
      connect
    rescue MyModule::ConnectionError => err
      flash.now[:notice] => err
    end
  end

(posted at 01:15 | filed under programming/rails | link)    (comments | add new)
own application configuration
./config/my_configuration.yml
MyConfig:
 username: my_username
 password: my_password
 host: my_host
./config/environment.rb
require 'ostruct'
require 'yaml'

OwnConfigFile = "#{RAILS_ROOT}/config/my_configuration.yml"
if File.exist?(MyConfigFile)
  ::ApplicationConfig = OpenStruct.new(YAML.load_file(MyConfigFile))
end
in application:
config = ApplicationConfig.MyConfig;
puts config['username']

(posted at 01:08 | filed under programming/rails | link)    (comments | add new)
Wed, 13 Sep 2006
Retrieving user login/pass from HTTP_AUTH*

I'm using this to get login information from mod_ldap_auth - user is authenticated against Microsoft's Active Directory (AD)

def getAuthenticated
  if request.env['HTTP_AUTHORIZATION'] then
    authdata = request.env['HTTP_AUTHORIZATION'].to_s.split
    user,pass = Base64.decode64(authdata[1]).split(':')[0..1]
    [ user, pass ]
  else
    [ "", "" ]
  end
end


(posted at 11:14 | filed under programming/rails | link)    (comments | add new)
Fri, 01 Sep 2006
Got Mongrel server working
I've just replaced old fashioned fast_cgi to the Mongrel ruby server. Bellow is my attempt to startup script for mongrel servers - designed for my slackware box. Hope it'll help anyone to start up his own mongrel server for Rails

#!/bin/sh # include config . /etc/rc.d/mongrel.servers function start_servers { echo -n "starting..." COUNT=${#SERVER[@]} for ((;COUNT;COUNT--)); do echo -n "#" $MONGREL start $PARAMS_START -c ${SERVER[$COUNT]} -p ${PORT[$COUNT]} >/dev/null 2>&1 done echo "Done" } function stop_servers { echo -n "stopping..." COUNT=${#SERVER[@]} for ((;COUNT;COUNT--)); do echo -n "#" $MONGREL stop -c ${SERVER[$COUNT]} >/dev/null 2>&1 done echo "Done" } function status_servers { echo "Mongrel status..." echo "" echo " PID PORT PATH " echo "------------------------------------------------------------------------------" COUNT=${#SERVER[@]} for ((;COUNT;COUNT--)); do if [ -f "${SERVER[$COUNT]}/log/mongrel.pid" ]; then PID=`cat ${SERVER[$COUNT]}/log/mongrel.pid | awk '{print $1}'` else PID="N/A" fi printf "%6s %6s %s\n" "$PID" "${PORT[$COUNT]}" "${SERVER[$COUNT]}" done echo "Done" } case "$1" in 'start') start_servers ;; 'stop') stop_servers ;; 'status') status_servers ;; *) start_servers esac
/etc/rc.d/rc.mongrel

MONGREL="mongrel_rails" PARAMS_START="-d -a 127.0.0.1" SERVER[1]="/path_to_rails_app1/" PORT[1]=8001 SERVER[2]="/path_to_rails_app2/" PORT[2]=8002
/etc/rc.d/mongrel.servers

and Apache config...

# load proxy modules LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_connect_module modules/mod_proxy_connect.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so ... <VirtualHost some_ip:80> ServerAdmin awesome@administrator DocumentRoot path_to_public_dir_of_rails_app ServerName some_server_name ErrorLog log/radiant-error_log CustomLog log/radiant-access_log combined ProxyPass / http://127.0.0.1:8001/ ProxyPassReverse / http://127.0.0.1:8001/ </VirtualHost>


(posted at 13:37 | filed under programming/rails | link)    (comments | add new)
Tue, 29 Aug 2006
Using GOOGLE API (WSDL example)
def search
 require 'soap/wsdlDriver'
 @title = 'Search Results'
 key = 'YOUR GOOGLE API KEY HERE'
 yoursite = 'YOUR SITE ADDRESS HERE'
 driver = SOAP::WSDLDriverFactory.new("http://api.google.com/GoogleSearch.wsdl").createDriver
 @results = driver.doGoogleSearch(key, @params['term']+" site:#{yoursite}", 0, 10, true, " ", false, " ", " ", " ")
end
and parsing data
<% for result in @results.resultElements %>
 

<%= result.title %>

<%= result.snippet %>

<%= result.URL %>

<% end %>

(posted at 20:48 | filed under programming/rails | link)    (comments | add new)