diff --git a/Vox/Core/Class/Context_Query.swift b/Vox/Core/Class/Context_Query.swift index 95a173a..86dc5d8 100644 --- a/Vox/Core/Class/Context_Query.swift +++ b/Vox/Core/Class/Context_Query.swift @@ -25,6 +25,13 @@ extension Context { } else if data is NSNull { value = nil } + } else if let linkDocumentData = resource.object?.value(forKeyPath: "links.\(key)") { + if let stringObject = linkDocumentData as? String, let urlObject = URL(string: stringObject) { + value = urlObject // assume simple string url + } else if let dictionary = linkDocumentData as? [String: Any] { + // need to parse { href: "", meta : {..}} + value = dictionary + } } } diff --git a/Vox/Core/Class/Resource.swift b/Vox/Core/Class/Resource.swift index c885a0a..83f66f7 100644 --- a/Vox/Core/Class/Resource.swift +++ b/Vox/Core/Class/Resource.swift @@ -48,6 +48,16 @@ open class Resource: BaseResource { return _relationships } + + public var links: NSMutableDictionary? { + var _links: NSMutableDictionary? + + context?.queue.sync { + _links = object?["links"] as? NSMutableDictionary + } + + return _links + } public required init(context: Context? = nil) { super.init() @@ -77,6 +87,7 @@ open class Resource: BaseResource { public func documentDictionary() throws -> [String: Any] { let attributes = self.attributes let relationships = self.relationships + let links = self.links var dictionary: [String: Any] = [ "type": self.type @@ -95,6 +106,11 @@ open class Resource: BaseResource { relationships.count > 0 { dictionary["relationships"] = relationships } + + if let links = links, + links.count > 0 { + dictionary["links"] = links + } return ["data": dictionary] } @@ -131,6 +147,7 @@ extension Array where Element: Resource { let attributes = resource.attributes let relationships = resource.relationships + let links = resource.links var dictionary: [String: Any] = [ "id": id, @@ -146,6 +163,11 @@ extension Array where Element: Resource { relationships.count > 0 { dictionary["relationships"] = relationships } + + if let links = links, + links.count > 0 { + dictionary["links"] = links + } return dictionary } diff --git a/VoxTests/Assets/Articles.json b/VoxTests/Assets/Articles.json index 9d33b90..276bf62 100644 --- a/VoxTests/Assets/Articles.json +++ b/VoxTests/Assets/Articles.json @@ -37,7 +37,17 @@ "type": "persons2" } } - } + }, + "links": { + "here": "www.example.com", + "there": "www.example2.com", + "related": { + "href": "http://example.com/articles/1/comments", + "meta": { + "count": 10 + } + } + } }], "included": [ { diff --git a/VoxTests/Deserializer/DeserializerCollectionSpec.swift b/VoxTests/Deserializer/DeserializerCollectionSpec.swift index 0f3e771..07b3031 100644 --- a/VoxTests/Deserializer/DeserializerCollectionSpec.swift +++ b/VoxTests/Deserializer/DeserializerCollectionSpec.swift @@ -23,6 +23,9 @@ fileprivate class Article2: Resource { @objc dynamic var author: Person2? @objc dynamic var hint: String? @objc dynamic var customObject: [String: Any]? + @objc dynamic var here: URL? + @objc dynamic var there: URL? + @objc dynamic var related: [String: Any]? } @@ -61,7 +64,9 @@ class DeserializerCollectionSpec: QuickSpec { expect(article?.descriptionText).to(equal("Desc")) expect(article?.keywords).notTo(beNil()) expect(article?.customObject).notTo(beNil()) - + expect(article?.here).to(equal(URL(string: "www.example.com")!)) + expect(article?.related).notTo(beNil()) + expect(article?.links).notTo(beNil()) let coauthors = article?.coauthors let author = article?.author