Skip to content

Instantly share code, notes, and snippets.

View toast38coza's full-sized avatar

Christo Crampton toast38coza

View GitHub Profile
@toast38coza
toast38coza / call-json.swift
Created October 19, 2014 19:43
Call a JSON endpoint with swift
let url = NSURL(string: "http://www.telize.com/geoip")
let urlRequest = NSURLRequest(URL: url)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url,
completionHandler: {data, response, error -> Void in
if error != nil {
println("error")
} else {
@toast38coza
toast38coza / save-image.swift
Created October 19, 2014 19:28
Save an image downloaded from the web
let url = NSURL(string: "http://www.haskell.org/happy/Happy.gif")
let urlRequest = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(),
completionHandler: {
response, data, error in
if error != nil {
println("There was an error")
} else {
@toast38coza
toast38coza / fetch-image.swift
Created October 19, 2014 19:11
Fetch an image from the web
let url = NSURL(string: "http://www.haskell.org/happy/Happy.gif")
let urlRequest = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(),
completionHandler: {
response, data, error in
if error != nil {
println("There was an error")
} else {
@toast38coza
toast38coza / helloAngular.html
Created October 11, 2014 14:22
The smallest amount of code to demonstrate the power of AngularJS
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body ng-app="HelloWorldApp" >
<div ng-controller="HelloController" class="container" >
@toast38coza
toast38coza / MyModelValidatorTest.java
Created September 4, 2014 09:48
Testing a Spring Web Flow validator
@Test
public void testValidateSomeEventId() {
// setup:
// mock request
MockRequestContext requestContext = new MockRequestContext();
// mock message context:
MessageContext messages = requestContext.getMessageContext();
messages.clearMessages();
@toast38coza
toast38coza / call_weather_service.py
Created August 29, 2014 08:25
Calling a SOAP WebService with Python Suds
from suds.client import Client
url="http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL"
client = Client(url)
print client ## shows the details of this service
result = client.service.GetWeatherInformation()
print result ## see: restult.txt below
@toast38coza
toast38coza / unitTestSpringValidationClass
Last active August 29, 2015 13:59
Unit testing a Spring.io Validator class with ValidationContext
@Test
public void testValidateInitialDetailsCapture() {
MyModel myModel = new MyModel();
// setup:
MyModel.setSomeField("Some Value");
// this is the line that stumped me
ValidationContext validationContext = new DefaultValidationContext(requestContext, null, null);
@toast38coza
toast38coza / ansible_stdout.yml
Created February 25, 2014 08:46
Get the stdout response from an ansible task. From: https://gist.github.com/toast38coza/6bdd151eae2ee9d751a7
- name: print to stdout
command: echo "hello"
register: hello
- debug: msg="{{ hello.stdout }}"
- debug: msg="{{ hello.stderr }}"
@toast38coza
toast38coza / common.py
Created October 7, 2013 13:55
Nice dynamic upload_to method for Django FileField or ImageField
from django.contrib.sites.models import Site
import uuid
def upload_to(instance, filename):
current_site = Site.objects.get_current()
extension = filename.split(".")[-1]
instance_slug = getattr(instance,"slug",False)
if not instance_slug:
@toast38coza
toast38coza / bulksms_send_message.py
Created September 6, 2013 12:28
Send an SMS using BulkSMS and python's requests library
import requests
url = "http://bulksms.2way.co.za:5567/eapi/submission/send_sms/2/2.0"
phone_number = 27123456678
data = {'username' : 'xxxx', 'password' : 'xxxx', 'message' : 'Testing SMS', 'msisdn' : phone_number}
response = requests.post(url,data)