rest_kat 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.rvmrc +2 -0
- data/Gemfile +15 -0
- data/LICENSE.txt +20 -0
- data/README.md +188 -0
- data/README.rdoc +19 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/model.h.erb +60 -0
- data/lib/model.m.erb +115 -0
- data/lib/rest_kat.rb +342 -0
- data/lib/tasks/iphone.rake +0 -0
- data/lib/validator.rb +53 -0
- data/rest_kat.gemspec +71 -0
- data/spec/rest_kat_spec.rb +7 -0
- data/spec/spec_helper.rb +12 -0
- data/src/MSRestSerializable.h +79 -0
- data/src/MSRestSerializable.m +212 -0
- metadata +131 -0
@@ -0,0 +1,212 @@
|
|
1
|
+
//
|
2
|
+
// NSApiBase.m
|
3
|
+
// MySugr
|
4
|
+
//
|
5
|
+
// Created by brad phelan on 10/4/11.
|
6
|
+
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
|
7
|
+
//
|
8
|
+
|
9
|
+
#import "MSRestSerializable.h"
|
10
|
+
#import <RestKit/RestKit.h>
|
11
|
+
#import <objc/runtime.h>
|
12
|
+
|
13
|
+
@implementation MSRestSerializable
|
14
|
+
|
15
|
+
|
16
|
+
- (RKObjectMapping *) _mapper
|
17
|
+
{
|
18
|
+
RKObjectMappingProvider * provider = [RKObjectManager sharedManager].mappingProvider;
|
19
|
+
RKObjectMapping * mapping = [provider objectMappingForClass:[self class]];
|
20
|
+
return mapping;
|
21
|
+
|
22
|
+
|
23
|
+
}
|
24
|
+
|
25
|
+
- (RKObjectMapping *) _serializer
|
26
|
+
{
|
27
|
+
RKObjectMappingProvider * provider = [RKObjectManager sharedManager].mappingProvider;
|
28
|
+
RKObjectMapping * mapping = [provider serializationMappingForClass:[self class]];
|
29
|
+
return mapping;
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
- (BOOL)isNew
|
35
|
+
{
|
36
|
+
BOOL hasId = class_getProperty([self class], [@"id" UTF8String]) != NULL;
|
37
|
+
return ([self valueForKey:@"id"] == NULL || !hasId);
|
38
|
+
}
|
39
|
+
|
40
|
+
|
41
|
+
- (NSDictionary *)convertToDictionary
|
42
|
+
{
|
43
|
+
NSError * error;
|
44
|
+
// First convert the query object into a dictionary
|
45
|
+
RKObjectSerializer * querySerializer = [RKObjectSerializer serializerWithObject:self mapping:[self _serializer]];
|
46
|
+
return [querySerializer serializedObject:&error];
|
47
|
+
}
|
48
|
+
|
49
|
+
|
50
|
+
- (NSString*)convertToJSON
|
51
|
+
{
|
52
|
+
RKObjectSerializer * serializer = [RKObjectSerializer serializerWithObject:self mapping:[self _serializer]];
|
53
|
+
NSError * error;
|
54
|
+
NSString * text = (NSString *)[serializer serializedObjectForMIMEType:RKMIMETypeJSON error:&error];
|
55
|
+
return text;
|
56
|
+
}
|
57
|
+
|
58
|
+
- (NSString *)convertToFormURLEncoded
|
59
|
+
{
|
60
|
+
RKObjectSerializer * serializer = [RKObjectSerializer serializerWithObject:self mapping:[self _serializer]];
|
61
|
+
|
62
|
+
NSError * error;
|
63
|
+
NSString * text = (NSString *)[serializer serializedObjectForMIMEType:RKMIMETypeFormURLEncoded error: &error];
|
64
|
+
return text;
|
65
|
+
}
|
66
|
+
|
67
|
+
- (NSString*)description
|
68
|
+
{
|
69
|
+
|
70
|
+
|
71
|
+
RKObjectSerializer * serializer = [RKObjectSerializer serializerWithObject:self mapping:[self _serializer]];
|
72
|
+
|
73
|
+
NSError * error;
|
74
|
+
NSString * description = (NSString *)[serializer serializedObjectForMIMEType:RKMIMETypeJSON error: &error];
|
75
|
+
return [description stringByReplacingOccurrencesOfString:@"\"" withString:@"'"];
|
76
|
+
}
|
77
|
+
|
78
|
+
- (id)copyWithZone:(NSZone *)zone
|
79
|
+
{
|
80
|
+
return [[MSRestSerializable allocWithZone:zone] init];
|
81
|
+
}
|
82
|
+
|
83
|
+
@end
|
84
|
+
|
85
|
+
@implementation MSRestSerializableResource
|
86
|
+
|
87
|
+
+ (RKObjectRouter *) router
|
88
|
+
{
|
89
|
+
return [RKObjectManager sharedManager].router;
|
90
|
+
}
|
91
|
+
|
92
|
+
+ (Class) classForResource
|
93
|
+
{
|
94
|
+
NSAssert(NO, @"Stub implementation. Must be implemented in subclass");
|
95
|
+
return NULL;
|
96
|
+
}
|
97
|
+
|
98
|
+
|
99
|
+
+ (void) initializeRouting
|
100
|
+
{
|
101
|
+
RKObjectRouter * router = [self router];
|
102
|
+
|
103
|
+
NSString * collectionResource = [self resourcePath];
|
104
|
+
NSString * objectResource = [NSString stringWithFormat:@"%@/(id)", collectionResource];
|
105
|
+
|
106
|
+
if([self isSingleton])
|
107
|
+
{
|
108
|
+
objectResource = collectionResource;
|
109
|
+
}
|
110
|
+
|
111
|
+
if ([self canCreate]){
|
112
|
+
[router routeClass:self toResourcePath:collectionResource forMethod:RKRequestMethodPOST];
|
113
|
+
}
|
114
|
+
|
115
|
+
if ([self canRead]){
|
116
|
+
[router routeClass:self toResourcePath:objectResource forMethod:RKRequestMethodGET];
|
117
|
+
}
|
118
|
+
|
119
|
+
if ([self canUpdate])
|
120
|
+
{
|
121
|
+
[router routeClass:self toResourcePath:objectResource forMethod:RKRequestMethodPUT];
|
122
|
+
}
|
123
|
+
|
124
|
+
if ([self canDelete])
|
125
|
+
{
|
126
|
+
[router routeClass:self toResourcePath:objectResource forMethod:RKRequestMethodDELETE];
|
127
|
+
}
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
+ (NSString *) resourcePath{
|
133
|
+
NSAssert(NO, @"Not implemented");
|
134
|
+
return NULL;
|
135
|
+
}
|
136
|
+
|
137
|
+
+ (bool) isSingleton{
|
138
|
+
return true;
|
139
|
+
}
|
140
|
+
+ (bool) canCreate{
|
141
|
+
return true;
|
142
|
+
}
|
143
|
+
+ (bool) canRead{
|
144
|
+
return true;
|
145
|
+
}
|
146
|
+
+ (bool) canUpdate{
|
147
|
+
return true;
|
148
|
+
}
|
149
|
+
+ (bool) canDelete{
|
150
|
+
return true;
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
- (void) saveWithDelegate:(id<RKObjectLoaderDelegate>)delegate
|
155
|
+
{
|
156
|
+
if ([self isNew])
|
157
|
+
{
|
158
|
+
NSLog(@"Creating new object\n%@", self);
|
159
|
+
// Save a new object
|
160
|
+
[[RKObjectManager sharedManager] postObject:self mapResponseWith:[self _mapper] delegate:delegate];
|
161
|
+
}else{
|
162
|
+
NSLog(@"Updating object\n%@", self);
|
163
|
+
// Update the object as it allready has an id
|
164
|
+
[[RKObjectManager sharedManager] putObject:self mapResponseWith:[self _mapper] delegate:delegate];
|
165
|
+
}
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
// Find an instance by id
|
170
|
+
+ (void) find:(NSNumber *)id withDelegate:(id<RKObjectLoaderDelegate>)delegate
|
171
|
+
{
|
172
|
+
MSRestSerializable * instance = [[self alloc] init];
|
173
|
+
[instance setValue:id forKey:@"id"];
|
174
|
+
[[RKObjectManager sharedManager] getObject:instance
|
175
|
+
mapResponseWith:[instance _mapper]
|
176
|
+
delegate:delegate];
|
177
|
+
}
|
178
|
+
|
179
|
+
// Load collection
|
180
|
+
+ (void) loadCollectionWithDelegate:(id<RKObjectLoaderDelegate>)delegate
|
181
|
+
{
|
182
|
+
RKObjectManager * objectManager = [RKObjectManager sharedManager];
|
183
|
+
RKObjectMapping * mapping = [objectManager.mappingProvider
|
184
|
+
objectMappingForClass:self ];
|
185
|
+
|
186
|
+
[objectManager loadObjectsAtResourcePath:[self resourcePath]
|
187
|
+
objectMapping:mapping
|
188
|
+
delegate:delegate];
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
// Load collection with query
|
193
|
+
+ (void) loadCollectionThroughQuery:(MSRestSerializable *)mappableQueryObject
|
194
|
+
withDelegate:(id<RKObjectLoaderDelegate>)delegate
|
195
|
+
{
|
196
|
+
|
197
|
+
RKObjectManager * objectManager = [RKObjectManager sharedManager];
|
198
|
+
RKObjectMapping * mapping = [objectManager.mappingProvider
|
199
|
+
objectMappingForClass:self ];
|
200
|
+
|
201
|
+
|
202
|
+
// Append the dictionary to the resource as query parameters
|
203
|
+
NSString * resourcePath = RKPathAppendQueryParams([self resourcePath], [mappableQueryObject convertToDictionary]);
|
204
|
+
|
205
|
+
// Load the objects at the query
|
206
|
+
[objectManager loadObjectsAtResourcePath:resourcePath
|
207
|
+
objectMapping:mapping
|
208
|
+
delegate:delegate];
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
@end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rest_kat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.4
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brad Phelan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-11-18 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: kwalify
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "0"
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.3.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.0.0
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jeweler
|
50
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.6.4
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *id004
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rcov
|
61
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *id005
|
70
|
+
description: The generated source code is a layer on top of the iOS REST framework http://restkit.org/
|
71
|
+
email: brad.phelan@mysugr.com
|
72
|
+
executables: []
|
73
|
+
|
74
|
+
extensions: []
|
75
|
+
|
76
|
+
extra_rdoc_files:
|
77
|
+
- LICENSE.txt
|
78
|
+
- README.md
|
79
|
+
- README.rdoc
|
80
|
+
files:
|
81
|
+
- .document
|
82
|
+
- .rspec
|
83
|
+
- .rvmrc
|
84
|
+
- Gemfile
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.md
|
87
|
+
- README.rdoc
|
88
|
+
- Rakefile
|
89
|
+
- VERSION
|
90
|
+
- lib/model.h.erb
|
91
|
+
- lib/model.m.erb
|
92
|
+
- lib/rest_kat.rb
|
93
|
+
- lib/tasks/iphone.rake
|
94
|
+
- lib/validator.rb
|
95
|
+
- rest_kat.gemspec
|
96
|
+
- spec/rest_kat_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- src/MSRestSerializable.h
|
99
|
+
- src/MSRestSerializable.m
|
100
|
+
homepage: http://github.com/mysugr/rest_kat
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
hash: 2884833936484461467
|
114
|
+
segments:
|
115
|
+
- 0
|
116
|
+
version: "0"
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: "0"
|
123
|
+
requirements: []
|
124
|
+
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.8.8
|
127
|
+
signing_key:
|
128
|
+
specification_version: 3
|
129
|
+
summary: Generate objective C RestKit boilerplate code from a yaml schema
|
130
|
+
test_files: []
|
131
|
+
|