Permalink
Browse files

added TKTumblelogsRequest and TKTumblelogsResponse, and some initial …

…tests for it.
  • Loading branch information...
1 parent d06574f commit 1306b5610412c7362a30cc6c782ce85c29b2d66a @isutton committed Dec 16, 2011
View
@@ -0,0 +1,52 @@
+//
+// Copyright (c) 2011 TumblrKit
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#import "TKRequest.h"
+
+@class TKTumblelog;
+@class TKTumblelogsRequest;
+@class TKTumblelogsResponse;
+
+@protocol TKTumblelogsRequestDelegate <NSObject>
+
+- (void)tumblelogsRequest:(TKTumblelogsRequest *)request didReceiveResponse:(TKTumblelogsResponse *)response;
+- (void)tumblelogsRequest:(TKTumblelogsRequest *)request didFailWithError:(NSError *)error;
+
+@end
+
+@interface TKTumblelogsRequest : TKRequest <NSXMLParserDelegate>
+{
+ @private
+ NSDictionary *_options;
+ NSMutableArray *_receivedTumblelogs;
+ NSString *_currentElementName;
+ TKTumblelog *_currentTumblelog;
+}
+
+@property (nonatomic, assign) id<TKTumblelogsRequestDelegate> delegate;
+
+- (id)initWithOptions:(NSDictionary *)options delegate:(id<TKTumblelogsRequestDelegate>)delegate;
+
+@end
+
+extern NSString *TKTumblelogsRequestEmailKey;
+extern NSString *TKTumblelogsRequestPasswordKey;
View
@@ -0,0 +1,131 @@
+//
+// Copyright (c) 2011 TumblrKit
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#import "TKTumblelogsRequest.h"
+#import "TKTumblelogsResponse.h"
+#import "TKTumblelog.h"
+
+@implementation TKTumblelogsRequest
+
+@synthesize delegate = _delegate;
+
+#pragma mark - NSObject
+
+- (void)dealloc;
+{
+ [_options release]; _options = nil;
+ [_receivedTumblelogs release]; _receivedTumblelogs = nil;
+ [super dealloc];
+}
+
+#pragma mark - NSXMLParserDelegate
+
+- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
+{
+ static NSDictionary *elementToSelectorDict = nil;
+
+ if (!elementToSelectorDict) {
+ elementToSelectorDict = [[NSDictionary alloc] initWithObjectsAndKeys:
+ @"setTitle:", @"title",
+ @"setName:",@"name",
+ @"setURLWithString:", @"url",
+ @"setAvatarURLWithString:", @"avatar-url",
+ @"setPrimaryWithString:", @"is-primary",
+ @"setTypeWithString:", @"type",
+ nil];
+ }
+
+ _currentElementName = elementName;
+
+ if ([elementName isEqualToString:@"tumblelog"]) {
+ _currentTumblelog = [[TKTumblelog alloc] init];
+
+ for (NSString *key in [attributeDict keyEnumerator]) {
+ SEL selector = NSSelectorFromString([elementToSelectorDict objectForKey:key]);
+ if (selector && [_currentTumblelog respondsToSelector:selector]) {
+ [_currentTumblelog performSelector:selector withObject:[attributeDict objectForKey:key]];
+ }
+ }
+ }
+}
+
+- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
+{
+ if ([elementName isEqualToString:@"tumblelog"]) {
+ [_receivedTumblelogs addObject:_currentTumblelog];
+ [_currentTumblelog release];
+ }
+}
+
+- (void)parserDidEndDocument:(NSXMLParser *)parser
+{
+ [parser release];
+ [_delegate tumblelogsRequest:self didReceiveResponse:[TKTumblelogsResponse responseWithTumblelogs:_receivedTumblelogs]];
+}
+
+#pragma mark - TKRequest
+
+- (NSURL *)URL;
+{
+ return [NSURL URLWithString:@"http://www.tumblr.com/api/authenticate"];
+}
+
+- (NSData *)HTTPBody;
+{
+ NSString *HTTPBodyString = [NSString stringWithFormat:@"email=%@&password=%@",
+ [_options objectForKey:TKTumblelogsRequestEmailKey],
+ [_options objectForKey:TKTumblelogsRequestPasswordKey],
+ nil];
+ return [HTTPBodyString dataUsingEncoding:NSUTF8StringEncoding];
+}
+
+- (void)connectionDidFinishLoadingData:(NSData *)data;
+{
+ _receivedTumblelogs = [[NSMutableArray alloc] init];
+ NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
+ [parser setDelegate:self];
+ [parser parse];
+}
+
+#pragma mark - API
+
+- (id)initWithOptions:(NSDictionary *)options delegate:(id<TKTumblelogsRequestDelegate>)delegate;
+{
+ if (!(self = [self init]))
+ return nil;
+
+ if (![options objectForKey:TKTumblelogsRequestEmailKey])
+ [NSException raise:@"TKTumblelogsRequestEmailKey is mandatory" format:@"TKTumblelogsRequestEmailKey is mandatory"];
+
+ if (![options objectForKey:TKTumblelogsRequestPasswordKey])
+ [NSException raise:@"TKTumblelogsRequestPasswordKey is mandatory" format:@"TKTumblelogsRequestPasswordKey is mandatory"];
+
+ _options = [options retain];
+ _delegate = delegate;
+
+ return self;
+}
+
+@end
+
+NSString const * TKTumblelogsRequestEmailKey = @"email";
+NSString const * TKTumblelogsRequestPasswordKey = @"password";
@@ -0,0 +1,32 @@
+//
+// Copyright (c) 2011 TumblrKit
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+@interface TKTumblelogsResponse : NSObject
+
+@property (nonatomic, retain) NSArray *tumblelogs;
+
+#pragma mark - API
+
++ (id)responseWithTumblelogs:(NSArray *)tumblelogs;
+- (id)initWithTumblelogs:(NSArray *)tumblelogs;
+
+@end
@@ -0,0 +1,54 @@
+//
+// Copyright (c) 2011 TumblrKit
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#import "TKTumblelogsResponse.h"
+
+@implementation TKTumblelogsResponse
+
+@synthesize tumblelogs = _tumblelogs;
+
+#pragma mark - NSObject
+
+- (void)dealloc;
+{
+ [_tumblelogs release]; _tumblelogs = nil;
+ [super dealloc];
+}
+
+#pragma mark - API
+
++ (id)responseWithTumblelogs:(NSArray *)tumblelogs;
+{
+ return [[[self alloc] initWithTumblelogs:tumblelogs] autorelease];
+}
+
+- (id)initWithTumblelogs:(NSArray *)tumblelogs;
+{
+ if (!(self = [self init]))
+ return nil;
+
+ _tumblelogs = [tumblelogs retain];
+
+ return self;
+}
+
+@end
@@ -8,8 +8,9 @@
#import <SenTestingKit/SenTestingKit.h>
#import "TKPostsRequest.h"
+#import "TKTumblelogsRequest.h"
-@interface TumblrKit_Tests_for_Mac : SenTestCase <TKPostsRequestDelegate>
+@interface TumblrKit_Tests_for_Mac : SenTestCase <TKPostsRequestDelegate,TKTumblelogsRequestDelegate>
{
BOOL _testIsDone;
}
@@ -9,6 +9,8 @@
#import "TumblrKit_Tests_for_Mac.h"
#import "TKPost.h"
#import "TKPostsResponse.h"
+#import "TKTumblelog.h"
+#import "TKTumblelogsResponse.h"
@implementation TumblrKit_Tests_for_Mac
@@ -85,6 +87,20 @@ - (void)testPostsRequest004;
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
}
+- (void)testTumblelogsRequest001;
+{
+ NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
+ @"email", TKTumblelogsRequestEmailKey,
+ @"password", TKTumblelogsRequestPasswordKey,
+ nil];
+
+ TKTumblelogsRequest *request = [[[TKTumblelogsRequest alloc] initWithOptions:options delegate:self] autorelease];
+ [request start];
+
+ while (!_testIsDone)
+ [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
+}
+
#pragma mark - TKPostsRequestDelegate
- (void)postsRequest:(TKPostsRequest *)request didReceiveResponse:(TKPostsResponse *)response;
@@ -109,4 +125,20 @@ - (void)postsRequest:(TKPostsRequest *)request didFailWithError:(NSError *)error
STFail(@"%@", error);
}
+#pragma mark - TKTumblelogsRequestDelegate
+
+- (void)tumblelogsRequest:(TKTumblelogsRequest *)request didReceiveResponse:(TKTumblelogsResponse *)response;
+{
+ _testIsDone = YES;
+
+ NSLog(@"Tumblelogs: %lu", [response.tumblelogs count]);
+}
+
+- (void)tumblelogsRequest:(TKTumblelogsRequest *)request didFailWithError:(NSError *)error;
+{
+ _testIsDone = YES;
+
+ STFail(@"%@", error);
+}
+
@end
Oops, something went wrong. Retry.

0 comments on commit 1306b56

Please sign in to comment.