29 #import <ActiveModelKit/ActiveModelKit.h>
30 #import <ActiveSupportKit/ActiveSupportKit.h>
37 return options == nil || [options count] == 0 ?
@"" : [NSString stringWithFormat:@"?%@", [options toQueryWithNamespace:nil]];
47 - (NSString *)defaultElementName
49 return [[[AMName alloc] initWithClass:[
self class]] element];
52 - (NSString *)defaultCollectionName
57 - (NSString *)defaultPrimaryKey
62 - (NSString *)defaultPrefixSource
64 return [[
self site] path];
67 - (void)findEveryWithOptions:(NSDictionary *)options completionHandler:(
void (^)(
ARHTTPResponse *response, NSArray *resources, NSError *error))completionHandler
70 NSDictionary *prefixOptions;
71 NSString *from = [options objectForKey:ARFromKey];
72 if (from && [from isKindOfClass:[NSString
class]])
75 path = [NSString stringWithFormat:@"%@%@", from, ARQueryStringForOptions([options objectForKey:ARParamsKey])];
79 NSDictionary *queryOptions = nil;
84 if ([object isKindOfClass:[NSArray class]])
90 completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
95 - (NSArray *)instantiateCollection:(NSArray *)collection prefixOptions:(NSDictionary *)prefixOptions
97 NSMutableArray *resources = [NSMutableArray array];
98 for (NSDictionary *attributes in collection)
102 return [resources copy];
105 - (
ARResource *)instantiateRecordWithAttributes:(NSDictionary *)attributes prefixOptions:(NSDictionary *)prefixOptions
107 Class aClass = NSClassFromString(ASInflectorCamelize([
self elementNameLazily], YES));
108 if (aClass == nil || ![aClass isSubclassOfClass:[
ARResource class]])
112 ARResource *resource = [[aClass alloc] initWithService:self attributes:attributes persisted:YES];
113 [resource setPrefixOptions:prefixOptions];
117 - (NSSet *)prefixParameters
119 NSMutableSet *parameters = [NSMutableSet set];
121 for (NSTextCheckingResult *result in [[NSRegularExpression regularExpressionWithPattern:
@":\\w+" options:0 error:NULL] matchesInString:prefixSource options:0 range:NSMakeRange(0, [prefixSource length])])
123 [parameters addObject:[[
prefixSource substringWithRange:[result range]] substringFromIndex:1]];
125 return [parameters copy];
128 - (void)splitOptions:(NSDictionary *)options prefixOptions:(NSDictionary **)outPrefixOptions queryOptions:(NSDictionary **)outQueryOptions
131 NSMutableDictionary *prefixOptions = [NSMutableDictionary dictionary];
132 NSMutableDictionary *queryOptions = [NSMutableDictionary dictionary];
133 [options enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
134 [[
prefixParameters member:key] != nil ? prefixOptions : queryOptions setObject:obj forKey:key];
136 if (outPrefixOptions)
138 *outPrefixOptions = [prefixOptions copy];
142 *outQueryOptions = [queryOptions copy];
147 #pragma mark HTTP Requests
189 [request setHTTPBody:data];
230 NSData *data = [response body];
231 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
234 data = [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] dataUsingEncoding:NSUTF8StringEncoding];
236 id object = data && [data length] ? [[
self formatLazily] decode:data error:&error] : nil;
239 completionHandler(response, object, nil);
244 completionHandler(response, nil, error);
250 completionHandler(response, nil, error);
256 completionHandler(response, nil, error);
262 completionHandler(nil, nil, error ? error : [NSError errorWithDomain:ARErrorDomain code:ARResponseIsNotHTTPError userInfo:nil]);