From 8d74807b2304b732cd70c74e6e44a795b29c4a4d Mon Sep 17 00:00:00 2001 From: Jake Petroules Date: Wed, 27 Aug 2014 20:53:27 -0400 Subject: [PATCH] Use string constants for RSS element and attribute names. --- Sparkle/SUAppcast.m | 8 ++++---- Sparkle/SUAppcastItem.m | 22 +++++++++++----------- Sparkle/SUConstants.h | 8 ++++++++ Sparkle/SUConstants.m | 8 ++++++++ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 18a6c6c0..2bfaedcc 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -145,14 +145,14 @@ for (NSString *name in nodesDict) { node = [self bestNodeInNodes:nodesDict[name]]; - if ([name isEqualToString:@"enclosure"]) + if ([name isEqualToString:SURSSElementEnclosure]) { // enclosure is flattened as a separate dictionary for some reason NSDictionary *encDict = [(NSXMLElement *)node attributesAsDictionary]; - dict[@"enclosure"] = encDict; + dict[name] = encDict; } - else if ([name isEqualToString:@"pubDate"]) + else if ([name isEqualToString:SURSSElementPubDate]) { // pubDate is expected to be an NSDate by SUAppcastItem, but the RSS class was returning an NSString NSDate *date = [NSDate dateWithNaturalLanguageString:[node stringValue]]; @@ -164,7 +164,7 @@ NSMutableArray *deltas = [NSMutableArray array]; NSEnumerator *childEnum = [[node children] objectEnumerator]; for (NSXMLNode *child in childEnum) { - if ([[child name] isEqualToString:@"enclosure"]) + if ([[child name] isEqualToString:SURSSElementEnclosure]) [deltas addObject:[(NSXMLElement *)child attributesAsDictionary]]; } dict[name] = deltas; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 7e776679..8a8950e5 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -47,7 +47,7 @@ - (BOOL)isDeltaUpdate { - return self.propertiesDictionary[@"enclosure"][SUAppcastAttributeDeltaFrom] != nil; + return self.propertiesDictionary[SURSSElementEnclosure][SUAppcastAttributeDeltaFrom] != nil; } - (BOOL)isCriticalUpdate @@ -64,7 +64,7 @@ { self = [super init]; if (self) { - id enclosure = dict[@"enclosure"]; + id enclosure = dict[SURSSElementEnclosure]; // Try to find a version string. // Finding the new version number from the RSS feed is a little bit hacky. There are two ways: @@ -80,11 +80,11 @@ } if (newVersion == nil) // no sparkle:version attribute anywhere? { - SULog(@"warning: for URL '%@' is missing sparkle:version attribute. Version comparison may be unreliable. Please always specify sparkle:version", enclosure[@"url"]); + SULog(@"warning: <%@> for URL '%@' is missing %@ attribute. Version comparison may be unreliable. Please always specify %@", SURSSElementEnclosure, enclosure[SURSSAttributeURL], SUAppcastAttributeVersion, SUAppcastAttributeVersion); // Separate the url by underscores and take the last component, as that'll be closest to the end, // then we remove the extension. Hopefully, this will be the version. - NSArray *fileComponents = [enclosure[@"url"] componentsSeparatedByString:@"_"]; + NSArray *fileComponents = [enclosure[SURSSAttributeURL] componentsSeparatedByString:@"_"]; if ([fileComponents count] > 1) { newVersion = [[fileComponents lastObject] stringByDeletingPathExtension]; } @@ -92,17 +92,17 @@ if (!newVersion) { if (error) { - *error = @"Feed item lacks sparkle:version attribute, and version couldn't be deduced from file name (would have used last component of a file name like AppName_1.3.4.zip)"; + *error = [NSString stringWithFormat:@"Feed item lacks %@ attribute, and version couldn't be deduced from file name (would have used last component of a file name like AppName_1.3.4.zip)", SUAppcastAttributeVersion]; } return nil; } propertiesDictionary = [[NSMutableDictionary alloc] initWithDictionary:dict]; - self.title = dict[@"title"]; - self.date = dict[@"pubDate"]; - self.itemDescription = dict[@"description"]; + self.title = dict[SURSSElementTitle]; + self.date = dict[SURSSElementPubDate]; + self.itemDescription = dict[SURSSElementDescription]; - NSString *theInfoURL = dict[@"link"]; + NSString *theInfoURL = dict[SURSSElementLink]; if (theInfoURL) { if (![theInfoURL isKindOfClass:[NSString class]]) { SULog(@"SUAppcastItem -initWithDictionary: Info URL is not of valid type."); @@ -120,7 +120,7 @@ return nil; } - NSString *enclosureURLString = enclosure[@"url"]; + NSString *enclosureURLString = enclosure[SURSSAttributeURL]; if (!enclosureURLString && !theInfoURL) { if (error) { *error = @"Feed item's enclosure lacks URL"; @@ -166,7 +166,7 @@ for (NSDictionary *deltaDictionary in deltaDictionaries) { NSMutableDictionary *fakeAppCastDict = [dict mutableCopy]; [fakeAppCastDict removeObjectForKey:SUAppcastElementDeltas]; - fakeAppCastDict[@"enclosure"] = deltaDictionary; + fakeAppCastDict[SURSSElementEnclosure] = deltaDictionary; SUAppcastItem *deltaItem = [[[self class] alloc] initWithDictionary:fakeAppCastDict]; deltas[deltaDictionary[SUAppcastAttributeDeltaFrom]] = deltaItem; diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index acc1a03c..d9d7887b 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -71,6 +71,14 @@ extern NSString *const SUAppcastElementMaximumSystemVersion; extern NSString *const SUAppcastElementReleaseNotesLink; extern NSString *const SUAppcastElementTags; +extern NSString *const SURSSAttributeURL; + +extern NSString *const SURSSElementDescription; +extern NSString *const SURSSElementEnclosure; +extern NSString *const SURSSElementLink; +extern NSString *const SURSSElementPubDate; +extern NSString *const SURSSElementTitle; + // ----------------------------------------------------------------------------- // Errors: // ----------------------------------------------------------------------------- diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 6a37f5bb..31c80ca3 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -63,3 +63,11 @@ NSString *const SUAppcastElementMinimumSystemVersion = @"sparkle:minimumSystemVe NSString *const SUAppcastElementMaximumSystemVersion = @"sparkle:maximumSystemVersion"; NSString *const SUAppcastElementReleaseNotesLink = @"sparkle:releaseNotesLink"; NSString *const SUAppcastElementTags = @"sparkle:tags"; + +NSString *const SURSSAttributeURL = @"url"; + +NSString *const SURSSElementDescription = @"description"; +NSString *const SURSSElementEnclosure = @"enclosure"; +NSString *const SURSSElementLink = @"link"; +NSString *const SURSSElementPubDate = @"pubDate"; +NSString *const SURSSElementTitle = @"title";