Skip to content

Instantly share code, notes, and snippets.

@rd13
rd13 / gist:4564928
Last active December 11, 2015 07:18
Nginx proxy pass to port 80 for Node / Rails app etc..
server {
listen 80;
server_name localhost.my_node_app
alias 10.72.12.91; #Rewrite all incoming requests to this app - for debugging on mobile devices etc where we cannot add to the hosts file.
access_log /var/log/nginx/my_node_app.log;
location / {
#Set correct headers
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
@rd13
rd13 / gist:4761725
Created February 12, 2013 11:35
Nodejitsu deploy error
info: jitsu v0.10.2, node v0.8.5
info: It worked if it ends with Nodejitsu ok
info: Executing command deploy
info: Analyzing application dependencies in node app.js
warn: Local package version appears to be old
warn: The package.json version will be incremented automatically
warn: About to write /private/var/www/blog/package.json
warn: Using '*' as a version for dependencies may eventually cause issues
warn: Use specific versions for dependencies to avoid future problems
warn: See: http://package.json.jit.su for more information
@rd13
rd13 / gist:5281231
Created March 31, 2013 16:43
Nokogiri / Rails - Parse currency conversion rates
doc = Nokogiri::HTML(open("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"))
doc.remove_namespaces!
doc.xpath("//Cube/Cube/Cube").each do |node|
puts node.attr("rate")
end
@rd13
rd13 / gist:5320930
Created April 5, 2013 17:05
Regex to match square brackets at end of string
$v = "Muse - Supermassive Black Hole[Dubstep Version][muse.mp3]";
//Match ending [], that contains .mp3
preg_match("/\[([^]]*.mp3+)\]$/i", $v, $match);
//Remove [] from matched string
$track_url = preg_replace('/[[\]]/', '', $match[0]);
//Remove mp3 link from title
$track_title = preg_replace("/\[([^]]*.mp3+)\]$/i", '', $v);
@rd13
rd13 / gist:5367834
Created April 11, 2013 22:49
Javascript function to pre-populate multiple 2D arrays
populateEmptyArrays = function(vars, num) {
//Function to pre-populate multiple 2D arrays
//vars = Array of variable names to pre-populate with arrays
//num = Array size
if(!(vars instanceof Array) || isNaN(num)) return;
for(i in vars) {
window[vars[i]] = (function(a){ while(a.push([]) < num); return a})([])
}
}
@rd13
rd13 / javascript-find-all.js
Created May 8, 2013 11:19
String search functions in Javascript, tests with and without Regex. Summary: Regex is faster. http://jsperf.com/javascript-find-all
// http://jsperf.com/javascript-find-all
function indexes(str, find) {
var result = [];
for(i=0;i<str.length; ++i) {
if (str.substring(i, i + find.length) == find) {
result.push(i);
}
}
return result;
@rd13
rd13 / gist:5539825
Created May 8, 2013 11:20
Count the number of matches in a given string.
function matches(str, find) {
var regex = new RegExp(find,"gi");
return str.match(regex).length;
}
console.log(matches("I learned to play the Ukulele in Lebanon.", "le"));
@rd13
rd13 / gist:5561606
Created May 11, 2013 22:11
Javascript - Swap adjacent letters
letterSwap = function(str, swap) {
ot = str.split('');
for( i in str) {
if(i%swap===0) {
ot[i] = str[parseInt(i)+swap-1];
ot[parseInt(i)+swap-1] = str[i];
}
}
return ot.join('');
}
@rd13
rd13 / Bikes.m
Last active December 17, 2015 11:58
#import "Bikes.h"
static sqlite3 *database = nil;
static sqlite3_stmt *detailStmt = nil;
@implementation Bikes
@synthesize BikesID, BikesName, price, isDirty, modelCount;
@synthesize modelName,modelID,modelType,groupCount,modelBrand;
@synthesize year,category,displacement,power,topSpeed,fuelConsumption,powerWeightRatio,naughtSixty,torque;
@rd13
rd13 / gist:7372240
Created November 8, 2013 15:01
Enigma Javascript
;'use strict';
enigma = (function() {
var _rotors = [ 'EKMFLGDQVZNTOWYHXUSPAIBRCJ',
'AJDKSIRUXBLHWTMCQGZNPYFVOE',
'BDFHJLCPRTXVZNYEIWGAKMUSQO'
]
, _reflector = "YRUHQSLDPXNGOKMIEBFZCWVJAT"
, _alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"