Skip to content

Instantly share code, notes, and snippets.

def self.days_of_week time_ago
days = Hash.new(0)
Transaction.find_each(:conditions => ["created_at > ?", time_ago]) do |t|
days[t.created_at.strftime('%A')] += 1 if t == t.user.transactions.first(:order => 'created_at')
end
puts days.sort{|a,b| b[1]<=>a[1]}.inspect
end
# [["Saturday", 44], ["Sunday", 62], ["Friday", 69], ["Monday", 80], ["Thursday", 93], ["Tuesday", 110], ["Wednesday", 115]]
barmstrong:universitytutor$ rake sitemap:install --trace
(in /Users/barmstrong/NetBeansProjects/universitytutor)
** Invoke sitemap:install (first_time)
** Invoke sitemap:require (first_time)
** Execute sitemap:require
rake aborted!
no such file to load -- sitemap_generator
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
/Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/dependencies.rb:156:in `require'
/Users/barmstrong/NetBeansProjects/viper/vendor/rails/railties/lib/commands/runner.rb:47: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/openssl/buffering.rb:178:in `syswrite': SSL_write:: ssl handshake failure (OpenSSL::SSL::SSLError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/openssl/buffering.rb:178:in `do_write'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/openssl/buffering.rb:219:in `print'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/imap.rb:1049:in `put_string'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/imap.rb:1021:in `send_command'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/imap.rb:1019:in `send_command'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/ima
@barmstrong
barmstrong / gist:1030817
Created June 17, 2011 03:33
Ruby file to use the Google Prediction API, with a very hacked OAuth2
# Ruby file to use the Google Prediction API, with a very hacked OAuth2
# You'll want to replace all the custom variables including..
# 1. Your google storage bucket name
# 2. Your google storage access credentials (note that gstore only works with "legacy" google storage access so you'll need to enabled this)
# 3. Your OAuth credentials which you setup from here https://code.google.com/apis/console/ by selecting "API Access"
# Note that I choose "Create client ID" and then "Installed Application".
#
# This script is intended to be run as a regular background process (like a cron job) to process data. It has no access to a browser and no web server to expose a callback url. Hence the hacking of OAuth2. This seems completely wrong to me but I haven't gotten any other authentication with the API to work. If anyone knows a better way please post a comment!
#
@barmstrong
barmstrong / 20110629202502_create_oauth2s.rb
Created June 30, 2011 01:38
sample code for setting up the Google Prediction API in a background process using OAuth2 and the google-api-ruby-client
class CreateOauth2s < ActiveRecord::Migration
def self.up
create_table :oauth2s do |t|
t.string :api
t.string :refresh_token
t.string :access_token
t.datetime :expires_at
t.timestamps
end
@barmstrong
barmstrong / gist:1302842
Created October 21, 2011 01:03
Ruby script to connect to New Relic and turn on our flame lamp!
#!/usr/bin/env ruby
## as seen on http://nerds.airbnb.com/monitoring-your-serverswith-fire
THRESHOLD = 1000 # milliseconds
flame_on = false
def log ms, msg
puts "#{Time.now.to_s} \t #{ms}ms \t #{msg}"
end
@barmstrong
barmstrong / gist:1323865
Created October 28, 2011 23:50
processing large csv files in ruby
class ZendeskTicketsJob
extend Resque::Plugins::ExponentialBackoff
@queue = :low
FIELDS = ['zendesk_id', 'requester_id', 'assignee_id', 'group', 'subject', 'tags', 'status', 'priority', 'via', 'ticket_type', 'created_at', 'assigned_at', 'solved_at', 'resolution_time', 'satisfaction', 'group_stations', 'assignee_stations', 'reopens', 'replies', 'first_reply_time_in_minutes', 'first_reply_time_in_minutes_within_business_hours', 'first_resolution_time_in_minutes', 'first_resolution_time_in_minutes_within_business_hours', 'full_resolution_time_in_minutes', 'full_resolution_time_in_minutes_within_business_hours', 'agent_wait_time_in_minutes', 'agent_wait_time_in_minutes_within_business_hours', 'requester_wait_time_in_minutes', 'requester_wait_time_in_minutes_within_business_hours', 'reservation_code', 'requires_manual_closing']
def self.perform(url)
`rm /tmp/zendesk_tickets*`
`wget #{url} -O /tmp/zendesk_tickets.csv.zip`
`unzip -p /tmp/zendesk_tickets.csv.zip > /tmp/zendesk_tickets.csv`
require 'sunspot'
require 'mongoid'
require 'sunspot/rails'
class Post
include Mongoid::Document
field :title
include Sunspot::Mongoid
searchable do
text :title
# Another attempt using the built in Rails.cache.fetch (better serialization/deserialization)
class ActiveSupport::Cache::DalliStore
def fast_fetch(name, options=nil)
options ||= {}
name = expanded_key name
dupe_name = name+'_dupe'
if block_given?
unless options[:force]
<div class="coinbase-button" data-code="818bdfa54c1525d774440edbd267ee60"></div>
<script src="https://coinbase.com/assets/button.js" type="text/javascript"></script>