From e05fdf049e32965e3ef9028255cd67722b5a8fb2 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 22 Jun 2015 21:34:44 -0400 Subject: [PATCH 1/6] Remove parens to promote trailing closure syntax --- DVR/Cassette.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DVR/Cassette.swift b/DVR/Cassette.swift index d86460c..8fd83b0 100644 --- a/DVR/Cassette.swift +++ b/DVR/Cassette.swift @@ -27,7 +27,7 @@ extension Cassette { var dictionary: [String: AnyObject] { return [ "name": name, - "interactions": interactions.map() { $0.dictionary } + "interactions": interactions.map { $0.dictionary } ] } From 2a05bc3659890ce58b4699f8158d617cee0b3cef Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 22 Jun 2015 21:35:17 -0400 Subject: [PATCH 2/6] Use flatMap to avoid mutability --- DVR/Cassette.swift | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/DVR/Cassette.swift b/DVR/Cassette.swift index 8fd83b0..8e16ceb 100644 --- a/DVR/Cassette.swift +++ b/DVR/Cassette.swift @@ -36,14 +36,10 @@ extension Cassette { self.name = name - var interactions = [Interaction]() if let array = dictionary["interactions"] as? [[String: AnyObject]] { - for dictionary in array { - if let interaction = Interaction(dictionary: dictionary) { - interactions.append(interaction) - } - } + interactions = array.flatMap { Interaction(dictionary: $0) } + } else { + interactions = [] } - self.interactions = interactions } } From 4cda281f1a0a47358ddcf05d75ec6a2608d89b89 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 22 Jun 2015 21:36:10 -0400 Subject: [PATCH 3/6] Infer types wherever possible --- DVR/Session.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DVR/Session.swift b/DVR/Session.swift index 22c098d..65436ad 100644 --- a/DVR/Session.swift +++ b/DVR/Session.swift @@ -6,7 +6,7 @@ public class Session: NSURLSession { public var outputDirectory: String public let cassetteName: String - public var recordingEnabled: Bool = true + public var recordingEnabled = true private let testBundle: NSBundle From 3cf2110e678ec784c836d81e8232b27d04889e16 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 22 Jun 2015 21:36:51 -0400 Subject: [PATCH 4/6] Avoid extra return SessionDataTask similarly catches to "[d]o nothing". --- DVR/Session.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/DVR/Session.swift b/DVR/Session.swift index 65436ad..a84c8c4 100644 --- a/DVR/Session.swift +++ b/DVR/Session.swift @@ -39,9 +39,7 @@ public class Session: NSURLSession { if let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] { return Cassette(dictionary: json) } - } catch { - return nil - } + } catch {} return nil } } From 8d1eed3035483ae933ee79586e8e115b09b35a2f Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Mon, 22 Jun 2015 21:37:46 -0400 Subject: [PATCH 5/6] Avoid mutability when possible, shorter, and clear --- DVR/URLResponse.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/DVR/URLResponse.swift b/DVR/URLResponse.swift index 54dd2dc..10295b9 100644 --- a/DVR/URLResponse.swift +++ b/DVR/URLResponse.swift @@ -17,13 +17,11 @@ class URLResonse: NSURLResponse { extension NSURLResponse { var dictionary: [String: AnyObject] { - var dictionary = [String: AnyObject]() - if let url = URL?.absoluteString { - dictionary["url"] = url + return ["url": url] } - return dictionary + return [:] } } From ecabe603c5b01bcb9f84ad8465e1a2f6d118fc75 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Tue, 23 Jun 2015 07:18:59 -0400 Subject: [PATCH 6/6] Use fatalError instead of print/abort --- DVR/SessionDataTask.swift | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/DVR/SessionDataTask.swift b/DVR/SessionDataTask.swift index 4f90a3a..fba41c4 100644 --- a/DVR/SessionDataTask.swift +++ b/DVR/SessionDataTask.swift @@ -31,14 +31,12 @@ class SessionDataTask: NSURLSessionDataTask { } if cassette != nil { - print("[DVR] Invalid request. The request was not found in the cassette.") - abort() + fatalError("[DVR] Invalid request. The request was not found in the cassette.") } // Cassette is missing. Record. if session.recordingEnabled == false { - print("[DVR] Recording is disabled.") - abort() + fatalError("[DVR] Recording is disabled.") } // Create directory @@ -60,14 +58,12 @@ class SessionDataTask: NSURLSessionDataTask { let outputPath = outputDirectory.stringByAppendingPathComponent(self.session.cassetteName).stringByAppendingPathExtension("json")! let data = try NSJSONSerialization.dataWithJSONObject(cassette.dictionary, options: [.PrettyPrinted]) data.writeToFile(outputPath, atomically: true) - print("[DVR] Persisted cassette at \(outputPath). Please add this file to your test target") - abort() + fatalError("[DVR] Persisted cassette at \(outputPath). Please add this file to your test target") } catch { // Do nothing } - print("[DVR] Failed to persist cassette.") - abort() + fatalError("[DVR] Failed to persist cassette.") } task?.resume() }