Skip to content

Instantly share code, notes, and snippets.

View kcurtin's full-sized avatar

Kevin Curtin kcurtin

View GitHub Profile
@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 / 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 / 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 / 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 / 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?