/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. #import "RESTClientSimulator.h"
  9. #import "SimulatedServer.h"
  10. #import "User.h"
  11. #define kKeyRequest @"request"
  12. #define kKeyDelegate @"delegate"
  13. #define kKeyResult @"result"
  14. @implementation RESTClientSimulator
  15. + (RESTClientSimulator *)sharedClientSimulator
  16. {
  17. static dispatch_once_t once;
  18. static RESTClientSimulator *sharedClient;
  19. dispatch_once(&once, ^ {sharedClient = [[RESTClientSimulator alloc] init];});
  20. return sharedClient;
  21. }
  22. // Performs a simulated callback that tries to immitate the servers response.
  23. - (RESTRequest *)simulatedRequestWithPath:(NSString *)path httpMethod:(NSString *)method delegate:(id<RestRequestDelegate>)delegate {
  24. RESTRequest *request = nil;
  25. if ([method isEqualToString:@"GET"]) {
  26. if ([path isEqualToString:@"/configuration.json"]) {
  27. // Package callback
  28. request = [[[RESTRequest alloc] init] autorelease];
  29. id result = [[SimulatedServer sharedServer] configuration];
  30. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  31. request, kKeyRequest,
  32. delegate, kKeyDelegate,
  33. result, kKeyResult,
  34. nil];
  35. [self performSelector:@selector(simulatedCallback:) withObject:dict afterDelay:1.0];
  36. }
  37. }
  38. if ([method isEqualToString:@"POST"]) {
  39. if ([path isEqualToString:@"/configuration.json"]) {
  40. // Package callback
  41. request = [[[RESTRequest alloc] init] autorelease];
  42. NSDictionary *result = [NSDictionary dictionaryWithObjectsAndKeys:@"ok", @"status", nil];
  43. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
  44. request, kKeyRequest,
  45. delegate, kKeyDelegate,
  46. result, kKeyResult,
  47. nil];
  48. // Update configuration on server
  49. NSDictionary *config = [NSDictionary dictionaryWithDictionary:[User currentUser].configuration.configurationDictionary];
  50. [[SimulatedServer sharedServer] performSelector:@selector(setConfiguration:) withObject:config afterDelay:0.5];
  51. [self performSelector:@selector(simulatedCallback:) withObject:dict afterDelay:1.0];
  52. }
  53. }
  54. return request;
  55. }
  56. // Performs a simulated callback on the delegate.
  57. - (void)simulatedCallback:(NSDictionary *)dict {
  58. RESTRequest *request = [dict objectForKey:kKeyRequest];
  59. id<RestRequestDelegate> delegate = [dict objectForKey:kKeyDelegate];
  60. id result = [dict objectForKey:kKeyResult];
  61. [delegate restRequest:request didLoadResult:result];
  62. }
  63. - (NSDictionary *)testConfiguration {
  64. return [NSDictionary dictionaryWithObjectsAndKeys:
  65. @"paused", @"MediaState",
  66. nil];
  67. }
  68. @end