Skip to content

Instantly share code, notes, and snippets.

View kcurtin's full-sized avatar

Kevin Curtin kcurtin

View GitHub Profile
@kcurtin
kcurtin / authentication.rb
Created July 31, 2012 14:02
A much needed refactoring of the create action in my authentication controller, refactored code on top, original on bottom
class AuthenticationsController < ApplicationController
def create
omniauth = auth_hash
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
if authentication
sign_in(authentication.user)
redirect_to root_url, notice: "Signed in!"
else
sign_in(User.create_from_omniauth(omniauth)) if current_user.blank?
@kcurtin
kcurtin / jukebox.rb
Created October 17, 2012 20:14
library and song classes
def assert_equal(actual, expected)
if expected == actual
puts 'pass'
else
puts "fail: expected #{expected}, got #{actual}"
end
end
def assert(statement)
if statement
@kcurtin
kcurtin / threaded.rb
Created October 19, 2012 16:38
threading in ruby: original implementation and the one with threading
def scrape_away(args)
threads = []
self.job_url_collection.each do |job_url|
threads << Thread.new do
job_doc = Nokogiri::HTML(open(job_url))
title = job_doc.css(args[:title_selector]).inner_text.strip
company = job_doc.css(args[:company_selector]).inner_text.strip
source = self.source
job_url = job_url.strip
location = job_doc.css(args[:location_selector]).inner_text.strip
@kcurtin
kcurtin / student_database.rb
Created October 20, 2012 14:47
Marshalling
def self.save_record(student)
record = Marshal.dump(student)
db_connection.execute('INSERT INTO students (student) VALUES (?);', [record])
end
def self.update_record(student)
id = student.id
row = Marshal.dump(student)
db_connection.execute('UPDATE students SET student = ? WHERE id = ?', [row, id])
end
@kcurtin
kcurtin / scraper.rb
Created October 21, 2012 14:03
scraping
class Scraper
require 'nokogiri'
require 'open-uri'
attr_accessor :source, :index_url, :index_doc, :job_url_collection, :base_url_for_job, :job_database
def initialize(source, index_url, base_url_for_job, job_database)
@source = source
@index_url = index_url
@base_url_for_job = base_url_for_job
@kcurtin
kcurtin / answer.rb
Created November 4, 2012 16:25
sexy string conversion
original = 'g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.'
original.each_char.each_with_object("") do |character, final|
character.next!.next! unless character.match /\W/
final << character
end
@kcurtin
kcurtin / what.rb
Created November 15, 2012 03:15
WTF?
Tweet.where('created_at > ?','2012-11-15 02:18:28')
Tweet Load (0.5ms) SELECT "tweets".* FROM "tweets" WHERE (created_at > '2012-11-15 02:18:28') ORDER BY published_at DESC
=> [#<Tweet id: 30, content: "RARARA", published_at: "2012-11-15 02:18:28", handle: nil, created_at: "2012-11-15 02:18:28", updated_at: "2012-11-15 02:18:28", person_id: 1>]
#Aren't the created_at dates equal? So the query should return false...
'2012-11-15 02:18:28'.to_datetime.in_time_zone('UTC')
@kcurtin
kcurtin / test_spec.rb
Created April 18, 2013 19:24
Test to ensure there are text versions of all your HTML email templates
describe UserMailer do
it "there is a corresponding text email template for each HTML template" do
# Path to the mailer templates
mailers_path = "#{Rails.root}/app/views/user_mailer/"
result = Dir.foreach(mailers_path) do |template|
# Ignore partials
next if template.match /^_/
# Check if it is an HTML template
@kcurtin
kcurtin / rspec_macro.vim
Created May 22, 2013 18:40
For anyone working with a test suite that uses the original (soon to be deprecated?) "should" syntax for rspec, this is a nice little macro that will convert shoulds to expects
" Example:
" response.body.should have_content("something")
" expect(response.body).to have_content("something")
" =================================================
" Macro that converts rspec "should"s to "expect"s
" =================================================
function! ConvertToExpect()
:normal! dd
:normal! P
@kcurtin
kcurtin / date_range.rb
Created June 14, 2013 14:04
Finally found a good way to set up default values for a Struct..
require 'date'
DateRange = Struct.new(:start_date, :end_date) do
def start_date
self[:start_date] || Date.today - 30
end
def end_date
self[:end_date] || Date.today
end