Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Basic repository retrieval and XML parsing works.

  • Loading branch information...
commit aecd1e5760ea5d8942a48d8bc850cadbff283389 1 parent 6a28467
Owain R Hunt authored
View
5 UAGithubEngine.h
@@ -9,8 +9,9 @@
#import <Cocoa/Cocoa.h>
#import "UAGithubEngineDelegate.h"
#import "UAGithubEngineRequestTypes.h"
+#import "UAGithubParserDelegate.h"
-@interface UAGithubEngine : NSObject {
+@interface UAGithubEngine : NSObject <UAGithubParserDelegate> {
id <UAGithubEngineDelegate> delegate;
NSString *username;
NSString *apiKey;
@@ -28,7 +29,7 @@
#pragma mark Repositories
-- (id)getRepositoriesForUser:(NSString *)aUser includeWatched:(BOOL)watched;
+- (void)getRepositoriesForUser:(NSString *)aUser includeWatched:(BOOL)watched;
- (id)getRepository:(NSString *)repositoryPath;
View
29 UAGithubEngine.m
@@ -8,6 +8,7 @@
#import "UAGithubEngine.h"
#import "UAGithubEngineRequestTypes.h"
+#import "UAGithubRepositoriesParser.h"
@implementation UAGithubEngine
@@ -68,7 +69,8 @@ - (NSData *)sendRequest:(NSString *)path withParameters:(NSDictionary *)params
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSURLResponse *response;
NSError *error;
- return [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
+ NSData *theData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
+ return theData;
}
@@ -76,9 +78,12 @@ - (NSData *)sendRequest:(NSString *)path withParameters:(NSDictionary *)params
#pragma mark Repositories
-- (id)getRepositoriesForUser:(NSString *)aUser includeWatched:(BOOL)watched
+- (void)getRepositoriesForUser:(NSString *)aUser includeWatched:(BOOL)watched
{
- return [self sendRequest:[NSString stringWithFormat:@"repos/%@/%@", (watched ? @"watched" : @"show"), aUser] withParameters:nil];
+ [[UAGithubRepositoriesParser alloc]
+ initWithXML:[self sendRequest:[NSString stringWithFormat:@"repos/%@/%@", (watched ? @"watched" : @"show"), aUser] withParameters:nil]
+ delegate:self
+ requestType:UAGithubRepositoriesRequest];
}
@@ -203,9 +208,19 @@ - (id)getUser:(NSString *)user
}
-/*
-
-
- */
+
+#pragma mark Parser Delegate Methods
+
+- (void)parsingSucceededForRequestOfType:(UAGithubRequestType)requestType withParsedObjects:(NSArray *)parsedObjects
+{
+
+}
+
+
+- (void)parsingFailedForRequestOfType:(UAGithubRequestType)requestType withError:(NSError *)parseError
+{
+
+}
+
@end
View
2  UAGithubEngine.xcodeproj/project.pbxproj
@@ -144,6 +144,7 @@
68E72AD8116652070069B287 /* XMLParsers */ = {
isa = PBXGroup;
children = (
+ 685261E1116A774C003AE003 /* UAGithubParserDelegate.h */,
685261DE116A771B003AE003 /* UAGithubXMLParser.h */,
685261DF116A771B003AE003 /* UAGithubXMLParser.m */,
68E72ACA116651EF0069B287 /* UAGithubRepositoriesParser.h */,
@@ -156,7 +157,6 @@
68E72AC8116651D80069B287 /* UAGithubLabelsParser.m */,
68E72ABC116651970069B287 /* UAGithubUsersParser.h */,
68E72ABD116651970069B287 /* UAGithubUsersParser.m */,
- 685261E1116A774C003AE003 /* UAGithubParserDelegate.h */,
);
name = XMLParsers;
sourceTree = "<group>";
View
1  UAGithubEngineRequestTypes.h
@@ -12,4 +12,5 @@ typedef enum UAGithubRequestType {
UAGithubAllIssuesRequest = 0,
UAGithubOpenIssuesRequest = 1,
UAGithubClosedIssuesRequest = 2,
+ UAGithubRepositoriesRequest = 3,
} UAGithubRequestType;
View
3  UAGithubParserDelegate.h
@@ -7,9 +7,12 @@
//
#import <Cocoa/Cocoa.h>
+#import "UAGithubEngineRequestTypes.h"
@protocol UAGithubParserDelegate
+- (void)parsingSucceededForRequestOfType:(UAGithubRequestType)requestType withParsedObjects:(NSArray *)parsedObjects;
+- (void)parsingFailedForRequestOfType:(UAGithubRequestType)requestType withError:(NSError *)parseError;
@end
View
3  UAGithubRepositoriesParser.h
@@ -7,9 +7,10 @@
//
#import <Cocoa/Cocoa.h>
+#import "UAGithubXMLParser.h"
-@interface UAGithubRepositoriesParser : NSObject {
+@interface UAGithubRepositoriesParser : UAGithubXMLParser {
}
View
58 UAGithubRepositoriesParser.m
@@ -11,4 +11,62 @@
@implementation UAGithubRepositoriesParser
+- (id)initWithXML:(NSData *)theXML delegate:(id)theDelegate requestType:(UAGithubRequestType)reqType {
+
+ if (self = [super initWithXML:theXML delegate:theDelegate requestType:reqType])
+ {
+ numberElements = [NSArray arrayWithObjects:@"watchers", @"forks", @"open-issues", nil];
+ boolElements = [NSArray arrayWithObjects:@"has-issues", @"has-downloads", @"fork", @"has-wiki", @"private", nil];
+ }
+
+ [parser parse];
+
+ return self;
+}
+
+
+- (void)parser:(NSXMLParser *)theParser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {
+ self.lastOpenedElement = elementName;
+
+ if ([elementName isEqualToString:@"repository"]) {
+ // Make new entry in parsedObjects.
+ NSMutableDictionary *newNode = [NSMutableDictionary dictionaryWithCapacity:0];
+ [parsedObjects addObject:newNode];
+ currentNode = newNode;
+ } else if (currentNode) {
+ // Create relevant name-value pair.
+ [currentNode setObject:[NSMutableString string] forKey:elementName];
+ }
+
+}
+
+
+- (void)parser:(NSXMLParser *)theParser foundCharacters:(NSString *)characters
+{
+ if (lastOpenedElement && currentNode) {
+ [[currentNode objectForKey:lastOpenedElement] appendString:characters];
+ }
+}
+
+
+- (void)parser:(NSXMLParser *)theParser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
+{
+ self.lastOpenedElement = nil;
+
+ if ([numberElements containsObject:elementName])
+ {
+ [currentNode setValue:[NSNumber numberWithInt:[[currentNode objectForKey:elementName] intValue]] forKey:elementName];
+ }
+ else if ([boolElements containsObject:elementName])
+ {
+ [currentNode setObject:[NSNumber numberWithBool:[[currentNode objectForKey:elementName] isEqualToString:@"true"]] forKey:elementName];
+ }
+ else if ([elementName isEqualToString:@"repository"])
+ {
+ currentNode = nil;
+ }
+
+}
+
+
@end
View
1  UAGithubUsersParser.h
@@ -10,6 +10,7 @@
@interface UAGithubUsersParser : NSObject {
+
}
View
18 UAGithubXMLParser.h
@@ -7,10 +7,28 @@
//
#import <Cocoa/Cocoa.h>
+#import "UAGithubParserDelegate.h"
+#import "UAGithubEngineRequestTypes.h"
@interface UAGithubXMLParser : NSObject {
+ id <UAGithubParserDelegate> delegate;
+ NSString *identifier;
+ UAGithubRequestType requestType;
+ NSData *xml;
+ NSMutableArray *parsedObjects;
+ NSXMLParser *parser;
+ __weak NSMutableDictionary *currentNode;
+ NSString *lastOpenedElement;
+
+ NSArray *numberElements;
+ NSArray *boolElements;
+ NSArray *parentElements;
}
+@property (nonatomic, retain) NSString *lastOpenedElement;
+
+- (id)initWithXML:(NSData *)theXML delegate:(id)theDelegate requestType:(UAGithubRequestType)reqType;
+
@end
View
38 UAGithubXMLParser.m
@@ -7,8 +7,46 @@
//
#import "UAGithubXMLParser.h"
+#import "UAGithubParserDelegate.h"
@implementation UAGithubXMLParser
+@synthesize lastOpenedElement;
+
+- (void)dealloc
+{
+ [lastOpenedElement release];
+ [super dealloc];
+
+}
+
+
+- (id)initWithXML:(NSData *)theXML delegate:(id)theDelegate requestType:(UAGithubRequestType)reqType
+{
+ if (self = [super init]) {
+ xml = [theXML retain];
+ requestType = reqType;
+ delegate = theDelegate;
+ parsedObjects = [[NSMutableArray alloc] initWithCapacity:0];
+
+ parser = [[NSXMLParser alloc] initWithData:xml];
+ [parser setDelegate:self];
+ [parser setShouldReportNamespacePrefixes:NO];
+ [parser setShouldProcessNamespaces:NO];
+ [parser setShouldResolveExternalEntities:NO];
+
+
+ }
+ return self;
+}
+
+
+- (void)parserDidEndDocument:(NSXMLParser *)theParser
+{
+ NSLog(@"Parsing complete: %@", parsedObjects);
+ [delegate parsingSucceededForRequestOfType:requestType withParsedObjects:parsedObjects];
+}
+
+
@end
Please sign in to comment.
Something went wrong with that request. Please try again.