Skip to content

Instantly share code, notes, and snippets.

require 'java'
require File.dirname(__FILE__) + '/commons-codec-1.3.jar'
require File.dirname(__FILE__) + '/commons-httpclient-3.1.jar'
require 'cgi'
module JRestClient
include_package 'org.apache.commons.httpclient'
include_package 'org.apache.commons.httpclient.methods'
include_package 'org.apache.commons.httpclient.params'
/*
Corresponding table:
CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`admin` tinyint(1) default '0',
PRIMARY KEY (`id`)
)
/*
JDO Persistence Manager in Scala using blocks
Sample below works with Google App Engine
Sample usage:
Persistence.manage { (pm) =>
val message = new Message("message body here")
pm.makePersistent(message)
var anchors = document.getElementsByTagName("a");
for(var i = 0; i < anchors.length; i++) {
var a = anchors[i];
var href = a.getAttribute("href");
// for some reason the HREF in this onClick is always bound to the last element in anchors[]. WHY?!
a.onclick = function(el) {
//console.log(href);
import org.mortbay.jetty.Server
import org.mortbay.jetty.servlet.Context
import org.mortbay.jetty.servlet.ServletHolder;
object Main {
def main(args: Array[String]) {
var server = new Server(8080)
println("Starting world server")
private byte[] getByteArrayFromInputStream(InputStream stream) throws IOException {
byte[] bytes;
int len;
byte[] buffer = new byte[8192];
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((len = stream.read(buffer, 0, buffer.length)) != -1) output.write(buffer, 0, len);
bytes = output.toByteArray();
} finally {
@ikai
ikai / 100_line_js_func.js
Created October 24, 2012 21:56
100 line JavaScript function
function longFunction() {
var x = 1;
x = 0;
x = 1;
x = 2;
x = 3;
x = 4;
x = 5;
x = 6;
x = 7;
@ikai
ikai / ImageFetcher.java
Created March 30, 2013 21:31
A tool I've used a few times in various Android samples. I've simplified and greatly generalized this from some code I helped write for a friend. This class helps fetch lots of images in a background thread pool, executing a callback on a main thread when complete. This code caches the images locally, opting to fetch first from the cache before …
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
@ikai
ikai / build.gradle
Created June 10, 2013 23:54
TIL: to get Gradle to use external Maven dependencies, you need to declare repositories OUTSIDE 'buildscript'. If you don't, you get an error telling you Gradle can't resolve whatever external dependencies your project depend on.
buildscript {
repositories {
maven {
url 'http://repo1.maven.org/maven2'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
@ikai
ikai / generate_token.py
Created July 1, 2013 22:12
Very simple Python sample to generate a refresh token for the YouTube API. Note that this will will also create a file called generate_token.py-oauth that contains this information.
#!/usr/bin/python
import httplib2
import os
import sys
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run