-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNSObject+ObjectiveResource.m
363 lines (297 loc) · 11.7 KB
/
NSObject+ObjectiveResource.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// NSObject+ObjectiveResource.m
// objectivesync
//
// Created by vickeryj on 1/29/09.
// Copyright 2009 Joshua Vickery. All rights reserved.
//
#import "NSObject+ObjectiveResource.h"
#import "Connection.h"
#import "Response.h"
#import "CoreSupport.h"
#import "XMLSerializableSupport.h"
#import "JSONSerializableSupport.h"
#import "FormSerializableSupport.h"
static NSString *_activeResourceSite = nil;
static NSString *_activeResourceUser = nil;
static NSString *_activeResourcePassword = nil;
static SEL _activeResourceParseDataMethod = nil;
static SEL _activeResourceSerializeMethod = nil;
static NSString *_activeResourceProtocolExtension = @".xml";
static ORSResponseFormat _format;
@implementation NSObject (ObjectiveResource)
#pragma mark configuration methods
+ (NSString *)getRemoteSite {
return _activeResourceSite;
}
+ (void)setRemoteSite:(NSString *)siteURL {
if (_activeResourceSite != siteURL) {
[_activeResourceSite autorelease];
_activeResourceSite = [siteURL copy];
}
}
+ (NSString *)getRemoteUser {
return _activeResourceUser;
}
+ (void)setRemoteUser:(NSString *)user {
if (_activeResourceUser != user) {
[_activeResourceUser autorelease];
_activeResourceUser = [user copy];
}
}
+ (NSString *)getRemotePassword {
return _activeResourcePassword;
}
+ (void)setRemotePassword:(NSString *)password {
if (_activeResourcePassword != password) {
[_activeResourcePassword autorelease];
_activeResourcePassword = [password copy];
}
}
+ (void)setRemoteResponseType:(ORSResponseFormat) format {
_format = format;
switch (format) {
case JSONResponse:
[[self class] setRemoteProtocolExtension:@".json"];
[[self class] setRemoteParseDataMethod:@selector(fromJSONData:)];
[[self class] setRemoteSerializeMethod:@selector(toJSONExcluding:)];
break;
default:
[[self class] setRemoteProtocolExtension:@".xml"];
[[self class] setRemoteParseDataMethod:@selector(fromXMLData:)];
[[self class] setRemoteSerializeMethod:@selector(toXMLElementExcluding:)];
break;
}
}
+ (ORSResponseFormat)getRemoteResponseType {
return _format;
}
+ (SEL)getRemoteParseDataMethod {
return (nil == _activeResourceParseDataMethod) ? @selector(fromXMLData:) : _activeResourceParseDataMethod;
}
+ (void)setRemoteParseDataMethod:(SEL)parseMethod {
_activeResourceParseDataMethod = parseMethod;
}
+ (SEL) getRemoteSerializeMethod {
return (nil == _activeResourceSerializeMethod) ? @selector(toXMLElementExcluding:) : _activeResourceSerializeMethod;
}
+ (void) setRemoteSerializeMethod:(SEL)serializeMethod {
_activeResourceSerializeMethod = serializeMethod;
}
+ (NSString *)getRemoteProtocolExtension {
return _activeResourceProtocolExtension;
}
+ (void)setRemoteProtocolExtension:(NSString *)protocolExtension {
if (_activeResourceProtocolExtension != protocolExtension) {
[_activeResourceProtocolExtension autorelease];
_activeResourceProtocolExtension = [protocolExtension copy];
}
}
// Find all items
+ (NSArray *)findAllRemoteWithResponse:(NSError **)aError {
NSLog(@"fetch as user: %@", [[self class] getRemoteUser]);
Response *res = [Connection get:[self getRemoteCollectionPath] withUser:[[self class] getRemoteUser] andPassword:[[self class] getRemotePassword]];
if([res isError] && aError) {
*aError = res.error;
return nil;
}
NSString *body = [[[NSString alloc] initWithData:res.body encoding:NSUTF8StringEncoding] autorelease];
NSString *withoutheader = [body stringByReplacingOccurrencesOfString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" withString:@""];
//NSLog(@"%d", [withoutheader compare:@"<nil-classes type=\"array\"/>\n"]);
if ([withoutheader compare:@"<nil-classes type=\"array\"/>\n"] == NSOrderedSame){
return nil; //empty array
}
id data = [self performSelector:[self getRemoteParseDataMethod] withObject:res.body];
if ([data isKindOfClass:[NSError class]]) {
*aError = (NSError*)data;
return nil;
}
return (NSArray*)data;
}
+ (NSArray *)findAllRemote:(NSError **) aError {
return [self findAllRemoteWithResponse:aError];
}
+ (id)findRemote:(NSString *)elementId withResponse:(NSError **)aError {
Response *res = [Connection get:[self getRemoteElementPath:elementId] withUser:[[self class] getRemoteUser] andPassword:[[self class] getRemotePassword]];
if([res isError] && aError) {
*aError = res.error;
}
id data = [self performSelector:[self getRemoteParseDataMethod] withObject:res.body];
return data;
}
+ (id)findRemote:(NSString *)elementId {
NSError *aError;
return [self findRemote:elementId withResponse:&aError];
}
+ (NSString *)getRemoteElementName {
return [[NSStringFromClass([self class]) stringByReplacingCharactersInRange:NSMakeRange(0, 1)
withString:[[NSStringFromClass([self class]) substringWithRange:NSMakeRange(0,1)] lowercaseString]] underscore];
}
+ (NSString *)getRemoteCollectionName {
return [[self getRemoteElementName] stringByAppendingString:@"s"];
}
+ (NSString *)getRemoteElementPath:(NSString *)elementId {
return [NSString stringWithFormat:@"%@%@/%@%@", [self getRemoteSite], [self getRemoteCollectionName], elementId, [self getRemoteProtocolExtension]];
}
+ (NSString *)getRemoteCollectionPath {
return [[[self getRemoteSite] stringByAppendingString:[self getRemoteCollectionName]] stringByAppendingString:[self getRemoteProtocolExtension]];
}
+ (NSString *)getRemoteCollectionPathWithParameters:(NSDictionary *)parameters {
return [self populateRemotePath:[self getRemoteCollectionPath] withParameters:parameters];
}
+ (NSString *)populateRemotePath:(NSString *)path withParameters:(NSDictionary *)parameters {
// Translate each key to have a preceeding ":" for proper URL param notiation
NSMutableDictionary *parameterized = [NSMutableDictionary dictionaryWithCapacity:[parameters count]];
for (NSString *key in parameters) {
[parameterized setObject:[parameters objectForKey:key] forKey:[NSString stringWithFormat:@":%@", key]];
}
return [path gsub:parameterized];
}
- (NSString *)getRemoteCollectionPath {
return [[self class] getRemoteCollectionPath];
}
- (NSArray*)getDefaultExclusions{
// exclude id , created_at , updated_at
return [NSArray arrayWithObjects:[self getRemoteClassIdName],@"createdAt",@"updatedAt",nil];
}
// Converts the object to the data format expected by the server
- (NSString *)convertToRemoteExpectedType {
return [self performSelector:[[self class] getRemoteSerializeMethod] withObject:[self getDefaultExclusions]];
}
#pragma mark default equals methods for id and class based equality
- (BOOL)isEqualToRemote:(id)anObject {
return [NSStringFromClass([self class]) isEqualToString:NSStringFromClass([anObject class])] &&
[anObject respondsToSelector:@selector(getRemoteId)] && [[anObject getRemoteId]isEqualToString:[self getRemoteId]];
}
- (NSUInteger)hashForRemote {
return [[self getRemoteId] intValue] + [NSStringFromClass([self class]) hash];
}
#pragma mark Instance-specific methods
- (id)getRemoteId {
id result = nil;
SEL idMethodSelector = NSSelectorFromString([self getRemoteClassIdName]);
if ([self respondsToSelector:idMethodSelector]) {
result = [self performSelector:idMethodSelector];
if ([result respondsToSelector:@selector(stringValue)]) {
result = [result stringValue];
}
}
return result;
}
- (void)setRemoteId:(id)orsId {
SEL setter = NSSelectorFromString([NSString stringWithFormat:@"set%@Id:",[self className]]);
if ([self respondsToSelector:setter]) {
[self performSelector:setter withObject:orsId];
}
}
- (NSString *)getRemoteClassIdName {
return [NSString stringWithFormat:@"%@Id",
[NSStringFromClass([self class]) stringByReplacingCharactersInRange:NSMakeRange(0, 1)
withString:[[NSStringFromClass([self class]) substringWithRange:NSMakeRange(0,1)] lowercaseString]]];
}
- (id)reload{
id reloaded = [[self class] findRemote:[self getRemoteId]];
[self setProperties:[reloaded properties]];
return self;
}
- (BOOL)createRemoteAtPath:(NSString *)path withResponse:(NSError **)aError {
//Response *res = [Connection post:[self convertToRemoteExpectedType] to:path withUser:[[self class] getRemoteUser] andPassword:[[self class] getRemotePassword]];
Response *res = [Connection post:path withUser:[[self class] getRemoteUser] andPassword:[[self class] getRemotePassword] forARObject:self];
if([res isError] && aError) {
*aError = res.error;
}
if ([res isARError]) {
NSString *body = [[[NSString alloc] initWithData:res.body encoding:NSUTF8StringEncoding] autorelease];
NSString *plist = [[[body stringByReplacingOccurrencesOfString:@"<errors>" withString:@"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"><plist version=\"1.0\"><array>"] stringByReplacingOccurrencesOfString:@"</errors>" withString:@"</array></plist>"] stringByReplacingOccurrencesOfString:@"error>" withString:@"string>"];
NSError *error = nil;
NSArray *prop = [NSPropertyListSerialization propertyListWithData:[plist dataUsingEncoding:NSUTF8StringEncoding] options:0 format:nil error:&error];
[self setProperties:[NSDictionary dictionaryWithObjectsAndKeys:prop, @"errors", nil]];
}
if ([res isSuccess]) {
NSDictionary *newProperties = [[[self class] performSelector:[[self class] getRemoteParseDataMethod] withObject:res.body] properties];
[self setProperties:newProperties];
return YES;
}
else {
return NO;
}
}
-(BOOL)updateRemoteAtPath:(NSString *)path withResponse:(NSError **)aError {
Response *res = [Connection put:path withUser:[[self class] getRemoteUser] andPassword:[[self class] getRemotePassword] forARObject:self];
if([res isError] && aError) {
*aError = res.error;
}
if ([res isSuccess]) {
if([(NSString *)[res.headers objectForKey:@"Content-Length"] intValue] > 1) {
NSDictionary *newProperties = [[[self class] performSelector:[[self class] getRemoteParseDataMethod] withObject:res.body] properties];
[self setProperties:newProperties];
}
return YES;
}
else {
return NO;
}
}
- (BOOL)destroyRemoteAtPath:(NSString *)path withResponse:(NSError **)aError {
Response *res = [Connection delete:path withUser:[[self class] getRemoteUser] andPassword:[[self class] getRemotePassword]];
if([res isError] && aError) {
*aError = res.error;
}
return [res isSuccess];
}
- (BOOL)createRemoteWithResponse:(NSError **)aError {
return [self createRemoteAtPath:[self getRemoteCollectionPath] withResponse:aError];
}
- (BOOL)createRemote {
NSError *error;
return [self createRemoteWithResponse:&error];
}
- (BOOL)createRemoteWithParameters:(NSDictionary *)parameters andResponse:(NSError **)aError {
return [self createRemoteAtPath:[[self class] getRemoteCollectionPathWithParameters:parameters] withResponse:aError];
}
- (BOOL)createRemoteWithParameters:(NSDictionary *)parameters {
NSError *error;
return [self createRemoteWithParameters:parameters andResponse:&error];
}
- (BOOL)destroyRemoteWithResponse:(NSError **)aError {
id myId = [self getRemoteId];
if (nil != myId) {
return [self destroyRemoteAtPath:[[self class] getRemoteElementPath:myId] withResponse:aError];
}
else {
// this should return a error
return NO;
}
}
- (BOOL)destroyRemote {
NSError *error;
return [self destroyRemoteWithResponse:&error];
}
- (BOOL)updateRemoteWithResponse:(NSError **)aError {
id myId = [self getRemoteId];
if (nil != myId) {
return [self updateRemoteAtPath:[[self class] getRemoteElementPath:myId] withResponse:aError];
}
else {
// this should return an error
return NO;
}
}
- (BOOL)updateRemote {
NSError *error;
return [self updateRemoteWithResponse:&error];
}
- (BOOL)saveRemoteWithResponse:(NSError **)aError {
id myId = [self getRemoteId];
if (nil == myId) {
return [self createRemoteWithResponse:aError];
}
else {
return [self updateRemoteWithResponse:aError];
}
}
- (BOOL)saveRemote {
NSError *error;
return [self saveRemoteWithResponse:&error];
}
@end