Skip to content

Instantly share code, notes, and snippets.

@hollanddd
hollanddd / rps.rb
Created October 18, 2012 22:38
rps from Sinatra Up and Running
require 'sinatra'
# before we process a route, we'll set the response as
# plain text and set up an array of viable moves that
# a player (and the computer) can perform
before do
content_type :txt
@defeat = {rock: :scissors, paper: :rock, scissors: :paper}
@throws = @defeat.keys
@hollanddd
hollanddd / nokogiri_practice.rb
Created December 5, 2012 18:39
screen scraping practice with nokogiri
require 'rubygems'
require 'nokogiri'
require 'open-uri'
def wine_woot(url)
doc = Nokogiri::HTML(open(url))
data = doc.xpath('//*[@id="summary"]/div')
puts doc.xpath('html/head/title').text + ' as site_name'
puts url + data.xpath('hgroup/a/@href').to_s + ' as site_href'
puts doc.xpath('//*[@id="todays-deal"]/a/img/@src').to_s + ' as img_src'
@hollanddd
hollanddd / deal.rb
Created December 6, 2012 20:11
scraping practice inside of AR:model
require 'open-uri'
class Deal < ActiveRecord::Base
attr_accessible :img_src, :price, :site_href, :site_name, :wine_name
def self.wine_woot(url)
doc = Nokogiri::HTML(open(url))
data = doc.xpath('//*[@id="summary"]/div')
site_name = doc.xpath('html/head/title').text
site_href = url + data.xpath('hgroup/a/@href').to_s
=begin
Given a hash of business hours that looks like:
{
"wed"=>{
"endtime"=>"08:00pm",
"starttime"=>"10:00am"
},
"sun"=>{
"endtime"=>"06:00pm",
"starttime"=>"10:00am"
=begin
There are better alternatives to storing images as a blob in the db like amazon s3,
but I don't have a choice in this case
rails g model image file_name:string content_type:string file_size:integer file_data:binary
populate in the console
rails c
require 'net/http'
=> true
host = 'host_url'
# RSpec 2.0 syntax Cheet Sheet
# defining spec within a module will automatically pick Player::MovieList as a 'subject' (see below)
module Player
describe MovieList, "with optional description" do
it "is pending example, so that you can write ones quickly"
it "is already working example that we want to suspend from failing temporarily" do
pending("working on another feature that temporarily breaks this one")
class Bird
class << self
attr_accessor :instances
end
self.instances = []
def initialize name
@name = name
@flying = false
self.class.instances << self
class Validator():
GEO = ('geo', 'lat/lng', 'location')
ZIP = ('zip code', 'postal code', 'zip')
PHONE = ('phone', 'phone number', 'number', '#')
STATE = ('state abbreviation', 'state code', 'state')
ADDRESS = ('address')
URL = ('url', 'web address', 'facebook page')
STATES = {
'AK': 'Alaska',
'AL': 'Alabama',
class Address:
'''
Algo:
Work backward. Start from the zip code, which will be near the end, and in one of two known formats: XXXXX or XXXXX-XXXX.
If this doesn't appear, you can assume you're in the city, state portion, below.
The next thing, before the zip, is going to be the state, and it'll be either in a two-letter format, or as words.
You know what these will be, too -- there's more than 50 of them.
Also, you could soundex the words to help compensate for spelling errors.
before that is the city, and it's probably on the same line as the state.
You could use a zip-code database to check the city and state based on the zip, or at least use it as a BS detector.
@hollanddd
hollanddd / email_attachment_fetcher.py
Last active August 29, 2015 13:55
fetches email attachments
import imaplib, email
def fetch_csv_from_email(self):
file_name_list = []
detatch_dir = '.'
if 'files' not in os.listdir(detatch_dir):
os.mkdir('files')
mail_session = imaplib.IMAP4_SSL('your_mail_box')
typ, account_details = mail_session.login('username', 'psswd')