Skip to content

Instantly share code, notes, and snippets.

View olegp's full-sized avatar

Oleg Podsechin olegp

View GitHub Profile
@olegp
olegp / e4x2json.js
Created October 23, 2010 20:36
one way converter from E4X XML to JSON
// One way converter from E4X XML to JSON
// 1) turns <body><item>1</item><item>2</item></body> into
// body: {item: ["1", "2"]} so that lists are easier to work with
// 2) turns things like: <body a="a">whatever</body> into
// body: {_a: "a", _: "whatever"}
// however <body>whatever</body> becomes simply body: "whatever
// - attributes specified by ignored are ignored
function E4XtoJSON(xml, ignored) {
var r, children = xml.*, attributes = xml.@*, length = children.length();
if(length == 0) {
@olegp
olegp / cache.js
Created November 5, 2010 15:44
ringo-cache
export("Cache");
function Cache() {
var self = this;
this.size = 0;
this.map = {};
function wrap(f, name, period, that) {
return function () {
var t = new Date().getTime();
(function(){
var url;
var links = document.getElementsByTagName("link");
for(var i = 0, l = links.length; i < l; i ++) {
var link = links.item(i);
if(link.tagName.toLowerCase() == "link") {
if(link.getAttribute("type") == "application/rss+xml") {
location.href = "http://www.google.com/reader/preview/*/feed/" +
"http://fulltextrssfeed.com/" +
link.getAttribute("href").substr("http://".length);
const GET = "GET", PUT = "PUT", POST = "POST", DELETE = "DELETE", HEAD = "HEAD";
function generate(base, endpoints) {
function camelCase(string) {
return string.charAt(0).toUpperCase() + string.substring(1);
}
function isArgument(component) {
return component.indexOf("{") == 0 &&
(component.indexOf("}") == component.length - 1);
}
var proxy = {};
@olegp
olegp / inodes.sh
Created April 16, 2011 12:01
Cloudkick custom plugin for checking the number of inodes in use
#!/bin/bash
echo "status ok ok"
arr=$(df -i | grep xvda | tr "\t" "\n")
OLD_IFS="$IFS";
IFS=" ";
vars=( $arr );
IFS="$OLD_IFS"
echo "metric Inodes int" ${vars[1]}
echo "metric IUsed int" ${vars[2]}
echo "metric IFree int" ${vars[3]}
@olegp
olegp / gist:980315
Created May 19, 2011 06:56
Mustache tweak
// data
{
text: {
index: {
title: "Home"
}
},
posts: []
}
@olegp
olegp / httpclient.js
Created May 30, 2011 17:22
CommonJS HttpClient implementation for Node using node-fibers
require('fibers');
var http = require('http');
exports.HttpClient = HttpClient;
// {method, url, headers, body}
// return value is {status:status, headers:{..}, body:[..]}
function HttpClient (settings) {
if (!(this instanceof HttpClient)) return new HttpClient(settings);
this.guts = {};
@olegp
olegp / package.js
Created September 7, 2011 07:09
ringo-admin package
/**
* @fileoverview Script to package up an existing RingoJS application as a WAR.
*/
//require('core/string');
var {join, Path, makeDirectory, move, copy, exists, symbolicLink, base, removeTree} = require('fs');
var engine = require('ringo/engine');
var shell = require('ringo/shell');
var Parser = require('ringo/args').Parser;
@olegp
olegp / cpu.sh
Created November 22, 2011 22:23
Cloudkick plugin for reporting CPU usage of processes based on their name
#!/bin/bash
# reports the combined cpu usage across all processes that include $1 in their name
N=0
LINES=$(ps -eo pcpu,args | grep $1)
while read -r LINE; do
C=$(echo $LINE | cut -d" " -f1)
N=$(echo "$N+$C" | bc)
done <<< "$LINES"
echo "status ok ok"
echo "metric cpu float $N"
@olegp
olegp / setup-node.sh
Created December 6, 2011 11:26
Bash script to install Node.js on Ubuntu from launchpad
# install node and node-dev (needed by node-fibers)
apt-get install python-software-properties curl -y
add-apt-repository ppa:chris-lea/node.js-devel <<EOF
EOF
apt-get update
apt-get install nodejs -y
apt-get install build-essential -y
apt-get install nodejs-dev -y
ln -s /usr/include/nodejs /usr/include/node