Permalink
Browse files

Initial Commit

  • Loading branch information...
0 parents commit 841604b1e70da7e926d0a763b0ce8d68da2d4cd1 @mattt mattt committed Jul 13, 2012
@@ -0,0 +1,3 @@
+[submodule "AFNetworking"]
+ path = AFNetworking
+ url = git://github.com/AFNetworking/AFNetworking.git
@@ -0,0 +1,76 @@
+// AFIncrementalStore.h
+//
+// Copyright (c) 2012 Mattt Thompson (http://mattt.me)
+//
+// 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 <CoreData/CoreData.h>
+#import "AFNetworking.h"
+
+extern NSString * AFIncrementalStoreUnimplementedMethodException;
+
+@protocol AFIncrementalStoreHTTPClient <NSObject>
+
+- (id)representationOrArrayOfRepresentationsFromResponseObject:(id)responseObject;
+
+- (NSString *)resourceIdentifierForRepresentation:(NSDictionary *)representation
+ ofEntity:(NSEntityDescription *)entity;
+
+- (NSDictionary *)propertyValuesForRepresentation:(NSDictionary *)representation
+ ofEntity:(NSEntityDescription *)entity
+ fromResponse:(NSHTTPURLResponse *)response;
+
+- (NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest
+ withContext:(NSManagedObjectContext *)context;
+
+- (NSURLRequest *)requestWithMethod:(NSString *)method
+ pathForObjectWithID:(NSManagedObjectID *)objectID
+ withContext:(NSManagedObjectContext *)context;
+
+- (NSURLRequest *)requestWithMethod:(NSString *)method
+ pathForRelationship:(NSRelationshipDescription *)relationship
+ forObjectWithID:(NSManagedObjectID *)objectID
+ withContext:(NSManagedObjectContext *)context;
+
+@optional
+
+- (BOOL)shouldRequestPathForObjectWithID:(NSManagedObjectID *)objectID
+ withContext:(NSManagedObjectContext *)context;
+
+- (BOOL)shouldRequestPathForObjectWithID:(NSManagedObjectID *)objectID
+ forRelationship:(NSRelationshipDescription *)relationship
+ withContext:(NSManagedObjectContext *)context;
+
+@optional
+
+
+
+@end
+
+#pragma mark -
+
+@interface AFIncrementalStore : NSIncrementalStore
+
+@property (nonatomic, strong) AFHTTPClient <AFIncrementalStoreHTTPClient> *HTTPClient;
+
++ (NSString *)type;
+
++ (NSManagedObjectModel *)model;
+
+@end
Oops, something went wrong.
@@ -0,0 +1,43 @@
+// AFRESTClient.h
+//
+// Copyright (c) 2012 Mattt Thompson (http://mattt.me)
+//
+// 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 "AFHTTPClient.h"
+#import "AFHTTPRequestOperation.h"
+#import "AFIncrementalStore.h"
+
+//@protocol AFResource
+//+ (NSString *)resourcesPath;
+//- (NSString *)identifier;
+//@optional
+//- (NSString *)etag;
+//- (NSDate *)timestamp;
+////- (BOOL)isValid:(NSError **)error;
+//@end
+
+@interface AFRESTClient : AFHTTPClient <AFIncrementalStoreHTTPClient>
+
+- (NSString *)pathForEntity:(NSEntityDescription *)entity;
+- (NSString *)pathForObject:(NSManagedObject *)object;
+- (NSString *)pathForRelationship:(NSRelationshipDescription *)relationship
+ forObject:(NSManagedObject *)object;
+
+@end
@@ -0,0 +1,134 @@
+// AFRESTClient.m
+//
+// Copyright (c) 2012 Mattt Thompson (http://mattt.me)
+//
+// 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 "AFRESTClient.h"
+
+static NSString * AFPluralizedString(NSString *string) {
+ if ([string hasSuffix:@"ss"] || [string hasSuffix:@"se"] || [string hasSuffix:@"sh"] || [string hasSuffix:@"ge"] || [string hasSuffix:@"ch"]) {
+ return [[string stringByAppendingString:@"es"] lowercaseString];
+ } else {
+ return [[string stringByAppendingString:@"s"] lowercaseString];
+ }
+}
+
+@implementation AFRESTClient
+
+- (NSString *)pathForEntity:(NSEntityDescription *)entity {
+ return AFPluralizedString(entity.name);
+}
+
+- (NSString *)pathForObject:(NSManagedObject *)object {
+ NSString *resourceIdentifier = [(NSIncrementalStore *)object.objectID.persistentStore referenceObjectForObjectID:object.objectID];
+ return [[self pathForEntity:object.entity] stringByAppendingPathComponent:[resourceIdentifier lastPathComponent]];
+}
+
+- (NSString *)pathForRelationship:(NSRelationshipDescription *)relationship
+ forObject:(NSManagedObject *)object
+{
+ return [[self pathForObject:object] stringByAppendingPathComponent:relationship.name];
+}
+
+#pragma mark - AFIncrementalStoreHTTPClient
+
+- (id)representationOrArrayOfRepresentationsFromResponseObject:(id)responseObject {
+ if ([responseObject isKindOfClass:[NSArray array]]) {
@steveswing
steveswing Aug 13, 2012

Should [NSArray array] be changed to [NSArray class]?

+ return responseObject;
+ } else if ([responseObject isKindOfClass:[NSDictionary dictionary]]) {
@steveswing
steveswing Aug 13, 2012

Should [NSDictionary dictionary] be changed to [NSDictionary class]?

+ // Distinguish between keyed array or individual representation
+ if ([[responseObject allKeys] count] == 1) {
+ id value = [responseObject valueForKey:[[responseObject allKeys] lastObject]];
+ if ([value isKindOfClass:[NSArray class]]) {
+ return value;
+ }
+ }
+
+ return responseObject;
+ }
+
+ return responseObject;
+}
+
+- (NSString *)resourceIdentifierForRepresentation:(NSDictionary *)representation
+ ofEntity:(NSEntityDescription *)entity
+{
+ static NSArray *_candidatePropertyNames = nil;
+ static dispatch_once_t onceToken;
+ dispatch_once(&onceToken, ^{
+ _candidatePropertyNames = [NSArray arrayWithObjects:@"ID", @"resourceIdentifier", @"identifier", @"url", @"URL", @"URLString", nil];
+ });
+
+ NSString *key = [[representation allKeys] firstObjectCommonWithArray:_candidatePropertyNames];
+ if (key) {
+ id value = [representation valueForKey:key];
+ if (value) {
+ return [value description];
+ }
+ }
+
+ return nil;
+}
+
+- (NSDictionary *)propertyValuesForRepresentation:(NSDictionary *)representation
+ ofEntity:(NSEntityDescription *)entity
+ fromResponse:(NSHTTPURLResponse *)response
+{
+ NSMutableDictionary *mutablePropertyValues = [representation mutableCopy];
+ @autoreleasepool {
+ NSMutableSet *mutableKeys = [NSMutableSet setWithArray:[representation allKeys]];
+ [mutableKeys minusSet:[NSSet setWithArray:[[entity propertiesByName] allKeys]]];
+ [mutablePropertyValues removeObjectsForKeys:[mutableKeys allObjects]];
+ }
+
+ return mutablePropertyValues;
+}
+
+- (NSURLRequest *)requestForFetchRequest:(NSFetchRequest *)fetchRequest
+ withContext:(NSManagedObjectContext *)context
+{
+ return [self requestWithMethod:@"GET" path:[self pathForEntity:fetchRequest.entity] parameters:nil];
+}
+
+- (NSURLRequest *)requestWithMethod:(NSString *)method
+ pathForObjectWithID:(NSManagedObjectID *)objectID
+ withContext:(NSManagedObjectContext *)context
+{
+ NSManagedObject *object = [context objectWithID:objectID];
+ return [self requestWithMethod:method path:[self pathForObject:object] parameters:nil];
+}
+
+- (NSURLRequest *)requestWithMethod:(NSString *)method
+ pathForRelationship:(NSRelationshipDescription *)relationship
+ forObjectWithID:(NSManagedObjectID *)objectID
+ withContext:(NSManagedObjectContext *)context
+{
+ NSManagedObject *object = [context objectWithID:objectID];
+ return [self requestWithMethod:method path:[self pathForRelationship:relationship forObject:object] parameters:nil];
+}
+
+#pragma mark - AFHTTPClient
+
+- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
+ [self cancelAllHTTPOperationsWithMethod:operation.request.HTTPMethod path:operation.request.URL.path];
+ [super enqueueHTTPRequestOperation:operation];
+}
+
+@end
Submodule AFNetworking added at 5dc7be
@@ -0,0 +1,36 @@
+// AppDelegate.h
+//
+// Copyright (c) 2012 Mattt Thompson (http://mattt.me)
+//
+// 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 <UIKit/UIKit.h>
+
+@interface AppDelegate : UIResponder <UIApplicationDelegate>
+
+@property (strong, nonatomic) UIWindow *window;
+@property (strong, nonatomic) UINavigationController *navigationController;
+@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
+@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
+@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
+
+- (void)saveContext;
+- (NSURL *)applicationDocumentsDirectory;
+
+@end
Oops, something went wrong.

0 comments on commit 841604b

Please sign in to comment.