33 #import <ActiveSupportKit/ActiveSupportKit.h>
49 + (void)setDefaultConnectionClass:(Class)aClass
60 [
self setTimeout:60.0];
70 - (id)initWithSite:(NSURL *)site
80 - (id)initWithSite:(NSURL *)site elementName:(NSString *)elementName
85 [
self setElementName:elementName];
90 - (
ARService *)serviceForSubelementNamed:(NSString *)elementName
101 #pragma mark Schema and Known Attributes
104 @synthesize schema = _schema;
108 return [[
self schema] allKeys];
115 @synthesize site = _site;
126 @synthesize format = _format;
143 @synthesize timeout = _timeout;
146 #pragma mark Connection
154 [
self setConnection:[[[[
self class] defaultConnectionClass] alloc] init]];
161 [connection setSite:[
self site]];
163 [connection setTimeout:[
self timeout]];
171 @synthesize headers = _headers;
178 [
self setHeaders:headers = [NSMutableDictionary dictionary]];
184 #pragma mark Element and Collection Names
214 #pragma mark Primary and Foreign Key
231 return [[ASInflector defaultInflector] foreignKey:[
self elementNameLazily] separateClassNameAndIDWithUnderscore:YES];
255 [
self setPrefixSource:prefixSource];
260 - (NSString *)prefixWithOptions:(NSDictionary *)options
274 return [[NSRegularExpression regularExpressionWithPattern:@":(\\w+)" options:0 error:NULL] stringByReplacingMatchesInString:[
self prefixSourceLazily] replacementStringForResult:^NSString *(NSTextCheckingResult *result, NSString *inString, NSInteger offset) {
275 return [[[options objectForKey:[[result regularExpression] replacementStringForResult:result inString:inString offset:offset template:@"$1"]] description] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
283 - (NSString *)elementPathForID:(NSNumber *)ID prefixOptions:(NSDictionary *)prefixOptions queryOptions:(NSDictionary *)queryOptions
285 if (queryOptions == nil)
289 NSString *IDString = [[ID stringValue] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
296 - (NSString *)newElementPathWithPrefixOptions:(NSDictionary *)prefixOptions
301 - (NSString *)collectionPathWithPrefixOptions:(NSDictionary *)prefixOptions queryOptions:(NSDictionary *)queryOptions
303 if (queryOptions == nil)
311 #pragma mark RESTful Services
326 if (object && error == nil)
328 if ([object isKindOfClass:[NSDictionary class]])
330 NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithDictionary:object];
331 [attrs addEntriesFromDictionary:attributes];
332 completionHandler(response, [[
ARResource alloc] initWithService:self attributes:attrs], nil);
341 completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
346 completionHandler(response, object, error);
355 completionHandler(response, error == nil ?
resource : nil, error);
367 completionHandler(response, resources && [resources count] ? [resources objectAtIndex:0] : nil, error);
374 completionHandler(response, resources && [resources count] ? [resources lastObject] : nil, error);
380 NSDictionary *prefixOptions = nil;
381 NSDictionary *queryOptions = nil;
385 if (object && error == nil)
387 if ([object isKindOfClass:[NSDictionary class]])
393 completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
398 completionHandler(response, object, error);
405 NSString *from = [options objectForKey:ARFromKey];
406 if (from && [from isKindOfClass:[NSString
class]])
408 NSString *path = [NSString stringWithFormat:@"%@%@", from, ARQueryStringForOptions([options objectForKey:ARParamsKey])];
410 if (object && error == nil)
412 if ([object isKindOfClass:[NSDictionary class]])
418 completionHandler(response, nil, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
423 completionHandler(response, object, error);
429 - (void)deleteWithID:(NSNumber *)ID options:(NSDictionary *)options completionHandler:(
void (^)(
ARHTTPResponse *response, NSError *error))completionHandler
433 completionHandler(response, error);
437 - (void)existsWithID:(NSNumber *)ID options:(NSDictionary *)options completionHandler:(
void (^)(
ARHTTPResponse *response, BOOL exists, NSError *error))completionHandler
444 NSDictionary *prefixOptions = nil;
445 NSDictionary *queryOptions = nil;
449 completionHandler(response, [response code] == 200, error);
454 completionHandler(nil, NO, nil);