Skip to content

Instantly share code, notes, and snippets.

View robotmay's full-sized avatar

Robert May robotmay

View GitHub Profile
@robotmay
robotmay / Distance search in SQL
Created March 2, 2011 16:55
SQL query to search based on latitude, longitude, and a distance in miles from a location
# Using an example table of 'addresses' with a latitude and longitude column
# Replace #LONGITUDE#, #LATITUDE#, and #DISTANCE_IN_MILES# with your search values
SELECT addresses.*, (ACOS( SIN(RADIANS(#LATITUDE#)) * SIN(RADIANS(addresses.latitude)) + COS(RADIANS(#LATITUDE#)) * COS(RADIANS(addresses.latitude)) * COS(RADIANS(addresses.longitude) - RADIANS(#LONGITUDE#)) ) * 3963.1676) AS distance
FROM addresses
WHERE (((ACOS( SIN(RADIANS(#LATITUDE#)) * SIN(RADIANS(addresses.latitude)) + COS(RADIANS(#LATITUDE#)) * COS(RADIANS(addresses.latitude)) * COS(RADIANS(addresses.longitude) - RADIANS(#LONGITUDE#)) ) * 3963.1676) <= #DISTANCE#) OR (addresses.latitude = #LATITUDE# AND addresses.longitude = #LONGITUDE#))
@robotmay
robotmay / #ruby on freenode
Created March 29, 2011 10:04
Tue 29th March 2011
10:53 -!- penos [[email protected]] has joined #ruby
10:53 <penos> i like to be raped by 15 gays
10:53 <merlish> you want #python, not #ruby
@robotmay
robotmay / Questions
Created August 8, 2011 23:12
Frozen Rails Give-away
Day job: Web Developer at Core Web Design (in the land of persistent rain; Wales).
Your Rails contributions (if any): Moral support and a few decent bug reports.
What's your Ruby/Rail experience?: Nearly two years using Ruby/Rails. Currently using it to build projects at work and at home which I wouldn't dream of attempting by myself in anything else.
How do you use GitHub?: I use GitHub for my own projects, my work projects, and even more as my go-to place for library discovery.
@robotmay
robotmay / gist:2479149
Created April 24, 2012 12:09
Find the closest date in an array of dates
# I have an array of dates (both past and present) and a single date ('needle' below),
# for which I want to find the closest date in either direction. Here's my solution;
# can you think of a better one?
dates.sort_by { |date| (date.to_time - needle.to_time).abs }.first
@robotmay
robotmay / gist:2577270
Created May 2, 2012 15:05
Grabbing data between two elements with Nokogiri
def collect_between(first, last)
first == last ? [first] : [first, *collect_between(first.next, last)]
end
date_h3 = page.at_xpath("//h3[contains(text(), 'Date:')]")
time_h3 = page.at_xpath("//h3[contains(text(), 'Time:')]")
date_strings = collect_between(date_h3, time_h3)
date_strings[1..(date_strings.size - 2)].collect(&:text).delete_if(&:blank?)
@robotmay
robotmay / application_controller.rb
Created May 16, 2012 11:12
Examples of low level caching
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter do
@categories = Rails.cache.fetch("global/categories", expires_in: 10.minutes) do
Category.where("posts_count > 0").all
end
end
end
@robotmay
robotmay / Railscasts 1 projects_controller.rb
Created May 29, 2012 22:51
The strange and wonderful world of forgotten Rails features: HTTP Streaming
class ProjectsController < ApplicationController
stream
def index
# rest of controller code
end
end
@robotmay
robotmay / compare.rb
Created June 26, 2012 13:32
Compare multiple CSV files; specify a pattern to match across the files and it will remove duplicates from the first input file.
#!/usr/bin/env ruby
require 'optparse'
require 'csv'
@options = {
:files => [],
:pattern => /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i,
:columns => [],
:output => nil
}
@robotmay
robotmay / network_rail.go
Created July 9, 2012 12:43
Network Rail ActiveMQ Streams in Go
package main
import (
"os"
"fmt"
"errors"
"net"
"encoding/json"
"github.com/gmallard/stompngo"
)
@robotmay
robotmay / place.rb
Created July 11, 2012 11:50
Scaling down
def ddg_info
@ddg_info ||= Rails.cache.fetch("places/#{id}-#{updated_at}/ddg_info", expires_in: 3.days) do
data = DDG.zeroClickInfo(name)
end
end