/
AppCommand.m
48 lines (36 loc) · 1.42 KB
/
AppCommand.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// AppCommand.m
// Chase.Mobi
//
// Created by Pete Hodgson on 10/6/10.
// Copyright 2010 ThoughtWorks. See NOTICE file for details.
//
#import <Foundation/Foundation.h>
#import "AppCommand.h"
#import "JSON.h"
#import "Operation.h"
#import "ViewJSONSerializer.h"
#import "FranklyProtocolHelper.h"
@implementation AppCommand
- (NSString *)handleCommandWithRequestBody:(NSString *)requestBody {
NSDictionary *requestCommand = [requestBody JSONValue];
NSDictionary *operationDict = [requestCommand objectForKey:@"operation"];
Operation *operation = [[[Operation alloc] initFromJsonRepresentation:operationDict] autorelease];
id <UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
if( ![operation appliesToObject:appDelegate] )
{
return [FranklyProtocolHelper generateErrorResponseWithReason:@"operation doesn't apply" andDetails:@"operation does not appear to be implemented in app delegate"];
}
id result;
@try {
result = [operation applyToObject:appDelegate];
}
@catch (NSException *e) {
NSLog( @"Exception while applying operation to app delegate:\n%@", e );
return [FranklyProtocolHelper generateErrorResponseWithReason:@"exception while executing operation" andDetails:[e reason]];
}
NSMutableArray *results = [NSMutableArray new];
[results addObject:[ViewJSONSerializer jsonify:result]];
return [FranklyProtocolHelper generateSuccessResponseWithResults: results];
}
@end