Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mirek/nsmutabledictionary-rest.framework
REST category for NSMutableDictionary - the way it should be done.
https://github.com/mirek/nsmutabledictionary-rest.framework
Last synced: about 1 month ago
JSON representation
REST category for NSMutableDictionary - the way it should be done.
- Host: GitHub
- URL: https://github.com/mirek/nsmutabledictionary-rest.framework
- Owner: mirek
- Created: 2010-06-06T21:49:22.000Z (over 14 years ago)
- Default Branch: master
- Last Pushed: 2010-06-16T21:38:18.000Z (over 14 years ago)
- Last Synced: 2024-05-09T16:18:11.448Z (8 months ago)
- Language: Objective-C
- Homepage:
- Size: 109 KB
- Stars: 24
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.mkdn
Awesome Lists containing this project
README
# NSMutableDictionary+REST.framework
REST support for Objective-C `NSMutableDictionary` (libxml2 SAX2 based - yes it works with iPhone/iPad).
This project is a part of [Safari Books Online Learn Something New Team Challenge](http://safaribooksonline.wordpress.com/2010/06/08/learn-something-new-team-challenge-gitpad-by-mirek-rusin/).
Feel free to contribute to become a team member, and who knows we may [win an iPad for you](http://www.safaribooksonline.com/Corporate/learnteamchallenge/?cid=2010_06_blog_learnsomethingnewteamchallenge).Easy to use a/synchronous interface:
// Example usage:
NSString *stringUrl = @"http://github.com/api/v2/xml/user/search/chacon";
NSURL *url = [NSURL URLWithString: stringUrl];
NSLog(@"%@", [NSMutableDictionary dictionaryWithRESTContentsOfURL: url]);
// ...will output something like this:
{
users = (
{
created = "2008-04-26T15:37:59Z";
followers = 1;
fullname = "Mirek Rusin";
id = "user-8561";
language = "C++";
location = London;
name = mirek;
pushed = "2010-05-23T21:16:50.272Z";
repos = 8;
score = "15.604";
type = user;
username = mirek;
},
{
created = "2009-10-16T16:38:48Z";
followers = 0;
fullname = "Mirek Rusin";
id = "user-140734";
language = Ruby;
location = London;
name = mirekrusin;
pushed = "2010-05-24T02:16:19.737Z";
repos = 1;
score = "2.978005";
type = user;
username = mirekrusin;
},
{
created = "2010-04-22T14:28:13Z";
followers = 0;
fullname = "Mirek Mencel";
id = "user-249912";
language = "";
location = "Wroc\U0142aw";
name = mirekm;
pushed = "2010-05-25T02:20:23.464Z";
repos = 0;
score = "2.978005";
type = user;
username = mirekm;
}
);
}
Can you spot rails'ish conversion style? The XML looks like this:
mirek
London
1
Mirek Rusin
C++
mirek
user-8561
8
user
2010-05-23T21:16:50.272Z
15.603998
2008-04-26T15:37:59Z
mirekrusin
London
0
Mirek Rusin
Ruby
mirekrusin
user-140734
1
user
2010-05-24T02:16:19.737Z
2.9780054
2009-10-16T16:38:48Z
mirekm
Wrocław
0
Mirek Mencel
mirekm
user-249912
0
user
2010-05-25T02:20:23.464Z
2.9780054
2010-04-22T14:28:13Z
## Async and sync at the same time?
Yes, look at this code:
NSString *stringUrl = @"http://github.com/api/v2/xml/user/search/mirek";
NSURL *url = [NSURL URLWithString: stringUrl];// Delegate gets objects asynchronously and constructor returns synchronously
NSDictionary *users = [NSMutableDictionary dictionaryWithRESTContentsOfURL: url
delegate: [[UserPrinter alloc] init]];
// Above method returned synchronously
printf("Total users: %i\n", (int)[[users objectForKey: @"users"] count]);...where a `delegate` object is defined as:
@interface UserPrinter : NSObject
@end@implementation UserPrinter
- (void) didFinishElement: (id) element withPath: (NSString *) path {
if ([path isEqualToString: @"/users/user"]) {
// Yielding users asynchronously
NSDictionary *user = (NSDictionary *)element;
printf("- %s (%s)\n", [[user objectForKey: @"fullname"] UTF8String],
[[user objectForKey: @"username"] UTF8String]);
}
}@end
The method returns synchronously and delegate receives messages asynchronously.## POST support
To POST the first user you could write something like this:
id user = [[users objectForKey: @"users"] objectAtIndex: 0];
[user postRESTWithURL: @"http://localhost:3100/raw_post"];`localhost:3100` runs included `restapp` example/test application.
Default encoding type is `multipart/form-data` so you can send images etc (as `NSData` objects):
NSURL *url = [NSURL URLWithString: @"http://www.google.co.uk//intl/en_com/images/srpr/logo1w.png"];
NSData *image = [NSData dataWithContentsOfURL: url];
id post = [NSMutableDictionary dictionaryWithObject: image
forKey: @"uploaded_data"];
[post postRESTWithURL: @"http://localhost:3100/raw_post"];Refer to the source code on how to set `application/x-www-form-urlencoded` and `multipart/form-data` encoding types explicitly.
## License
© 2010 Mirek Rusin, Released under the Apache License, Version 2.0
http://www.apache.org/licenses/LICENSE-2.0## Authors
* Mirek Rusin