ActiveResourceKit  v1.2 (498.0)
 All Classes Files Functions Variables Typedefs Enumerator Properties Macros Pages
ARResource+Private.m
Go to the documentation of this file.
1 // ActiveResourceKit ARResource+Private.m
2 //
3 // Copyright © 2011, 2012, Roy Ratcliffe, Pioneering Software, United Kingdom
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EITHER
16 // EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO
18 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
19 // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22 //
23 //------------------------------------------------------------------------------
24 
25 #import "ARResource+Private.h"
26 #import "ARService+Private.h"
27 #import "ARHTTPResponse.h"
28 #import "ARErrors.h"
29 
30 NSNumber *ARIDFromResponse(ARHTTPResponse *response)
31 {
32  NSString *location = [[response headerFields] objectForKey:@"Location"];
33  if (location == nil)
34  {
35  return nil;
36  }
37  NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:@"\\/([^\\/]*?)(\\.\\w+)?$" options:0 error:NULL];
38  NSTextCheckingResult *match = [re firstMatchInString:location options:0 range:NSMakeRange(0, [location length])];
39  if (match == nil || [match numberOfRanges] < 2)
40  {
41  return nil;
42  }
43  NSString *string = [location substringWithRange:[match rangeAtIndex:1]];
44  NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
45  return [numberFormatter numberFromString:string];
46 }
47 
48 BOOL ARResponseCodeAllowsBody(NSInteger code)
49 {
50  return !((100 <= code && code <= 199) || code == 204 || code == 304);
51 }
52 
53 @implementation ARResource(Private)
54 
55 - (void)updateWithCompletionHandler:(void (^)(ARHTTPResponse *response, NSError *error))completionHandler
56 {
57  NSString *path = [[self serviceLazily] elementPathForID:[self ID] prefixOptions:[self prefixOptions] queryOptions:nil];
58  [[self serviceLazily] put:path body:[self encode] completionHandler:^(ARHTTPResponse *response, id attributes, NSError *error) {
59  if (attributes)
60  {
61  if ([attributes isKindOfClass:[NSDictionary class]])
62  {
63  [self loadAttributesFromResponse:response attributes:attributes];
64  completionHandler(response, nil);
65  }
66  else
67  {
68  completionHandler(response, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
69  }
70  }
71  else
72  {
73  completionHandler(response, error);
74  }
75  }];
76 }
77 
78 - (void)createWithCompletionHandler:(void (^)(ARHTTPResponse *response, NSError *error))completionHandler
79 {
80  NSString *path = [[self serviceLazily] collectionPathWithPrefixOptions:nil queryOptions:nil];
81  [[self serviceLazily] post:path body:[self encode] completionHandler:^(ARHTTPResponse *response, id attributes, NSError *error) {
82  if (attributes)
83  {
84  if ([attributes isKindOfClass:[NSDictionary class]])
85  {
86  [self setID:ARIDFromResponse(response)];
87  [self loadAttributesFromResponse:response attributes:attributes];
88  completionHandler(response, nil);
89  }
90  else
91  {
92  completionHandler(response, [NSError errorWithDomain:ARErrorDomain code:ARUnsupportedRootObjectTypeError userInfo:nil]);
93  }
94  }
95  else
96  {
97  completionHandler(response, error);
98  }
99  }];
100 }
101 
102 - (void)loadAttributesFromResponse:(ARHTTPResponse *)response attributes:(NSDictionary *)attributes
103 {
104  NSDictionary *headerFields;
105  NSString *contentLength;
106 
107  if (ARResponseCodeAllowsBody([response code]) &&
108  ((contentLength = [headerFields = [response headerFields] objectForKey:@"Content-Length"]) == nil || ![contentLength isEqualToString:@"0"]))
109  {
110  [self loadAttributes:attributes removeRoot:YES];
111  [self setPersisted:YES];
112  }
113 }
114 
115 - (NSString *)elementPathWithOptions:(NSDictionary *)options
116 {
117  return [[self serviceLazily] elementPathForID:[self ID] prefixOptions:options ? options : [self prefixOptions] queryOptions:nil];
118 }
119 
120 //------------------------------------------------------------------------------
121 #pragma mark Active Model Conversions
122 //------------------------------------------------------------------------------
123 
124 - (NSArray *)toKey
125 {
126  return [self persisted] ? [NSArray arrayWithObject:[self ID]] : nil;
127 }
128 
129 - (NSString *)toParam
130 {
131  // How does -componentsJoinedByString:aString handle non-string elements?
132  return [self persisted] ? [[self toKey] componentsJoinedByString:@"-"] : nil;
133 }
134 
135 @end