Skip to content

Instantly share code, notes, and snippets.

View joshtwist's full-sized avatar

Josh Twist joshtwist

View GitHub Profile
@joshtwist
joshtwist / accounts.insert.js
Last active December 10, 2015 12:08
Mobile Services script for an 'accounts' table that explores adding custom identity.
var crypto = require('crypto');
var iterations = 1000;
var bytes = 32;
var aud = "Custom";
var masterKey = "<your-master-key>";
function insert(item, user, request) {
var accounts = tables.getTable('accounts');
if (request.parameters.login) {
// this is a login attempt
var fs = require('fs');
var assert = require('assert');
var tables = require('./mockTable.js');
// read the insert function ready for testing
eval(fs.readFileSync("./table/accounts.insert.js", "utf8"));
suite('accounts tables', function() {
test('insert hashing and creating salt', function(done) {
var testUsername = "testusername";
@joshtwist
joshtwist / backup.js
Created January 20, 2013 17:14
Backup your SQL Database using Mobile Services' scheduler
function backup() {
var request = require('request');
var util = require('util');
var date = new Date();
var year = date.getUTCFullYear();
var month = date.getUTCMonth() + 1;
var day = date.getUTCDate();
var body = {
@joshtwist
joshtwist / read.js
Last active December 11, 2015 18:09
Dispatching to different functions inside a script based on the parameter
function read(query, user, request) {
var dispatch = {
op1 : operation1,
op2 : operation2,
}
if (request.parameters.operation && dispatch.hasOwnProperty(request.parameters.operation)) {
dispatch[request.parameters.operation](query, user, request);
return;
@joshtwist
joshtwist / pulling.js
Last active December 13, 2015 21:08
Creating blob files for Windows 8 Periodic Notifications - this is the scheduler script
var accountName = '<YOUR STORAGE ACCOUNT NAME HERE>';
var accountKey = '<YOUR STORAGE ACCOUNT KEY HERE>';
var azure = require('azure');
var blobService = azure.createBlobService(accountName, accountKey);
function pulling() {
var xml = '<tile>' +
'<visual>' +
'<binding template="TileSquarePeekImageAndText01">' +
'<text id="1">' + new Date().toString() + '</text>' +
@joshtwist
joshtwist / ExampleFilter.m
Last active December 15, 2015 02:08
Filter to add parameters to each request (to assist with versioning)
- (void)handleRequest:(NSURLRequest *)request
next:(MSFilterNextBlock)next
response:(MSFilterResponseBlock)response
{
// A wrapped response block that decrements the busy counter
MSFilterResponseBlock wrappedResponse = ^(NSHTTPURLResponse *innerResponse, NSData *data, NSError *error) {
response(innerResponse, data, error);
};
// add additional versioning information to the querystring for versioning purposes
@joshtwist
joshtwist / TodoService.m
Last active December 15, 2015 21:29
Simple sample of In-App purchase receipt verification and storage in Mobile Services DB
// this is from the client code - simply pass the transactionReceipt property of the SKPaymentTransaction
// as the receipt parameter to the method below. This will POST the data to the mobile service which will,
// in turn, invoke the insert function on the server shown in this gist
- (void)insertReceipt:(NSData *)receipt completion:(CompletionBlock)completion
{
NSString *string = [[NSString alloc] initWithData:receipt encoding:NSUTF8StringEncoding];
[self.receipts insert:@{ @"receipt" : string } completion:^(NSDictionary *item, NSError *error) {
completion();
}];
}
- (void)loadProducts:(QSCompletionBlock)completion;
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
self.products = response.products;
// callback on the completion block
self.loadProductsComplete();
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
// We'll add code here later
- (void)viewDidAppear:(BOOL)animated
{
MSClient *client = self.todoService.client;
if (client.currentUser != nil) {
return;
}
[client loginWithProvider:@"twitter" controller:self animated:YES completion:^(MSUser *user, NSError *error) {
[self.todoService loadProducts:^{