Created
August 12, 2016 14:04
-
-
Save juliantejera/60bf9d50ac2a8e58d51afc62f4932d56 to your computer and use it in GitHub Desktop.
Liaison
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RepositoryParserLiaison.swift | |
// Fire | |
// | |
// Created by Julian Tejera on 6/13/15. | |
// Copyright (c) 2015 Julian Tejera. All rights reserved. | |
// | |
import Foundation | |
class RepositoryParserLiaison<T, ParserType: Parser where ParserType.T == T> { | |
let repository: RestRepositoryProtocol | |
let parser: ParserType | |
init(repository: RestRepositoryProtocol, parser: ParserType) { | |
self.repository = repository | |
self.parser = parser | |
} | |
func find(id: String, parameters: [String : String]?, callback: ((response: ParsedResponse<T>) -> Void)?) { | |
repository.find(id, parameters: parameters, callback: { (response) -> Void in | |
self.handleNetworkResponse(response, callback: callback) | |
}) | |
} | |
func findAll(parameters: [String : String]?, callback: ((response: ParsedResponse<T>) -> Void)?) { | |
repository.findAll(parameters, callback: { (response) -> Void in | |
self.handleNetworkResponse(response, callback: callback) | |
}) | |
} | |
func create(item: T, parameters: [String : String]?, callback: ((response: ParsedResponse<T>) -> Void)?) { | |
repository.create(self.parser.toDictionary(item), parameters: parameters, callback: { (response) -> Void in | |
self.handleNetworkResponse(response, callback: callback) | |
}) | |
} | |
func update(id: String, item: T, parameters: [String : String]?, callback: ((response: ParsedResponse<T>) -> Void)?) { | |
repository.update(id, item: self.parser.toDictionary(item), parameters: parameters, callback: { (response) -> Void in | |
self.handleNetworkResponse(response, callback: callback) | |
}) | |
} | |
func delete(id: String, parameters: [String : String]?, callback: ((response: ParsedResponse<T>) -> Void)?) { | |
repository.delete(id, parameters: parameters, callback: { (response) -> Void in | |
self.handleNetworkResponse(response, callback: callback) | |
}) | |
} | |
func handleNetworkResponse(networkResponse: NetworkResponse, callback: ((response: ParsedResponse<T>) -> Void)?) { | |
switch networkResponse { | |
case .SingleItem(let dictionary): | |
if let object = self.parser.toObject(dictionary) { | |
callback?(response: .SingleItem(object)) | |
} else { | |
// callback?(response: .Error(RestRepositoryParsingError())) | |
} | |
case .MultipleItems(let array): | |
var objects = [T]() | |
for dictionary in array { | |
if let object = self.parser.toObject(dictionary) { | |
objects.append(object) | |
} | |
} | |
callback?(response: .MultipleItems(objects)) | |
case .Error(let error): | |
callback?(response: .Error(error)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment