Skip to content

Commit b98c4e9

Browse files
committed
Play vinyl in a single and central point using Swift generics
1 parent 9cc8df8 commit b98c4e9

File tree

5 files changed

+66
-46
lines changed

5 files changed

+66
-46
lines changed

Vinyl.xcodeproj/project.pbxproj

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
7DBD4CC91C79CCE2008C5487 /* JSONHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C72AD2EB1C6E275000E11612 /* JSONHelper.swift */; };
3131
7DBD4CCA1C79CCE2008C5487 /* Turntable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75D79221C6E0E9D004B92A9 /* Turntable.swift */; };
3232
7DBD4CCB1C79CCE2008C5487 /* TurntableConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D22F3011C75351B00A81608 /* TurntableConfiguration.swift */; };
33-
7DBD4CCC1C79CCE2008C5487 /* URLSessionTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = C708861C1C738A19002DBB2C /* URLSessionTask.swift */; };
33+
7DBD4CCC1C79CCE2008C5487 /* URLSessionDataTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = C708861C1C738A19002DBB2C /* URLSessionDataTask.swift */; };
3434
7DBD4CCD1C79CCE2008C5487 /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC5704C1C72A1AB00BA1458 /* Player.swift */; };
3535
7DBD4CCE1C79CCE2008C5487 /* TrackMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC5704E1C72A1FD00BA1458 /* TrackMatcher.swift */; };
3636
7DBD4CCF1C79CCE2008C5487 /* Vinyl.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75D79201C6E0B32004B92A9 /* Vinyl.swift */; };
@@ -47,7 +47,7 @@
4747
7DBD4CDA1C79CCF4008C5487 /* JSONHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C72AD2EB1C6E275000E11612 /* JSONHelper.swift */; };
4848
7DBD4CDB1C79CCF4008C5487 /* Turntable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75D79221C6E0E9D004B92A9 /* Turntable.swift */; };
4949
7DBD4CDC1C79CCF4008C5487 /* TurntableConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D22F3011C75351B00A81608 /* TurntableConfiguration.swift */; };
50-
7DBD4CDD1C79CCF4008C5487 /* URLSessionTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = C708861C1C738A19002DBB2C /* URLSessionTask.swift */; };
50+
7DBD4CDD1C79CCF4008C5487 /* URLSessionDataTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = C708861C1C738A19002DBB2C /* URLSessionDataTask.swift */; };
5151
7DBD4CDE1C79CCF4008C5487 /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC5704C1C72A1AB00BA1458 /* Player.swift */; };
5252
7DBD4CDF1C79CCF4008C5487 /* TrackMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC5704E1C72A1FD00BA1458 /* TrackMatcher.swift */; };
5353
7DBD4CE01C79CCF4008C5487 /* Vinyl.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75D79201C6E0B32004B92A9 /* Vinyl.swift */; };
@@ -64,7 +64,7 @@
6464
7DBD4CEB1C79CCFA008C5487 /* JSONHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = C72AD2EB1C6E275000E11612 /* JSONHelper.swift */; };
6565
7DBD4CEC1C79CCFA008C5487 /* Turntable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75D79221C6E0E9D004B92A9 /* Turntable.swift */; };
6666
7DBD4CED1C79CCFA008C5487 /* TurntableConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D22F3011C75351B00A81608 /* TurntableConfiguration.swift */; };
67-
7DBD4CEE1C79CCFA008C5487 /* URLSessionTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = C708861C1C738A19002DBB2C /* URLSessionTask.swift */; };
67+
7DBD4CEE1C79CCFA008C5487 /* URLSessionDataTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = C708861C1C738A19002DBB2C /* URLSessionDataTask.swift */; };
6868
7DBD4CEF1C79CCFA008C5487 /* Player.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC5704C1C72A1AB00BA1458 /* Player.swift */; };
6969
7DBD4CF01C79CCFA008C5487 /* TrackMatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DC5704E1C72A1FD00BA1458 /* TrackMatcher.swift */; };
7070
7DBD4CF11C79CCFA008C5487 /* Vinyl.swift in Sources */ = {isa = PBXBuildFile; fileRef = C75D79201C6E0B32004B92A9 /* Vinyl.swift */; };
@@ -90,6 +90,7 @@
9090
7DBD4D0B1C79CEE6008C5487 /* Vinyl.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DBD4D0A1C79CEE6008C5487 /* Vinyl.h */; settings = {ATTRIBUTES = (Public, ); }; };
9191
7DBD4D0C1C79CEEB008C5487 /* Vinyl.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DBD4D0A1C79CEE6008C5487 /* Vinyl.h */; settings = {ATTRIBUTES = (Public, ); }; };
9292
7DBD4D0D1C79CEED008C5487 /* Vinyl.h in Headers */ = {isa = PBXBuildFile; fileRef = 7DBD4D0A1C79CEE6008C5487 /* Vinyl.h */; settings = {ATTRIBUTES = (Public, ); }; };
93+
7DE666A61CAB49DA00ED063D /* URLSessionTaskType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE666A51CAB49DA00ED063D /* URLSessionTaskType.swift */; };
9394
82D8B78E1C78FE3100D8E037 /* SwiftCheck.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 82D8B7851C78FE2400D8E037 /* SwiftCheck.framework */; };
9495
82D8B78F1C78FE3800D8E037 /* SwiftCheck.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 82D8B7851C78FE2400D8E037 /* SwiftCheck.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9596
C70886321C74D452002DBB2C /* vinyl_multiple.json in Resources */ = {isa = PBXBuildFile; fileRef = C70886301C74D452002DBB2C /* vinyl_multiple.json */; };
@@ -238,13 +239,14 @@
238239
7DC5704C1C72A1AB00BA1458 /* Player.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Player.swift; sourceTree = "<group>"; };
239240
7DC5704E1C72A1FD00BA1458 /* TrackMatcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrackMatcher.swift; sourceTree = "<group>"; };
240241
7DC570501C72ACA100BA1458 /* RequestMatcherRegistryTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestMatcherRegistryTests.swift; sourceTree = "<group>"; };
242+
7DE666A51CAB49DA00ED063D /* URLSessionTaskType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionTaskType.swift; sourceTree = "<group>"; };
241243
82D8B7731C78E3FF00D8E037 /* Arbitrary.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Arbitrary.swift; sourceTree = "<group>"; };
242244
82D8B7771C78FE2400D8E037 /* SwiftCheck.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = SwiftCheck.xcodeproj; path = Carthage/Checkouts/SwiftCheck/SwiftCheck.xcodeproj; sourceTree = SOURCE_ROOT; };
243245
C70886111C7346CF002DBB2C /* NSURLQueryItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSURLQueryItem.swift; sourceTree = "<group>"; };
244246
C70886141C735136002DBB2C /* FuncComposition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FuncComposition.swift; sourceTree = "<group>"; };
245247
C70886161C737684002DBB2C /* NSURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSURLRequest.swift; sourceTree = "<group>"; };
246248
C708861A1C737C1B002DBB2C /* SequenceType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SequenceType.swift; sourceTree = "<group>"; };
247-
C708861C1C738A19002DBB2C /* URLSessionTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionTask.swift; sourceTree = "<group>"; };
249+
C708861C1C738A19002DBB2C /* URLSessionDataTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLSessionDataTask.swift; sourceTree = "<group>"; };
248250
C70886201C73FBBF002DBB2C /* TrackTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrackTests.swift; sourceTree = "<group>"; };
249251
C70886301C74D452002DBB2C /* vinyl_multiple.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = vinyl_multiple.json; sourceTree = "<group>"; };
250252
C70886311C74D452002DBB2C /* vinyl_single.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = vinyl_single.json; sourceTree = "<group>"; };
@@ -412,7 +414,8 @@
412414
C72AD2EA1C6E274700E11612 /* JSON_Helpers */,
413415
C75D79221C6E0E9D004B92A9 /* Turntable.swift */,
414416
7D22F3011C75351B00A81608 /* TurntableConfiguration.swift */,
415-
C708861C1C738A19002DBB2C /* URLSessionTask.swift */,
417+
7DE666A51CAB49DA00ED063D /* URLSessionTaskType.swift */,
418+
C708861C1C738A19002DBB2C /* URLSessionDataTask.swift */,
416419
E530E6101C864B3300D9B590 /* URLSessionUploadTask.swift */,
417420
7DC5704C1C72A1AB00BA1458 /* Player.swift */,
418421
7DC5704E1C72A1FD00BA1458 /* TrackMatcher.swift */,
@@ -778,7 +781,7 @@
778781
7DBD4CC91C79CCE2008C5487 /* JSONHelper.swift in Sources */,
779782
7DBD4CCA1C79CCE2008C5487 /* Turntable.swift in Sources */,
780783
7DBD4CCB1C79CCE2008C5487 /* TurntableConfiguration.swift in Sources */,
781-
7DBD4CCC1C79CCE2008C5487 /* URLSessionTask.swift in Sources */,
784+
7DBD4CCC1C79CCE2008C5487 /* URLSessionDataTask.swift in Sources */,
782785
7DBD4CCD1C79CCE2008C5487 /* Player.swift in Sources */,
783786
7DBD4CCE1C79CCE2008C5487 /* TrackMatcher.swift in Sources */,
784787
7DBD4CCF1C79CCE2008C5487 /* Vinyl.swift in Sources */,
@@ -815,7 +818,7 @@
815818
7DBD4CEB1C79CCFA008C5487 /* JSONHelper.swift in Sources */,
816819
7DBD4CEC1C79CCFA008C5487 /* Turntable.swift in Sources */,
817820
7DBD4CED1C79CCFA008C5487 /* TurntableConfiguration.swift in Sources */,
818-
7DBD4CEE1C79CCFA008C5487 /* URLSessionTask.swift in Sources */,
821+
7DBD4CEE1C79CCFA008C5487 /* URLSessionDataTask.swift in Sources */,
819822
7DBD4CEF1C79CCFA008C5487 /* Player.swift in Sources */,
820823
7DBD4CF01C79CCFA008C5487 /* TrackMatcher.swift in Sources */,
821824
7DBD4CF11C79CCFA008C5487 /* Vinyl.swift in Sources */,
@@ -852,9 +855,10 @@
852855
7DBD4CDA1C79CCF4008C5487 /* JSONHelper.swift in Sources */,
853856
7DBD4CDB1C79CCF4008C5487 /* Turntable.swift in Sources */,
854857
7DBD4CDC1C79CCF4008C5487 /* TurntableConfiguration.swift in Sources */,
855-
7DBD4CDD1C79CCF4008C5487 /* URLSessionTask.swift in Sources */,
858+
7DBD4CDD1C79CCF4008C5487 /* URLSessionDataTask.swift in Sources */,
856859
7DBD4CDE1C79CCF4008C5487 /* Player.swift in Sources */,
857860
7DBD4CDF1C79CCF4008C5487 /* TrackMatcher.swift in Sources */,
861+
7DE666A61CAB49DA00ED063D /* URLSessionTaskType.swift in Sources */,
858862
7DBD4CE01C79CCF4008C5487 /* Vinyl.swift in Sources */,
859863
7DBD4CE11C79CCF4008C5487 /* Track.swift in Sources */,
860864
7DBD4CE21C79CCF4008C5487 /* Response.swift in Sources */,

Vinyl/Turntable.swift

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ public final class Turntable: NSURLSession {
2525

2626
public init(configuration: TurntableConfiguration, delegateQueue: NSOperationQueue? = nil) {
2727

28-
self.turntableConfiguration = configuration
28+
turntableConfiguration = configuration
2929
if let delegateQueue = delegateQueue {
30-
self.operationQueue = delegateQueue
30+
operationQueue = delegateQueue
3131
} else {
32-
self.operationQueue = NSOperationQueue()
33-
self.operationQueue.maxConcurrentOperationCount = 1
32+
operationQueue = NSOperationQueue()
33+
operationQueue.maxConcurrentOperationCount = 1
3434
}
3535
super.init()
3636
}
3737

3838
public convenience init(vinyl: Vinyl, turntableConfiguration: TurntableConfiguration, delegateQueue: NSOperationQueue? = nil) {
3939

4040
self.init(configuration: turntableConfiguration, delegateQueue: delegateQueue)
41-
self.player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
41+
player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
4242
}
4343

4444
public convenience init(cassetteName: String, bundle: NSBundle = testingBundle(), turntableConfiguration: TurntableConfiguration = TurntableConfiguration(), delegateQueue: NSOperationQueue? = nil) {
@@ -54,42 +54,41 @@ public final class Turntable: NSURLSession {
5454
}
5555

5656
// MARK: - Private methods
57-
58-
private func playVinyl(request: NSURLRequest, completionHandler: RequestCompletionHandler) throws -> NSURLSessionDataTask {
59-
60-
guard let player = self.player else {
57+
58+
private func playVinyl<URLSessionTask: URLSessionTaskType>(request request: NSURLRequest, fromData bodyData: NSData? = nil, completionHandler: RequestCompletionHandler) throws -> URLSessionTask {
59+
60+
guard let player = player else {
6161
fatalError("Did you forget to load the Vinyl? 🎶")
6262
}
63-
64-
let completion = try player.playTrack(forRequest: request)
65-
63+
64+
let completion = try player.playTrack(forRequest: transformRequest(request, bodyData: bodyData))
65+
6666
return URLSessionTask {
6767
self.operationQueue.addOperationWithBlock {
6868
completionHandler(completion.data, completion.response, completion.error)
6969
}
7070
}
7171
}
72-
73-
private func playVinyl(request: NSURLRequest, fromData bodyData: NSData?, completionHandler: RequestCompletionHandler) throws -> NSURLSessionUploadTask {
74-
75-
guard let player = self.player else {
76-
fatalError("Did you forget to load the Vinyl? 🎶")
77-
}
78-
79-
guard let uploadRequest = request.mutableCopy() as? NSMutableURLRequest else {
80-
fatalError("💥")
72+
73+
private func transformRequest(request: NSURLRequest, bodyData: NSData? = nil) -> NSURLRequest {
74+
guard let bodyData = bodyData else {
75+
return request
8176
}
82-
uploadRequest.HTTPBody = bodyData
83-
let completion = try player.playTrack(forRequest: uploadRequest)
84-
85-
return URLSessionUploadTask {
86-
self.operationQueue.addOperationWithBlock {
87-
completionHandler(completion.data, completion.response, completion.error)
88-
}
77+
78+
guard let mutableRequest = request.mutableCopy() as? NSMutableURLRequest else {
79+
fatalError("💥 Houston, we have a problem 🚀")
8980
}
81+
82+
mutableRequest.HTTPBody = bodyData
83+
84+
return mutableRequest
9085
}
9186

92-
// MARK: - NSURLSession methods
87+
}
88+
89+
// MARK: - NSURLSession methods
90+
91+
extension Turntable {
9392

9493
public override func dataTaskWithURL(url: NSURL, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) -> NSURLSessionDataTask {
9594
let request = NSURLRequest(URL: url)
@@ -99,7 +98,7 @@ public final class Turntable: NSURLSession {
9998
public override func dataTaskWithRequest(request: NSURLRequest, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) -> NSURLSessionDataTask {
10099

101100
do {
102-
return try playVinyl(request, completionHandler: completionHandler)
101+
return try playVinyl(request: request, completionHandler: completionHandler) as URLSessionDataTask
103102
}
104103
catch Error.TrackNotFound {
105104
errorHandler.handleTrackNotFound(request, playTracksUniquely: turntableConfiguration.playTracksUniquely)
@@ -108,13 +107,13 @@ public final class Turntable: NSURLSession {
108107
errorHandler.handleUnknownError()
109108
}
110109

111-
return URLSessionTask(completion: {})
110+
return URLSessionDataTask(completion: {})
112111
}
113112

114113
public override func uploadTaskWithRequest(request: NSURLRequest, fromData bodyData: NSData?, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) -> NSURLSessionUploadTask {
115114

116115
do {
117-
return try playVinyl(request, fromData: bodyData, completionHandler: completionHandler)
116+
return try playVinyl(request: request, fromData: bodyData, completionHandler: completionHandler) as URLSessionUploadTask
118117
}
119118
catch Error.TrackNotFound {
120119
errorHandler.handleTrackNotFound(request, playTracksUniquely: turntableConfiguration.playTracksUniquely)
@@ -131,25 +130,29 @@ public final class Turntable: NSURLSession {
131130
}
132131
}
133132

133+
// MARK: - Loading Methods
134+
134135
extension Turntable {
135136

136137
public func loadVinyl(vinylName: String, bundle: NSBundle = testingBundle()) {
137138

138139
let vinyl = Vinyl(plastic: Turntable.createVinylPlastic(vinylName, bundle: bundle))
139-
self.player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
140+
player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
140141
}
141142

142143
public func loadCassette(cassetteName: String, bundle: NSBundle = testingBundle()) {
143144

144145
let vinyl = Vinyl(plastic: Turntable.createCassettePlastic(cassetteName, bundle: bundle))
145-
self.player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
146+
player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
146147
}
147148

148149
public func loadVinyl(vinyl: Vinyl) {
149-
self.player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
150+
player = Turntable.createPlayer(vinyl, configuration: turntableConfiguration)
150151
}
151152
}
152153

154+
// MARK: - Bootstrap methods
155+
153156
extension Turntable {
154157

155158
private static func createPlayer(vinyl: Vinyl, configuration: TurntableConfiguration) -> Player {

Vinyl/URLSessionTask.swift renamed to Vinyl/URLSessionDataTask.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// URLSessionTask.swift
2+
// URLSessionDataTask.swift
33
// Vinyl
44
//
55
// Created by Rui Peres on 16/02/2016.
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
public final class URLSessionTask: NSURLSessionDataTask {
11+
public final class URLSessionDataTask: NSURLSessionDataTask, URLSessionTaskType {
1212

1313
private let completion: Void -> Void
1414

Vinyl/URLSessionTaskType.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// URLSessionTaskType.swift
3+
// Vinyl
4+
//
5+
// Created by David Rodrigues on 30/03/16.
6+
// Copyright © 2016 Velhotes. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
protocol URLSessionTaskType {
12+
init(completion: Void -> Void)
13+
}

Vinyl/URLSessionUploadTask.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
public final class URLSessionUploadTask: NSURLSessionUploadTask {
11+
public final class URLSessionUploadTask: NSURLSessionUploadTask, URLSessionTaskType {
1212

1313
private let completion: Void -> Void
1414

0 commit comments

Comments
 (0)