Skip to content

Instantly share code, notes, and snippets.

View jasonneylon's full-sized avatar

Jason Neylon jasonneylon

View GitHub Profile
public static TResult Aggregate<TSource, TAccumulate, TResult>(
this IEnumerable<TSource> source,
TAccumulate seed,
Func<TAccumulate, TSource, TAccumulate> func)
@jasonneylon
jasonneylon / counter.rb
Created April 7, 2011 10:07
Scottish ruby tutorial counter code
def counter(start=0, increment=1)
count = nil
lambda do
count.nil? ? count = start : count += increment
end
end
result = counter(2,3)
puts result.call
@jasonneylon
jasonneylon / ConfigurationExercise.rb
Created April 7, 2011 10:52
Configuration exercise from Scottish Ruby Conference
class AppServer
attr_accessor :admin_password, :port
end
class Configuration
attr_accessor :tail_logs, :max_connections, :admin_password
def initialize
@app_server = AppServer.new
end
class PostcodeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\s?[0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))$$/i
record.errors[attribute] << (options[:message] || "invalid postcode")
end
end
end
@jasonneylon
jasonneylon / address.rb
Created December 29, 2012 16:27
Uk postcode validation with rails/activerecord
class Address < ActiveRecord::Base
attr_accessible :postcode
validates_format_of :postcode, :with => /^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\s?[0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))$$/i, :message => "invalid postcode"
end
@jasonneylon
jasonneylon / address.rb
Created December 29, 2012 16:33
Using the custom validator
class Address < ActiveRecord::Base
attr_accessible :postcode
validates :postcode, :postcode => true
end
@jasonneylon
jasonneylon / form.rb
Last active December 10, 2015 17:18
Create a form style layout for a PDF using Prawn
require "prawn"
Prawn::Document.generate("form.pdf") do
move_down 5
text "Application form", style: :bold, size: 25
bounding_box([0,cursor], width: bounds.width, height: 120) do
fill_color "DCDCDC"
fill_rectangle [0, cursor], bounds.right, bounds.top
fill_color "000000"
@jasonneylon
jasonneylon / filledin_form.rb
Created January 6, 2013 13:16
Create a form style layout with filled in values for a PDF using Prawn
require "prawn"
def form(pdf, fields)
pdf.float do
pdf.bounding_box([5, pdf.cursor], width: 250, height: 200) do
fields.keys.each do |label|
pdf.pad(5) { pdf.text label }
end
end
end
@jasonneylon
jasonneylon / gist:5538770
Last active December 17, 2015 02:49
R script to read Air Quality Egg NO2 data from Cosm and plot it
install.packages('RCurl')
install.packages("ggplot2")
library(ggplot2)
library(RCurl)
# grab an api key from cosm and put it here
api_key = 'YOUR_API_KEY'
# your feed id
feed_id = '106267'
@jasonneylon
jasonneylon / gist:5652217
Last active May 16, 2016 22:28
Javascript to render AirQualityEgg data from Xively using RIckshaw JS
xively.setKey( "YOUR API KEY HERE" );
var startOfYesterday = moment().subtract("day", 1).startOf('day');
var endOfYesterday = moment().startOf('day');
console.log(startOfYesterday);
console.log(endOfYesterday);
var query = {
start: startOfYesterday.toJSON(),