Permalink
Browse files

v1.0 commit.

Library now fully functional.
  • Loading branch information...
1 parent a732a09 commit 7cd3ac7d0e0821a51e566f1e8ae69b5633619e3f Alex Olson committed Jan 3, 2011
Showing with 81 additions and 28 deletions.
  1. +3 −1 Examples/CocoaApp/AppController.m
  2. +27 −9 Examples/CocoaApp/Kynetx.m
  3. +4 −1 Examples/CocoaApp/main.m
  4. +47 −17 kynetx.m
@@ -15,7 +15,9 @@ @implementation AppController
-(IBAction) sendTestKynetx:(id) sender {
[self setApp:[[Kynetx alloc] initWithAppID:[appIDField stringValue]]];
- NSArray* ok = [app raiseEvent:@"jump_for_joy" params:[NSDictionary dictionaryWithObjectsAndKeys:@"yay", @"nay", @"ok", @"false", nil]];
+ NSDictionary* urlParams = [NSDictionary dictionaryWithObjectsAndKeys:@"bar", @"foo", @"whiz", @"cheese", nil];
+ NSArray* directives = [app raiseEvent:@"jump_for_joy" params:urlParams];
+ NSLog(@"%@", directives);
}
-(IBAction) appIDVal:(id) sender {
View
@@ -30,29 +30,47 @@ - (id) init {
- (id) initWithAppID:(id) input {
if (self = [super init]) {
- if (input == nil) {
- input = @"a369x123";
- }
[self setAppid:input];
- [self setEventDomain:@"desktop"];
+ // need to see if there's a better way to do this.
+ // the purpose of this check is to see what event domain to set.
+ // if NSapp is true, then we are in a Cocoa app
+ if (NSApp) {
+ [self setEventDomain:@"desktop"];
+ } else {
+ [self setEventDomain:@"iphone"];
+ }
}
return self;
}
- (NSArray*) raiseEvent:(NSString *) name params:(NSDictionary*) params {
- NSString* urlString = [NSString stringWithFormat:@"https://cs.kobj.net/blue/event/%@/%@/%@?", [self eventDomain], name, [self appid]];
+ NSString* urlString = [NSString stringWithFormat:@"https://cs.kobj.net/blue/event/%@/%@/%@", [self eventDomain], name, [self appid]];
NSURL* url = [self URLFromDict:params withBaseURL:urlString];
- NSLog(@"URL STRING IN RAISEEVENT: %@",url);
+ NSURLRequest* request = [NSURLRequest requestWithURL:url];
+ NSLog(@"Request: %@", request);
+ NSData* knsResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ return [self parseDirectives:knsResponse];
}
- (NSArray*) parseDirectives:(NSData*) response {
SBJsonParser* parser = [[[SBJsonParser alloc] init] autorelease];
NSString* responseString = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding] autorelease];
- NSLog(@"KNS Response: %@", responseString);
NSRange knsCommentRange = NSMakeRange(0, 32);
NSString* jsonString = [responseString stringByReplacingCharactersInRange:knsCommentRange withString:@""];
- NSLog(@"jsonString: %@", jsonString);
- return [parser objectWithString:jsonString];
+ NSArray* rawDirectives = [[parser objectWithString:jsonString] objectForKey:@"directives"];
+ NSMutableArray* directives = [[[NSMutableArray alloc] initWithCapacity:20] autorelease];
+ for (NSDictionary *rawDirective in rawDirectives) {
+ NSDictionary* meta = [rawDirective objectForKey:@"meta"];
+ NSDictionary* directive = [NSDictionary dictionaryWithObjectsAndKeys:
+ [meta objectForKey:@"rid"], @"rid",
+ [meta objectForKey:@"rule_name"], @"rule_name",
+ [meta objectForKey:@"txn_id"], @"txn_id",
+ [rawDirective objectForKey:@"name"], @"action",
+ [rawDirective objectForKey:@"options"], @"options",
+ nil];
+ [directives addObject:directive];
+ }
+ return directives;
}
- (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring {
View
@@ -10,5 +10,8 @@
int main(int argc, char *argv[])
{
- return NSApplicationMain(argc, (const char **) argv);
+ NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
+ int retVal = NSApplicationMain(argc, (const char **) argv);
+ [pool drain];
+ return retVal;
}
View
@@ -9,6 +9,14 @@
#import "Kynetx.h"
+// secret
+@interface Kynetx ()
+// overwrite property
+@property (nonatomic, retain) NSString* eventDomain;
+
+@end
+
+
@implementation Kynetx
// property synthesis
@@ -17,42 +25,58 @@ @implementation Kynetx
- (id) init {
// just pass nil to preferred constructor
- return [[[self alloc] initWithAppID:nil] autorelease]; // return allocated, autoreleased instance
+ return [self initWithAppID:nil];
}
- (id) initWithAppID:(id) input {
if (self = [super init]) {
- if (input == nil) {
- input = @"a369x123";
- }
[self setAppid:input];
- [self setEventDomain:@"desktop"];
+ // need to see if there's a better way to do this.
+ // the purpose of this check is to see what event domain to set.
+ // if NSapp is true, then we are in a Cocoa app
+ if (NSApp) {
+ [self setEventDomain:@"desktop"];
+ } else {
+ [self setEventDomain:@"iphone"];
+ }
}
- return self; // need to return alloced instance
+ return self;
}
- (NSArray*) raiseEvent:(NSString *) name params:(NSDictionary*) params {
- NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://cs.kobj.net/blue/event/%@/%@/%@", [self eventDomain], name, [self appid]]]];
- NSLog(@"Request URL: %@", request);
- NSData* response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- [self URLFromDict:[[[NSDictionary alloc] initWithObjectsAndKeys:@"yay",@"bay", nil] autorelease] withBaseURL:@"fdskljfdsjkdfsjklsdfkjldfsjklfdsjkl"];
- return [self parseDirectives:response];
+ NSString* urlString = [NSString stringWithFormat:@"https://cs.kobj.net/blue/event/%@/%@/%@", [self eventDomain], name, [self appid]];
+ NSURL* url = [self URLFromDict:params withBaseURL:urlString];
+ NSURLRequest* request = [NSURLRequest requestWithURL:url];
+ NSLog(@"Request: %@", request);
+ NSData* knsResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ return [self parseDirectives:knsResponse];
}
- (NSArray*) parseDirectives:(NSData*) response {
SBJsonParser* parser = [[[SBJsonParser alloc] init] autorelease];
NSString* responseString = [[[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding] autorelease];
- NSLog(@"KNS Response: %@", responseString);
NSRange knsCommentRange = NSMakeRange(0, 32);
NSString* jsonString = [responseString stringByReplacingCharactersInRange:knsCommentRange withString:@""];
- NSLog(@"jsonString: %@", jsonString);
- return [parser objectWithString:jsonString];
+ NSArray* rawDirectives = [[parser objectWithString:jsonString] objectForKey:@"directives"];
+ NSMutableArray* directives = [[[NSMutableArray alloc] initWithCapacity:20] autorelease];
+ for (NSDictionary *rawDirective in rawDirectives) {
+ NSDictionary* meta = [rawDirective objectForKey:@"meta"];
+ NSDictionary* directive = [NSDictionary dictionaryWithObjectsAndKeys:
+ [meta objectForKey:@"rid"], @"rid",
+ [meta objectForKey:@"rule_name"], @"rule_name",
+ [meta objectForKey:@"txn_id"], @"txn_id",
+ [rawDirective objectForKey:@"name"], @"action",
+ [rawDirective objectForKey:@"options"], @"options",
+ nil];
+ [directives addObject:directive];
+ }
+ return directives;
}
- (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring {
NSMutableString* buildString = [[[NSMutableString alloc] init] autorelease];
- NSPredicate* hasQuestionMark = [[[NSPredicate alloc] initWithFormat:@"SELF matches %@", @"\?$"] autorelease];
- if (![hasQuestionMark evaluateWithObject: URLstring]) {
+ NSRange questionMarkRange = [URLstring rangeOfString:@"?"];
+ if (questionMarkRange.location == NSNotFound || questionMarkRange.location != URLstring.length - 1) {
// if the base url string does not have a question mark at the end, we need to add it
[buildString appendFormat:@"%@%@", URLstring, @"?"];
} else {
@@ -66,8 +90,14 @@ - (NSURL*) URLFromDict:(NSDictionary*) params withBaseURL:(NSString*) URLstring
for (int i = 0; i < count; i++) {
id key = [keys objectAtIndex:i];
id value = [params objectForKey:key];
-
+ if (i != count - 1) {
+ [buildString appendFormat:@"%@=%@&",key,value];
+ } else {
+ [buildString appendFormat:@"%@=%@",key,value];
+ }
}
+
+ return [[[NSURL alloc] initWithString:buildString] autorelease];
}
// destructor

0 comments on commit 7cd3ac7

Please sign in to comment.