Skip to content

Instantly share code, notes, and snippets.

View hwatkins's full-sized avatar

Hugh Watkins hwatkins

View GitHub Profile
@hwatkins
hwatkins / twitpic.rb
Created July 23, 2008 19:55
Simple script to find most popular twitpic photos
require 'rubygems'
require 'mechanize'
require 'hpricot'
site="www.twitpic.com"
# Grab the highest picture number
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Mac Safari'
page = agent.get("http://#{site}/public_timeline")
doc = Hpricot(page.body)
More Asynchronous Processing
Dave » 04 November 2008 » In Technology »
In a previous series on using workling and starling for asychronous processing, I described how to setup background tasks. Here is a quick way to use this for emails without a lot of changes to your application.
First, create lib/asynch_mail.rb:
# Makes an actionmailer class queue its emails into a workling queue
# instead of sending them sycnhronously
#
Ruby Background Tasks with Starling
Dave » 25 March 2008 » In Technology »
At Inquisix, we help sales professionals exchange trusted referrals. To do that requires several background tasks, some that could take 10-15 minutes to process. Obviously, I can’t make a client wait for that, so I needed a system that could handle background tasks. At first, I started with backgroundrb, and it worked just fine. Backgroundrb was in production for two months while Inquisix grew. However, there were a few things about backgroundrb that bothered me:
It uses a lot of memory. Every worker creates at least one process. Plus, there is a master process to watch everything and deal with communication. It doesn’t take much before you end up with 5-6 processes. I had to upgrade my test server just to deal with the extra memory requirements.
It’s not easy to build a queue with control over threads without creating a ton of processes.
Too many times, I wanted to do something pretty straight forward, but I had to dig throug
How great is named_scope?
Dave » 12 December 2008 » In Technology » 3 Comments
If you haven’t used named_scope in Rails 2.1+ yet, I suggest you learn about it ASAP. Prior versions of Rails had a plugin that could perform similar functions, but it’s nice to have it part of the core system.
What does it do?
It does just what it says. It gives you a way to utilize the power of with_scope but name it. The best way to show it is with an example.
Let’s say you have a user model. Users have a gender attribute and a activated flag. When operating on your user object, you could sprinkle your code with something like:
module Main where
import Network.Socket
import System.Environment
import Control.Concurrent
import Control.Monad
-- Port number to listen on
listenPort :: PortNumber
listenPort = 3000
%%% File : recomendations.erl
%%% Author : Russell Brown <russell@pango.lan>
%%% Description : The first chapter of Programming Collective Inteligence, but in elrang, like.
%%% Created : 15 Jun 2009 by Russell Brown <russell@pango.lan>
-module(recomendations).
-compile(export_all).
%%
Where would the target audience becoming from? I assume primarily Java or .NET
I’m not qualified for .NET, but most the questions I get when you mention using Ruby (primarily Rails) instead of Java (J2EE) is they equate that to the View Layer, not the model and controller part.
So I would pick a sample application that would really show the entire stack. Here would be some ideas:
Large Retailer: System allows customers to be notified when product X is back in stock at their local store. The Ruby system must be able to talk back with Mainframe for order shipments and a J2EE application for pricing and customer records. In this case the only model is the “in stock request” the login information for the customer may or may not reside in the Ruby system
Airline: System to send SMS messages about Flight Delays based on Frequent Flyer information. Assume the airlines current Web interface will be where the customer enables this, so there is no user focused View layer instead it’s primarily a web interface
%%
%% Load balancer
%%
%% Author: Krzysztof KliÅ› <krzysztof.klis@gmail.com>
%%
%% This program is free software: you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation, either version 3 of the License, or
%% (at your option) any later version.
%%
$:.unshift('~/rufus/rufus-tokyo/lib')
require 'benchmark'
require 'rubygems'
require 'faker'
require 'date'
#
# the data
#
#!/usr/bin/env ruby
#
# Discover the contribution status of a user for all their watched repos: owner, collaborator, or watcher
# Returns JSON { "repo" => { ... }, "owner" => is project owner?, "collaborator" => has collaborator access? }
#
# Returns either Array of JSON above; or streams each JSON result as discovered (--stream flag)
#
# Usage:
#
# Save to file github_user_collaborations.rb