Skip to content

Instantly share code, notes, and snippets.

View anolson's full-sized avatar
👋

Andrew Olson anolson

👋
View GitHub Profile
@anolson
anolson / application_controller.rb
Created November 16, 2010 16:11
Rails set mobile format.
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :redirect_mobile_device
before_filter :set_mobile_format
private
def redirect_mobile_device
if is_mobile_device?
@anolson
anolson / application_controller.rb
Created November 16, 2010 21:22
Rails navigation helpers
module ApplicationHelper
def navigation(collection)
content_tag :ul do
build_navigation_items(collection)
end
end
def build_navigation_items(collection)
content = ""
collection.each do |item|
@anolson
anolson / random_pronounceable_string.rb
Created December 20, 2010 00:37
Create random pronounceable strings
module RandomPronounceableString
VOWELS = "aeiou"
CONSONANTS = "bcdfghjklmnprstvwyz"
def random_pronounceable
%(#{random_consonant}#{random_vowel}#{random_consonant}#{random_vowel}#{random_consonant}#{random_vowel})
end
def random_vowel
random_letter(VOWELS)
@anolson
anolson / model.rb
Created December 21, 2010 20:02
ActiveRecord like model with validations and dynamic attributes
require 'rubygems'
require 'active_model'
require 'active_support'
class Model
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
def initialize(attributes = {})
@anolson
anolson / google_geocode.rb
Created January 9, 2011 20:41
Simple Google geocoder with HTTParty.
require 'rubygems'
require 'httparty'
module Google
module Geocode
class RequestDenied < StandardError; end
class Client
include HTTParty
@anolson
anolson / ride4ruby.rb
Created January 11, 2011 21:21
January Contest: Ride4Ruby
#!/usr/bin/env ruby
require 'net/http'
require 'rexml/document'
module Ride4Ruby
GOOGLE_MAPS_API_KEY = "your google key"
@states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"]
class Location
@anolson
anolson / dialog.js
Created March 2, 2011 20:31
Super simple ajax javascript dialog
document.observe('dom:loaded', function(){
$('click_me').observe('click', function(event) {
activateDialog(event);
});
});
function activateDialog(event) {
var element = Event.findElement(event);
fetchDialogContent(element.href);
event.stop();
require "test/unit"
class TestFibonachi < Test::Unit::TestCase
def test_fibonachi_numbers
numbers = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]
numbers.each_with_index { |number, index|
assert_equal number, Fibonachi.number(index)
}
@anolson
anolson / sendfile.rb
Created August 26, 2011 21:01
Upload a file to Strava
#!/usr/bin/env ruby
require 'net/smtp'
require 'optparse'
module SendFile
class CommandlineOptions
attr_accessor :options
@anolson
anolson / log_producer.rb
Created November 10, 2011 21:53
Log streaming in ruby.
class LogProducer
attr_accessor :streams
def initialize(options = {})
@streams = Hash.new
@filename = options[:filename]
add_stream(options[:stream])
end