Skip to content

Enforce completion closures for operations (Public API change) #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# PPEventRegistryAPI — CHANGELOG

##0.0.2
* Enforce completion closures for operations (Public API change)
[Pavel Pantus](https://github.com/pantuspavel), [#60](https://github.com/pantuspavel/PPEventRegistryAPI/pull/60)
* flatMap instead of forEach
[Pavel Pantus](https://github.com/pantuspavel), [#59](https://github.com/pantuspavel/PPEventRegistryAPI/pull/59)
* Minor stubs naming and style corrections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PPAsyncOperation: Operation {
internal let httpMethod: String
internal var transport: PPTransportProtocol!
internal var modelMapper: PPModelMapper!
internal var completionHandler: (([String: Any]?, NSError?) -> Void)?
internal var completionHandler: ([String: Any]?, NSError?) -> Void = {_,_ in }

init(controller: String, httpMethod: String, parameters: [String: Any]) {
self.controller = controller
Expand Down Expand Up @@ -50,7 +50,7 @@ class PPAsyncOperation: Operation {

override final func start() {
if isCancelled {
completionHandler?(nil, NSError(domain: "Operation was cancelled", code: 0, userInfo: nil))
completionHandler(nil, NSError(domain: "Operation was cancelled", code: 0, userInfo: nil))
isFinished = true
return
}
Expand All @@ -59,9 +59,9 @@ class PPAsyncOperation: Operation {

transport.postRequest(controller: controller, httpMethod: httpMethod, parameters: parameters) { response, error -> Void in
if self.isCancelled {
self.completionHandler?(response, NSError(domain: "Operation was cancelled", code: 0, userInfo: nil))
self.completionHandler(response, NSError(domain: "Operation was cancelled", code: 0, userInfo: nil))
} else {
self.completionHandler?(response, error)
self.completionHandler(response, error)
}

self.isExecuting = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

final class PPGetEventOperation: PPAsyncOperation {
init(identifier: NSNumber, completionHandler: ((_ event: PPEvent?, _ error: NSError?) -> Void)?) {
init(identifier: NSNumber, completionHandler: @escaping (_ event: PPEvent?, _ error: NSError?) -> Void) {
let parameters: [String : Any] = ["action": "getEvent",
"eventUri": identifier,
"infoConceptLang": "eng",
Expand All @@ -30,7 +30,7 @@ final class PPGetEventOperation: PPAsyncOperation {
}

DispatchQueue.main.async {
completionHandler?(events.first, error)
completionHandler(events.first, error)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation

final class PPGetRecentArticles: PPAsyncOperation {
init(completionHandler: ((_ events: [PPArticle], _ error: NSError?) -> Void)?) {
init(completionHandler: @escaping (_ events: [PPArticle], _ error: NSError?) -> Void) {
let parameters: [String: Any] = ["action": "getRecentActivity",
"addEvents": false,
"addActivity": false,
Expand All @@ -29,7 +29,7 @@ final class PPGetRecentArticles: PPAsyncOperation {
}

DispatchQueue.main.async {
completionHandler?(events, error)
completionHandler(events, error)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
import Foundation

final class PPLoginOperation: PPAsyncOperation {
init(email: String, password: String, completionHandler: ((_ error: NSError?) -> Void)?) {
init(email: String, password: String, completionHandler: @escaping (_ error: NSError?) -> Void) {
let parameters = ["email": email, "pass": password]
let completion: ([String: Any]?, NSError?) -> Void = { response, error in
if let action = response?["action"] as? String, action == "unknownUser" {
DispatchQueue.main.async {
let error = NSError(domain: "Unknown User", code: 0, userInfo: nil)
completionHandler?(error)
completionHandler(error)
}
} else {
DispatchQueue.main.async {
completionHandler?(error)
completionHandler(error)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions PPEventRegistryAPI/Classes/API/PPEventRegistryAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class PPEventRegistryAPI {
|| operation.isKind(of: PPLoginOperation.self) else {
DispatchQueue.main.async {
let error = NSError(domain: "Log In Needed", code: 100, userInfo: nil)
operation.completionHandler?(nil, error)
operation.completionHandler(nil, error)
}
return
}
Expand All @@ -56,22 +56,22 @@ public final class PPEventRegistryAPI {

extension PPEventRegistryAPI {

public func login(_ email: String, password: String, completionHandler: ((_ error: NSError?) -> Void)?) {
public func login(_ email: String, password: String, completionHandler: @escaping (_ error: NSError?) -> Void) {
let login = PPLoginOperation(email: email, password: password) { error in
self.state = error == nil ? .loggedIn(email: email, password: password) : .loggedOut
completionHandler?(error)
completionHandler(error)
}
schedule(login)
}

public func getEvent(withID id: NSNumber, completionHandler: ((_ event: PPEvent?, _ error: NSError?) -> Void)?) {
public func getEvent(withID id: NSNumber, completionHandler: @escaping (_ event: PPEvent?, _ error: NSError?) -> Void) {
let getEvent = PPGetEventOperation(identifier: id) { event, error in
completionHandler?(event, error)
completionHandler(event, error)
}
schedule(getEvent)
}

public func getRecentArticles(_ completionHandler: ((_ articles: [PPArticle], _ error: NSError?) -> Void)?) {
public func getRecentArticles(_ completionHandler: @escaping (_ articles: [PPArticle], _ error: NSError?) -> Void) {
let getRecentActivity = PPGetRecentArticles(completionHandler: completionHandler)
schedule(getRecentActivity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
E9C3362B1D399A160021E933 /* PPGetRecentAriclesSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9C3362A1D399A160021E933 /* PPGetRecentAriclesSpec.swift */; };
E9CB7B491D37C43B001BBEDB /* PPEventRegistryAPISpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9CB7B481D37C43B001BBEDB /* PPEventRegistryAPISpec.swift */; };
E9CB7B4B1D37C69C001BBEDB /* PPGetEventOperationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9CB7B4A1D37C69C001BBEDB /* PPGetEventOperationSpec.swift */; };
E9DF7B941D92C72E00F5B8EA /* CHANGELOG.md in Sources */ = {isa = PBXBuildFile; fileRef = E9DF7B931D92C72E00F5B8EA /* CHANGELOG.md */; };
E9E0B73A1D18CAD300702E4E /* Dictionary+PPJoin.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E0B7391D18CAD300702E4E /* Dictionary+PPJoin.swift */; };
E9E9CBC61D40FB8100EE5F0C /* PPModelMapperSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E9CBC51D40FB8100EE5F0C /* PPModelMapperSpec.swift */; };
E9E9CBC71D40FB8100EE5F0C /* PPModelMapperSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E9CBC51D40FB8100EE5F0C /* PPModelMapperSpec.swift */; };
Expand Down Expand Up @@ -1503,7 +1502,6 @@
files = (
E9E0B73A1D18CAD300702E4E /* Dictionary+PPJoin.swift in Sources */,
E9F2EA011D176E7800DBF268 /* PPEventRegistryAPI.swift in Sources */,
E9DF7B941D92C72E00F5B8EA /* CHANGELOG.md in Sources */,
E9F2EA071D1770EB00DBF268 /* PPLoginOperation.swift in Sources */,
E9FB398A1D1A1F0B003E17EA /* PPGetEventOperation.swift in Sources */,
E9584F851D20196800B32570 /* PPModelMapper.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ class PPModelMapperSpec: QuickSpec {
expect(events).to(haveCount(1))
}

it("Creates an empty event in case of an empty input") {
let event: PPEvent = modelMapper.mapDataToModelObject([:])
expect(event.title).to(equal(""))
expect(event.summary).to(equal(""))
expect(event.eventDate).to(equal(""))
expect(event.location).to(equal(""))
expect(event.image).to(beNil())
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import Nimble
class PPGetEventOperationSpec: QuickSpec {
override func spec() {

var getEventOperation = PPGetEventOperation(identifier: 123, completionHandler: nil)
var getEventOperation = PPGetEventOperation(identifier: 123, completionHandler: {_,_ in})

beforeEach {
getEventOperation = PPGetEventOperation(identifier: 123, completionHandler: nil)
getEventOperation = PPGetEventOperation(identifier: 123, completionHandler: {_,_ in})
}

it("subclass of PPAsyncOperation") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import Nimble
class PPGetRecentArticlesSpec: QuickSpec {
override func spec() {

var getRecentArticles = PPGetRecentArticles(completionHandler: nil)
var getRecentArticles = PPGetRecentArticles(completionHandler: {_,_ in})

beforeEach {
getRecentArticles = PPGetRecentArticles(completionHandler: nil)
getRecentArticles = PPGetRecentArticles(completionHandler: {_,_ in})
}

it("subclass of PPAsyncOperation") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class PPLoginOperationSpec: QuickSpec {

var loginOperation = PPLoginOperation(email: "[email protected]",
password: "q",
completionHandler: nil)
completionHandler: {_ in})

beforeEach {
loginOperation = PPLoginOperation(email: "[email protected]",
password: "qwerty",
completionHandler: nil)
completionHandler: {_ in})
}

it("subclass of PPAsyncOperation") {
Expand Down