PageRenderTime 60ms CodeModel.GetById 12ms app.highlight 45ms RepoModel.GetById 1ms app.codeStats 1ms

/Mobile/iPhone/StoffiRemote/StoffiRemote.xcodeproj/RESTClientSimulator.m

http://yet-another-music-application.googlecode.com/
Objective C | 86 lines | 56 code | 18 blank | 12 comment | 4 complexity | f5486faf6f8ff80bd16d45a627821988 MD5 | raw file
 1//
 2//  RESTClientSimulator.m
 3//  StoffiRemote
 4//
 5//  Created by Fredrik Gadnell on 9/29/11.
 6//  Copyright 2011 __MyCompanyName__. All rights reserved.
 7//
 8
 9#import "RESTClientSimulator.h"
10#import "SimulatedServer.h"
11#import "User.h"
12
13#define kKeyRequest @"request"
14#define kKeyDelegate @"delegate"
15#define kKeyResult @"result"
16
17@implementation RESTClientSimulator
18
19+ (RESTClientSimulator *)sharedClientSimulator
20{
21    static dispatch_once_t once;
22    static RESTClientSimulator *sharedClient;
23    dispatch_once(&once, ^ {sharedClient = [[RESTClientSimulator alloc] init];});
24    return sharedClient;
25}
26
27// Performs a simulated callback that tries to immitate the servers response.
28- (RESTRequest *)simulatedRequestWithPath:(NSString *)path httpMethod:(NSString *)method delegate:(id<RestRequestDelegate>)delegate {
29    RESTRequest *request = nil;
30    
31    if ([method isEqualToString:@"GET"]) {
32        
33        if ([path isEqualToString:@"/configuration.json"]) {
34            // Package callback
35            request = [[[RESTRequest alloc] init] autorelease];
36            id result = [[SimulatedServer sharedServer] configuration];
37            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
38                                  request, kKeyRequest, 
39                                  delegate, kKeyDelegate,
40                                  result, kKeyResult,
41                                  nil];
42            
43            [self performSelector:@selector(simulatedCallback:) withObject:dict afterDelay:1.0];
44        }
45    }
46    
47    if ([method isEqualToString:@"POST"]) {
48        
49        if ([path isEqualToString:@"/configuration.json"]) {
50            // Package callback
51            request = [[[RESTRequest alloc] init] autorelease];
52            NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:@"ok", @"status", nil];
53            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
54                                  request, kKeyRequest, 
55                                  delegate, kKeyDelegate,
56                                  result, kKeyResult,
57                                  nil];
58            
59            // Update configuration on server
60            NSDictionary *config = [NSDictionary dictionaryWithDictionary:[User currentUser].configuration.configurationDictionary];
61            [[SimulatedServer sharedServer] performSelector:@selector(setConfiguration:) withObject:config afterDelay:0.5];
62            
63            [self performSelector:@selector(simulatedCallback:) withObject:dict afterDelay:1.0];
64        }
65        
66    }
67    
68    return request;
69}
70
71// Performs a simulated callback on the delegate.
72- (void)simulatedCallback:(NSDictionary *)dict {
73    RESTRequest *request = [dict objectForKey:kKeyRequest];
74    id<RestRequestDelegate> delegate = [dict objectForKey:kKeyDelegate];
75    id result = [dict objectForKey:kKeyResult];
76    
77    [delegate restRequest:request didLoadResult:result];
78}
79
80- (NSDictionary *)testConfiguration {
81    return [NSDictionary dictionaryWithObjectsAndKeys:
82            @"paused", @"MediaState",
83            nil];
84}
85
86@end