|
| 1 | +// |
| 2 | +// Content.swift |
| 3 | +// Pods |
| 4 | +// |
| 5 | +// Created by Cesar on 9/23/15. |
| 6 | +// |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import PromiseKit |
| 11 | +import Alamofire |
| 12 | + |
| 13 | +public final class Content: Model,ResponseObject,ResponseCollection { |
| 14 | + |
| 15 | + var children: [Content] = [] |
| 16 | + public let uuid: String |
| 17 | + public let subtype: CONTENT_TYPES |
| 18 | + |
| 19 | + public enum CONTENT_TYPES : String { |
| 20 | + case APP = "scala:content:app" |
| 21 | + case FILE = "scala:content:file" |
| 22 | + case FOLDER = "scala:content:folder" |
| 23 | + case URL = "scala:content:url" |
| 24 | + case UNKNOWN = "" |
| 25 | + } |
| 26 | + |
| 27 | + required public init?(response: NSHTTPURLResponse, representation: AnyObject) { |
| 28 | + if let representation = representation as? [String: AnyObject] { |
| 29 | + self.uuid = representation["uuid"] as! String |
| 30 | + self.subtype = CONTENT_TYPES(rawValue: representation["subtype"] as! String)! |
| 31 | + } else { |
| 32 | + self.uuid = "" |
| 33 | + self.subtype = CONTENT_TYPES.UNKNOWN |
| 34 | + } |
| 35 | + |
| 36 | + if let childrenPath = representation.valueForKeyPath("children") as? [[String: AnyObject]] { |
| 37 | + self.children = Content.collection(response:response, representation: childrenPath) |
| 38 | + } |
| 39 | + |
| 40 | + super.init(response: response, representation: representation) |
| 41 | + |
| 42 | + // remove children from document |
| 43 | + document["children"] = nil |
| 44 | + } |
| 45 | + |
| 46 | + public static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [Content] { |
| 47 | + var contents: [Content] = [] |
| 48 | + if let representation = representation as? [[String: AnyObject]] { |
| 49 | + for contentRepresentation in representation { |
| 50 | + if let content = Content(response: response, representation: contentRepresentation) { |
| 51 | + contents.append(content) |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + return contents |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + Get Children from Node |
| 60 | + @return Promise<[Content]>. |
| 61 | + */ |
| 62 | + public func getChildren() ->Promise<[Content]>{ |
| 63 | + if (!children.isEmpty) { |
| 64 | + return Promise<[Content]> { fulfill, reject in |
| 65 | + fulfill(children) |
| 66 | + } |
| 67 | + } else { |
| 68 | + return Promise { fulfill, reject in |
| 69 | + Alamofire.request(Router.getContent(uuid) ) |
| 70 | + .responseObject { (response: Response<Content, NSError>) in |
| 71 | + switch response.result{ |
| 72 | + case .Success(let data): |
| 73 | + self.children = data.children |
| 74 | + |
| 75 | + fulfill(self.children) |
| 76 | + case .Failure(let error): |
| 77 | + return reject(error) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + Get Url |
| 86 | + @return String. |
| 87 | + */ |
| 88 | + |
| 89 | + public func getUrl () -> String? { |
| 90 | + |
| 91 | + let rt = auth?.get("restrictedToken") as! String |
| 92 | + |
| 93 | + switch(self.subtype) { |
| 94 | + case .FILE: |
| 95 | + let escapeUrl = self.document["path"]!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! |
| 96 | + return "\(hostUrl)/api/delivery\(escapeUrl)?_rt=\(rt)" |
| 97 | + case .APP: |
| 98 | + let escapeUrl = self.document["path"]!.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! |
| 99 | + return "\(hostUrl)/api/delivery\(escapeUrl)/index.html?_rt=\(rt)" |
| 100 | + case .URL: |
| 101 | + return self.document["url"] as? String |
| 102 | + default: |
| 103 | + return nil |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + Get Url to a file variant |
| 109 | + @return String. |
| 110 | + */ |
| 111 | + public func getVariantUrl (name: String) -> String? { |
| 112 | + |
| 113 | + if(CONTENT_TYPES.FILE == self.subtype && hasVariant(name)){ |
| 114 | + if let url = getUrl() { |
| 115 | + let rt = auth?.get("restrictedToken") as! String |
| 116 | + let variant = name.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)! |
| 117 | + return "\(url)?variant=\(variant)&_rt=\(rt)" |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | + public func hasVariant(name: String) -> Bool { |
| 125 | + if let variants = self.document["variants"] as? [[String:AnyObject]] { |
| 126 | + for variant in variants { |
| 127 | + if(variant["name"] as! String == name){ |
| 128 | + return true |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments