Skip to content

Commit 2affc72

Browse files
author
Josh Holtz
authored
Merge pull request #43 from joshdholtz/resource-meta
Added meta to resource
2 parents 2567863 + ae482d0 commit 2affc72

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

Classes/JSONAPIResource.h

+25
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,30 @@
135135
*/
136136
- (void)setID:(id)identifier;
137137

138+
/**
139+
* Get the meta for a resource instance. Optional for resources that come
140+
* from persistance storage (i.e. the server).
141+
*
142+
* In general, this should be implemented by a @property ID in the realized class. The
143+
* @property declaration will automatically synthesize the get/set members declared in this
144+
* protocol. The property storage is an implementation detail, which is why the protocol does
145+
* not use a @property declaration.
146+
*
147+
* @return The meta for a resource instance.
148+
*/
149+
- (NSDictionary*)meta;
150+
151+
/**
152+
* Set the meta for a resource instance. Optional for resources that come
153+
* from persistance storage (i.e. the server).
154+
*
155+
* In general, this should be implemented by a @property ID in the realized class. The
156+
* @property declaration will automatically synthesize the get/set members declared in this
157+
* protocol. The property storage is an implementation detail, which is why the protocol does
158+
* not use a @property declaration.
159+
*
160+
* @param meta The meta for a resource instance.
161+
*/
162+
- (void)setMeta:(NSDictionary*)meta;
138163

139164
@end

Classes/JSONAPIResourceBase.h

+6
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@
4343
*/
4444
@property (strong, atomic) id ID;
4545

46+
/**
47+
* Meta for a resource instance. Optional for resources that come
48+
* from persistance storage (i.e. the server).
49+
*/
50+
@property (strong, atomic) NSDictionary *meta;
51+
4652
@end

Classes/JSONAPIResourceParser.m

+5
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
181181
NSDictionary *relationships = [dictionary objectForKey:@"relationships"];
182182
NSDictionary *attributes = [dictionary objectForKey:@"attributes"];
183183
NSDictionary *links = [dictionary objectForKey:@"links"];
184+
NSDictionary *meta = [dictionary objectForKey:@"meta"];
184185

185186
id ID = [dictionary objectForKey:@"id"];
186187
NSFormatter *format = [descriptor idFormatter];
@@ -197,6 +198,10 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
197198
NSString *selfLink = links[@"self"];
198199
[resource setValue:selfLink forKey:descriptor.selfLinkProperty];
199200
}
201+
202+
if ([resource respondsToSelector:@selector(setMeta:)]) {
203+
[resource setMeta:meta];
204+
}
200205

201206
// Loops through all keys to map to properties
202207
NSDictionary *properties = [descriptor properties];

JSONAPI.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "JSONAPI"
3-
s.version = "1.0.6"
3+
s.version = "1.0.7"
44
s.summary = "A library for loading data from a JSON API datasource."
55
s.description = <<-DESC
66
A library for loading data from a JSON API datasource. Parses JSON API data into models with support for auto-linking of resources and custom model classes.

Project/JSONAPITests/JSONAPITests.m

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ - (void)testDataArticles {
6464
XCTAssertEqual(jsonAPI.resources.count, 1, @"Resources should contain 1 resource");
6565

6666
ArticleResource *article = jsonAPI.resource;
67+
68+
XCTAssertNotNil(article.meta, @"Meta should not be nil");
69+
XCTAssertEqualObjects(article.meta[@"hehe"], @"hoho", @"Meta's 'hehe' should equal 'hoho'");
70+
6771
XCTAssert([article isKindOfClass:[ArticleResource class]], @"Article should be a ArticleResource");
6872
XCTAssertEqualObjects(article.ID, @"1", @"Article id should be 1");
6973
XCTAssertTrue([article.selfLink isEqualToString:@"http://example.com/articles/1"], @"Article selfLink should be 'http://example.com/articles/1'");

Project/JSONAPITests/main_example.json

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"data": [{
1111
"type": "articles",
1212
"id": "1",
13+
"meta": {
14+
"hehe": "hoho"
15+
},
1316
"attributes": {
1417
"title": "JSON API paints my bikeshed!",
1518
"versions": [

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ For some full examples on how to use everything, please see the tests - https://
2121
2222
Version | Changes
2323
--- | ---
24+
**1.0.7** | Added `meta` and `setMeta` to `JSONAPIResource` and `JSONAPIResourceBase` (https://github.com/joshdholtz/jsonapi-ios/pull/43).
2425
**1.0.6** | Improved resource parsing and added parsing of `selfLinks` (https://github.com/joshdholtz/jsonapi-ios/pull/35). Thanks to [ artcom](https://github.com/ artcom) for helping! Also removed the need to define `setIdProperty` and `setSelfLinkProperty` in every resource (automatically mapped in the init of `JSONAPIResourceDescriptor`)
2526
**1.0.5** | Fix 1-to-many relationships serialization according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/34). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
2627
**1.0.4** | Add support for empty to-one relationship according to JSON API v1.0 (https://github.com/joshdholtz/jsonapi-ios/pull/33). Thanks to [RafaelKayumov](https://github.com/RafaelKayumov) for helping!
@@ -48,7 +49,7 @@ Clone the repository and drop in the .h and .m files from the "Classes" director
4849
JSONAPI is available through [CocoaPods](http://cocoapods.org), to install
4950
it simply add the following line to your Podfile:
5051
51-
pod 'JSONAPI', '~> 1.0.6'
52+
pod 'JSONAPI', '~> 1.0.7'
5253
5354
## Classes/protocols
5455
For some full examples on how to use everything, please see the tests - https://github.com/joshdholtz/jsonapi-ios/blob/master/Project/JSONAPITests/JSONAPITests.m

0 commit comments

Comments
 (0)