Skip to content

Instantly share code, notes, and snippets.

View jasonneylon's full-sized avatar

Jason Neylon jasonneylon

View GitHub Profile
@jasonneylon
jasonneylon / hash_functions.rb
Created May 9, 2014 09:56
Flatten the values of a hash where each value is one item array
# {:a => [1], :b => [2]} => {:a => 1, :b => 2}
def flatten_hash_values(h)
Hash[h.map {|m, i| [m, i.first]}]
end
@jasonneylon
jasonneylon / gist:6455085
Created September 5, 2013 19:40
Two sided horizontal bar chart
<!DOCTYPE html>
<html>
<head>
<title>Bar Chart</title>
<script type="text/javascript" src="javascript/d3.v2.min.js"></script>
<style type="text/css">
.chart {
background: #00ccff;
margin: 10px;
@jasonneylon
jasonneylon / gist:6362994
Last active December 21, 2015 20:39
Get pairs of dates for every week in the last year
require 'active_support/all'
(12.months.ago.to_i..0.months.ago.to_i).step(1.week).to_a.push(Time.new).each_cons(2) do |from, to|
puts "From #{Time.at(from).to_s} to #{Time.at(to).to_s}"
end
OUTPUT
======
From 2012-08-28 08:11:55 +0100 to 2012-09-04 08:11:55 +0100
From 2012-09-04 08:11:55 +0100 to 2012-09-11 08:11:55 +0100
@jasonneylon
jasonneylon / gist:5676205
Created May 30, 2013 07:14
Watch the current values of a mongo document in the terminal
watch 'mongo app_db --eval "printjson(db.sessions.findOne({_id: ObjectId(\"51a62b06f9b19aac870000b1\")}))"'
@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(),
@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 / 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 / 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 / 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 / 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