Skip to content

Instantly share code, notes, and snippets.

View krhoyt's full-sized avatar

Kevin Hoyt krhoyt

View GitHub Profile
@krhoyt
krhoyt / transform.js
Created March 19, 2015 16:37
Linear transform to map a value in one range to a value in another.
function scale( value, old_top, old_bottom, new_top, new_bottom )
{
// minTo + (maxTo - minTo) * ((value - minFrom) / (maxFrom - minFrom))
return new_bottom + ( new_top - new_bottom ) * ( ( value - old_bottom ) / ( old_top - old_bottom ) );
}
@krhoyt
krhoyt / app.js
Created July 29, 2015 00:20
IBM Mobile Cloud Services Boilerplate
// Libraries
var express = require( 'express' );
var ibmbluemix = require( 'ibmbluemix' )
var ibmdata = require( 'ibmdata' );
var winston = require( 'winston' );
// Constants
var LOG_PATH = '../logs/winston.log';
// Bluemix
@krhoyt
krhoyt / quickbase.php
Created April 5, 2013 03:30
General access to QuickBase database through the XML-RPC API using PHP.
<?php
$username = "_USER_NAME_";
$password = "_PASSWORD_";
$token = "_TOKEN_";
$dbid = "_DATABASE_ID_";
$access = authenticate( $username, $password );
$survey = questions( $dbid, $token, $access["ticket"] );
@krhoyt
krhoyt / getage.js
Last active December 15, 2015 20:09
Returns the number years, months, days and minutes that have elapsed since a given date. Designed for displaying my daughters age on her web site.
var MILLIS_DAY = 24 * 60 * 60 * 1000;
var MILLIS_HOUR = 60 * 60 * 1000;
var MILLIS_MINUTE = 60 * 1000;
var MILLIS_MONTH = 30 * 24 * 60 * 60 * 1000;
var MILLIS_YEAR = 31557600000;
function getAge( birthday )
{
var days = null;
var difference = null;
@krhoyt
krhoyt / create-write-xml.m
Last active December 27, 2015 16:59
Create an XML document and write it to disk.
- ( void ) createAndWriteXMLDocument : ( NSString * ) fileName
{
NSArray * directories;
NSData * output;
NSFileManager * manager;
NSMutableString * path;
NSXMLDocument * doc;
NSXMLElement * root;
NSXMLNode * first;
NSXMLNode * last;
@krhoyt
krhoyt / send-twilio.m
Last active December 27, 2015 16:59
Send a text message using the Twilio service.
- ( void ) sendTwilioTextMessage : ( NSString * ) toNumber withBody : ( NSString * ) body
{
NSArray * items;
NSData * post;
NSError * error;
NSHTTPURLResponse * response;
NSMutableURLRequest * request;
NSString * contents;
NSXMLDocument * doc;
@krhoyt
krhoyt / http-query-string.m
Last active December 27, 2015 16:59
Send an HTTP GET request with query string values.
- ( NSString * ) encodeDictionary : ( NSDictionary * ) parameters
{
NSMutableString * query;
query = [[NSMutableString alloc] init];
[parameters enumerateKeysAndObjectsUsingBlock : ^( id key, id value, BOOL *stop ) {
[query appendFormat : @"%@=%@&", key, value];
}];
@krhoyt
krhoyt / load-parse-xml.m
Last active December 27, 2015 16:59
Load an XML file, parse the contents, and find a specific node.
- ( void ) loadAndParseXMLFile : ( NSString * ) url
{
NSArray * items;
NSArray * query;
NSError * error;
NSXMLDocument * doc;
NSXMLNode * item;
// XPath for the win!
doc = [[NSXMLDocument alloc] initWithContentsOfURL : [NSURL URLWithString : url] options : ( NSXMLNodePreserveWhitespace | NSXMLNodePreserveCDATA ) error : &error];
@krhoyt
krhoyt / http-post.m
Last active December 27, 2015 16:59
Make an HTTP POST and read the response content into a string for further processing.
- ( NSString * ) loadFileHTTPFormPost : ( NSString * ) url withFormData : ( NSDictionary * ) parameters
{
NSData * post;
NSError * error;
NSHTTPURLResponse * response;
NSMutableString * location;
NSMutableURLRequest * request;
NSString * contents;
NSString * form;
@krhoyt
krhoyt / http-get.m
Created November 7, 2013 18:32
Plain HTTP GET request.
- ( NSString * ) loadFileHTTPGet : ( NSString * ) url
{
NSArray * crayola;
NSArray * parts;
NSData * get;
NSError * error;
NSHTTPURLResponse * response;
NSMutableURLRequest * request;
NSString * contents;