Created
February 19, 2012 23:01
-
-
Save dingbat/1866356 to your computer and use it in GitHub Desktop.
Example of NSRConfig subclass with parameterization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface MyConfig : NSRConfig | |
| @end | |
| @implementation MyConfig | |
| - (NSString *) parameterizeDictionary:(NSDictionary *)dict | |
| { | |
| NSMutableString *parameters = [NSMutableString string]; | |
| for (NSString *key in dict) | |
| { | |
| //add an "&" to the beginning of the string if it's not the first key | |
| if (parameters.length > 0) | |
| [parameters appendString:@"&"]; | |
| id object = [dict objectForKey:key]; | |
| if ([object isKindOfClass:[NSDictionary class]]) | |
| { | |
| //recursion if the object is another dict | |
| [parameters appendFormat:[self parameterizeDictionary:object]]; | |
| } | |
| else | |
| { | |
| //add each key + value to the param string | |
| [parameters appendFormat:@"%@=%@", key, object]; | |
| } | |
| } | |
| return parameters; | |
| } | |
| //this is the NSRConfig override... | |
| - (NSString *) makeRequestType:(NSString *)type | |
| requestBody:(NSString *)requestStr | |
| route:(NSString *)route | |
| sync:(NSError **)error | |
| orAsync:(void (^)(NSString *, NSError *))completionBlock | |
| { | |
| //make a dictionary of whatever JSON is passed in and parameterize it | |
| NSDictionary *dictionary = [requestStr JSONValue]; | |
| NSString *parameterized = [self parameterizeDictionary:dictionary]; | |
| //make the originally intended call, only with the parameterized string | |
| return [super makeRequestType:type requestBody:parameterized route:route | |
| sync:error orAsync:completionBlock]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment