Skip to content

Instantly share code, notes, and snippets.

View arschles's full-sized avatar
🎯
Focusing

Aaron Schlesinger arschles

🎯
Focusing
View GitHub Profile
@arschles
arschles / PhotosLoader1.scala
Created October 29, 2011 20:11
Android PhotosLoader in Scala
import scala.actor._
class PhotosLoader extends Actor {
override def act() {
loop {
react {
case photoToLoad:PhotoToLoad =>
val bmp = getBitmap(photoToLoad.url)
if(bmp != null) {
@arschles
arschles / gist:2288158
Created April 3, 2012 00:06
Scala PhoneGap Build Client
val BaseBuildURL = "https://build.phonegap.com/api/v1"
val UTF8Charset = Charset.forName("UTF-8")
case class MultipartInfo(contentType: Header, contentEncoding: Option[Header])
val baseHeaders: List[Header] = {
val authRaw = "%s:%s".format(phoneGapCreds.username, phoneGapCreds.password)
val authBase64 = Base64.encodeBase64(authRaw.getBytes(UTF8Charset))
val authString = new String(authBase64, UTF8Charset)
List(
new BasicHeader(HttpHeaders.AUTHORIZATION, "Basic %s".format(authString)),
new BasicHeader(HttpHeaders.ACCEPT, "*/*"),
@arschles
arschles / gist:2288251
Created April 3, 2012 00:27
Making the StackMob Java Client SDK do a synchronous get call
package my.package
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.TimeUnit;
import java.util.Map;
import java.util.HashMap;
long requestTimeout = 5;
TimeUnit requestTimeoutUnit = TimeUnit.SECONDS;//wait max 5 seconds for StackMob requests to return. if longer, fail
@arschles
arschles / simpleget.java
Created May 25, 2012 19:28
HttpService Simple GET Request
@Override
public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider provider) {
HttpService http = provider.getHttpService();
String url = "http://stackmob.com";
GetRequest req = new GetRequest(url);
HttpResponse resp = http.get(req);
Map<String, String> map = new HashMap<String, String>();
map.put("response_code", resp.getCode());
map.put("url", url);
return new ResponseToProcess(HttpURLConnection.HTTP_OK, map);
@arschles
arschles / asyncget.java
Created May 25, 2012 19:29
HttpService Async GET Requests
@Override
public ResponseToProcess execute(ProcessedAPIRequest request, SDKServiceProvider provider) {
String url1 = "http://www.bing.com/search?q=kawasaki+ninja";
GetRequest req1 = new GetRequest(url1);
String url2 = "http://www.bing.com/search?q=honda+nighthawk";
GetRequest req2 = new GetRequest(url2);
String url3 = "http://www.bing.com/search?q=bmw+1300s";
GetRequest req3 = new GetRequest(url3);
HttpService http = provider.getHttpService();
@arschles
arschles / iteratee.scala
Created May 31, 2012 00:16
Iteratee recollection from memory
import scalaz._
import Scalaz._
import scalaz.concurrent._
import java.util.concurrent.atomic._
//the representation of a "chunk" of data (including errors or EOF)
//this was the best name I could ever come up with, but it seems like there's a better one
sealed trait Chunk {
def fold[T](valid: Array[Byte] => T, error: Throwable => T, eof: => T) = this match {
case ValidBytes(data) => valid(data)
@arschles
arschles / stackmob.py
Created May 31, 2012 06:55
StackMob with Python
import oauth.oauth as oauth
import httplib
import json
import sys
PUBLIC_KEY = "68023efc-6b09-42d0-9b43-18c531d79a98"
PRIVATE_KEY = "f41c5002-64c0-46c1-b249-b667866ba305"
class Client:
def __init__(self, url, key, secret):
@arschles
arschles / typesafeEquals.scala
Created June 20, 2012 20:54
How to use === in scalaz
//declare the data structure and the Equal instance
import scalaz._
import Scalaz._
case class MyThing(thing1: String, thing2: String)
object MyThing {
implicit def MyThingEqual = new Equal[MyThing] {
override def equal(t1: MyThing, t2: MyThing) = (t1.thing1 === t2.thing1) && (t1.thing2 === t2.thing2)
}
@arschles
arschles / HttpRequestSignerAPI.java
Created July 5, 2012 19:07
OAuth Signing with StackMob Custom Code's HttpService
//goes in src/main/java/com/stackmob/oauthhttpservice
package com.stackmob.oauthhttpservice;
import org.scribe.services.TimestampService;
import org.scribe.services.TimestampServiceImpl;
import org.scribe.model.Token;
import org.scribe.builder.api.DefaultApi10a;
class HttpRequestSignerAPI extends DefaultApi10a {
public static class TimeService extends TimestampServiceImpl {
@arschles
arschles / registerIOS.java
Created July 11, 2012 18:49
Registering an iOS push token from the stackmob-java-client-sdk
StackMob.RegistrationIDAndUser json = new StackMob.RegistrationIDAndUser("a88fqsdg8as87rgq87wrg8as8dg78zDf8a98sdf98asd8fa7sdf", "johnsmith", "ios");
stackmob.postPush("register_device_token_universal", json, newStackMobCallback() {
@Override public void success(String responseBody) {
//responseBody will be a list of MyObject instances
//do something with your objects
Logger.debug("stackmob success");
}
@Override public void failure(StackMobException e) {
//handle the failure
Logger.debug("stackmob fail" + e);