Skip to content

Instantly share code, notes, and snippets.

@greenisus
greenisus / gist:10670762
Created April 14, 2014 18:14
HS Event Routing
- (void)handleEvent:(NSDictionary *)json {
NSData *data = [json[@"json"] dataUsingEncoding:NSUTF8StringEncoding];
NSString *type = json[@"event_type"];
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if ([type isEqualToString:@"new_tweet"]) {
[self handleNewTweet:object];
@greenisus
greenisus / gist:3080870
Created July 10, 2012 03:57
Preference for open in Chrome or Safari
#define kBrowserPref @"browserPref"
- (IBAction)openInBrowserButtonPressed:(id)sender {
NSString *myURL = nil; // put however you get the URL here
// can we open in Chrome?
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"googlechrome:"]]) {
// let's check the preference
@greenisus
greenisus / gist:1649217
Created January 20, 2012 19:49
multipart POST
+ (void)multipartPost:(NSString *)path postParams:(NSDictionary *)postParams success:(void (^)(id object))successHandler
failure:(void (^)(NSHTTPURLResponse*, NSData*, NSError*))failureHandler NS_AVAILABLE(10_7, 5_0) {
NSString *boundary = @"----------------------------ToOdOiPhOnEaPp";
NSMutableURLRequest *request = [self postRequest:path];
NSString *contentType = $S(@"multipart/form-data, boundary=%@", boundary);
[request setValue:contentType forHTTPHeaderField:@"Content-type"];
@greenisus
greenisus / gist:1304992
Created October 21, 2011 21:12
RSServer get
+ (void)get {
[RSServer get:nil completionBlock:^(CKResult *result) {
NSLog(@"server get response code %i: %@", result.responseCode, [[NSString alloc] initWithData:result.responseBody encoding:NSUTF8StringEncoding]);
if ([result isSuccess]) {
// We should remove servers that didn't come back in the results.
// To do this, we need to find the complement to the result set and
- (id) serialize:(id) object{
NSData *value = nil;
id serializedObject = nil;
if ([object isKindOfClass:[CKRecord class]]) {
CKRecord *record = (CKRecord *)object;
NSMutableDictionary *jsonDictionary = [[NSMutableDictionary alloc] init];
RSServer *server = [RSServer blank];
server.name = @"ck-test";
server.flavorId = [[RSFlavor first] id];
server.imageId = [[RSImage first] id];
[server post:nil completionBlock:^(CKResult *result) {
if ([result isSuccess]) {
[self alert:@"success!"];
- (void)rebuildWithImage:(RSImage *)image success:(CKBasicBlock)successBlock failure:(void (^)(CKResult *result))failureBlock {
CKRequest *request = [self actionRequest];
[request setBodyWithJSONDict:$D($D(image.id, @"imageId"), @"rebuild")];
[self send:request success:successBlock failure:failureBlock];
}
- (CKRequest *)actionRequest {
// TODO: self.id needs to be a string instead of an int
CKRequest *request = [CKRequest requestWithRemotePath:$S(@"/servers/%@/action", self.id)];
NSLog(@"request url: %@", request.remoteURL);
request.method = CKRequestMethodPOST;
[request addHeaders:[NSDictionary dictionaryWithObject:[[RSAccount activeAccount] api_auth_token] forKey:@"X-Auth-Token"]];
return request;
}
@greenisus
greenisus / gist:1254963
Created September 30, 2011 21:00
account data source
//
// RSAccountsDataSource.m
// Rackspace
//
// Created by Michael Mayo on 9/27/11.
// Copyright (c) 2011 Rackspace, US Inc. All rights reserved.
//
#import "RSAccountsDataSource.h"
#import "RSAccount.h"
@greenisus
greenisus / gist:1254957
Created September 30, 2011 20:58
account vc
//
// RSAccountsViewController.m
// Rackspace
//
// Created by Michael Mayo on 9/27/11.
// Copyright (c) 2011 Rackspace, US Inc. All rights reserved.
//
#import "RSAccountsViewController.h"
#import "RSAccountsDataSource.h"