-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteModel.m
More file actions
146 lines (128 loc) · 4.63 KB
/
RemoteModel.m
File metadata and controls
146 lines (128 loc) · 4.63 KB
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
//
// RemoteModel.h
// Created by Nitin Shantharam on 12/24/11.
//
#import "RemoteModel.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
@implementation RemoteModel
@synthesize error;
@synthesize response;
- (id)initWithBaseURL:(NSString*)aBaseUrl {
self = [super init];
if (self) {
baseURL = aBaseUrl;
apiToken = nil;
}
return self;
}
- (void)findAll {
if ([apiToken isEqualToString:@""] || apiToken == nil)
[self get:baseURL];
else
[self get:[NSString stringWithFormat:@"%@?api_token=%@", baseURL, apiToken]];
}
- (void)findOne:(NSString*)anId {
if ([apiToken isEqualToString:@""] || apiToken == nil)
[self get:[NSString stringWithFormat:@"%@/%@", baseURL, anId]];
else
[self get:[NSString stringWithFormat:@"%@/%@?api_token=%@", baseURL, anId, apiToken]];
}
- (void)create:(NSDictionary*)params {
if ([apiToken isEqualToString:@""] || apiToken == nil)
[self post:[NSString stringWithFormat:@"%@", baseURL] postParams:params];
else
[self post:[NSString stringWithFormat:@"%@?api_token=", baseURL, apiToken] postParams:params];
}
- (void)update:(NSString*)anId params:(NSDictionary*)params {
[params setValue:@"put" forKey:@"_method"];
if (anId == nil) {
if ([apiToken isEqualToString:@""] || apiToken == nil)
[self post:[NSString stringWithFormat:@"%@", baseURL] postParams:params];
else
[self post:[NSString stringWithFormat:@"%@?api_token=%@", baseURL, apiToken] postParams:params];
}
else {
if ([apiToken isEqualToString:@""] || apiToken == nil)
[self post:[NSString stringWithFormat:@"%@/%@", baseURL, anId] postParams:params];
else
[self post:[NSString stringWithFormat:@"%@/%@?api_token=%@", baseURL, anId, apiToken] postParams:params];
}
}
- (void)destroy:(NSString*)anId {
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setValue:@"delete" forKey:@"_method"];
if ([apiToken isEqualToString:@""] || apiToken == nil)
[self post:[NSString stringWithFormat:@"%@/%@", baseURL, anId] postParams:params];
else
[self post:[NSString stringWithFormat:@"%@/%@?api_token=%@", baseURL, anId, apiToken] postParams:params];
}
- (void)actionOnResource:(NSString*)anId action:(NSString*)anAction postParams:(NSDictionary*)params {
if ([apiToken isEqualToString:@""] || apiToken == nil)
[self post:[NSString stringWithFormat:@"%@/%@/%@", baseURL, anId, anAction] postParams:params];
else
[self post:[NSString stringWithFormat:@"%@/%@/%@?api_token=%@", baseURL, anId, anAction, apiToken] postParams:params];
}
- (void)get:(NSString*)url {
response = nil;
__weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setAllowCompressedResponse:YES];
[request setCompletionBlock:^{
SBJsonParser *parser = [SBJsonParser new];
response = [parser objectWithString:[request responseString]];
if(completionBlock){
completionBlock();
}
}];
[request setFailedBlock:^{
error = [request error];
if(failureBlock){
failureBlock();
}
}];
[request startAsynchronous];
}
- (void)post:(NSString*)url postParams:(NSDictionary*)postParams {
response = nil;
__weak ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
for (NSString *key in postParams) {
id val = [postParams objectForKey:key];
if ([val isKindOfClass:[NSString class]]) {
[request setPostValue:val forKey:key];
}
else {
NSDictionary *dict = (NSDictionary*)val;
[request setData:[dict objectForKey:@"data"]
withFileName:[dict objectForKey:@"filename"]
andContentType:[dict objectForKey:@"content_type"]
forKey:key];
}
}
[request setCompletionBlock:^{
SBJsonParser *parser = [SBJsonParser new];
response = [parser objectWithString:[request responseString]];
if(completionBlock){
completionBlock();
}
}];
[request setFailedBlock:^{
error = [request error];
if(failureBlock){
failureBlock();
}
}];
[request startAsynchronous];
}
- (void)setCompletionBlock:(RemoteModelBasicBlock)aCompletionBlock
{
completionBlock = [aCompletionBlock copy];
}
- (void)setFailedBlock:(RemoteModelBasicBlock)aFailedBlock
{
failureBlock = [aFailedBlock copy];
}
- (void)signWithApiToken:(NSString*)anApiToken {
apiToken = anApiToken;
}
@end