Skip to content
This repository was archived by the owner on Dec 31, 2018. It is now read-only.
This repository was archived by the owner on Dec 31, 2018. It is now read-only.

Add engagement rules to selectively parse JSON #64

Description

@doozMen
// TODO: Add tests for these rules

/// The `EngagementRule` enum defines how deep a JSON is to be parsed
/**

{ node:subnode:{node:subnode:...}

 */
/// This is a placeholder for a fearue that we should have in the future
///  case ObjectDetermined // The rules of the Type to be parsed should be used.
/// case Inline // Only the data that you already have
/// case Exhaustive // fetch all sub entities via new service requests
/// case ExhaustiveRecursive // fetch all sub entities of sub entities (nuclear)
public enum EngagementRule: Equatable {
    case None // Stop parsing 1 node deep
    case All // Parse all
}

public func == (lhs: EngagementRule, rhs: EngagementRule) -> Bool {
    switch lhs {
    case .None:
        switch rhs {
        case .None:
            return true
        default:
            return false
        }
    case .All:
        switch rhs {
        case .All:
            return true
        default:
            return false
        }
    }
}

And change Order to

public enum Method: String {
    case GET, POST, PUT, DELETE, PATCH
}

public class Order {
    public typealias Rules = [(nodeKey: String, rule: EngagementRule)]
    public let path: String
    public let method: Method

    /// The rules for the nodes that should be fetched.
    public let rulesOfEngagement: Rules?

    public init(path: String, method: Method = .GET, rulesOfEngagement: Rules? = nil) {
    public init(path: String, method: Method = .GET) {
        self.path = path
        self.method = method
        self.rulesOfEngagement = rulesOfEngagement
    }

    public func request(withConfiguration configuration: Configuration) -> NSURLRequest? {
        let mutableRequest = NSMutableURLRequest(URL: NSURL(string: "\(configuration.baseURL)/\(path)")!, cachePolicy: .ReloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0)
        mutableRequest.HTTPMethod = method.rawValue

        return mutableRequest
    }

    public func collectionRequestForConfiguration(configuration: Configuration) -> NSURLRequest? {
        return nil
    }

    /// Parse sub nodes or not
    public func engagementRuleForNodeKey(nodeKey: String) -> EngagementRule {
        guard let rulesOfEngagement = rulesOfEngagement else {
            return .None
        }

        if let rule = (rulesOfEngagement.filter { $0.nodeKey == nodeKey }).first {
            return rule.rule
        } else {
            return .None
        }
    }

    // TODO: #60 Add ability to paginate
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions