Skip to content

Commit

Permalink
Use NSRUL instead of String for images
Browse files Browse the repository at this point in the history
  • Loading branch information
Reda Lemeden committed Jan 31, 2016
1 parent 4674250 commit 92118e6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
47 changes: 21 additions & 26 deletions Source/ImageScout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public typealias ScoutCompletionBlock = (NSError?, CGSize, ScoutedImageType) ->

let unsupportedFormatErrorMessage = "Unsupported image format. ImageScout only supports PNG, GIF, and JPEG."
let unableToParseErrorMessage = "Scouting operation failed. The remote image is likely malformated or corrupt."
let invalidURIErrorMessage = "Invalid URI parameter."

let errorDomain = "ImageScoutErrorDomain"

Expand All @@ -27,27 +26,35 @@ public class ImageScout {
sessionDelegate.scout = self
}

/// Takes a URL string and a completion block and returns void.
/// Takes an `NSURL` and a completion block.
/// The completion block takes an optional error, a size, and an image type,
/// and returns void.

public func scoutImageWithURI(URI: String, completion: ScoutCompletionBlock) {
guard let URL = NSURL(string: URI) else {
let URLError = ImageScout.error(invalidURIErrorMessage, code: 100)
return completion(URLError, CGSizeZero, ScoutedImageType.Unsupported)
}

public func scoutImageWithURL(URL: NSURL, completion: ScoutCompletionBlock) {
let operation = ScoutOperation(task: session.dataTaskWithURL(URL))
operation.completionBlock = { [unowned self] in
completion(operation.error, operation.size, operation.type)
self.operations[URI] = nil
self.operations[URL.absoluteString] = nil
}

addOperation(operation, withURI: URI)
addOperation(operation, withURI: URL.absoluteString)
}

// MARK: Delegate Methods
// MARK: - Private Methods

private func addOperation(operation: ScoutOperation, withURI URI: String) {
operations[URI] = operation
queue.addOperation(operation)
}

// MARK: - Class Methods

class func error(message: String, code: Int) -> NSError {
return NSError(domain: errorDomain, code:code, userInfo:[NSLocalizedDescriptionKey: message])
}
}

extension ImageScout {
func didReceiveData(data: NSData, task: NSURLSessionDataTask) {
guard let requestURL = task.currentRequest?.URL?.absoluteString else { return }
guard let operation = operations[requestURL] else { return }
Expand All @@ -56,23 +63,11 @@ public class ImageScout {
}

func didCompleteWithError(error: NSError?, task: NSURLSessionDataTask) {
guard let requestURL = task.currentRequest?.URL?.absoluteString else { return }
guard let operation = operations[requestURL] else { return }
guard let requestURL = task.currentRequest?.URL?.absoluteString,
let operation = operations[requestURL]
else { return }

let completionError = error ?? ImageScout.error(unableToParseErrorMessage, code: 101)
operation.terminateWithError(completionError)
}

// MARK: Private Methods

private func addOperation(operation: ScoutOperation, withURI URI: String) {
operations[URI] = operation
queue.addOperation(operation)
}

// MARK: Class Methods

class func error(message: String, code: Int) -> NSError {
return NSError(domain: errorDomain, code:code, userInfo:[NSLocalizedDescriptionKey: message])
}
}
2 changes: 1 addition & 1 deletion Source/ScoutOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ScoutOperation: NSOperation {
}

func terminateWithError(completionError: NSError) {
error = ImageScout.error(invalidURIErrorMessage, code: 100)
error = completionError
complete()
}

Expand Down
16 changes: 8 additions & 8 deletions Tests/ImageScoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class ImageScoutTests: XCTestCase {
func testScoutingJPEG() {
let scout = ImageScout()
let expectation = expectationWithDescription("Scout JPEG images")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "jpg")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "jpg")!

scout.scoutImageWithURI(imagePath!.absoluteString) { (error, size, type) -> () in
scout.scoutImageWithURL(imagePath) { (error, size, type) -> () in
expectation.fulfill()
XCTAssertEqual(size, CGSize(width: 500, height: 375), "Image size should be 500 by 375")
XCTAssertEqual(type, ScoutedImageType.JPEG, "Image type should be JPEG")
Expand All @@ -30,9 +30,9 @@ class ImageScoutTests: XCTestCase {
func testScoutingPNG() {
let scout = ImageScout()
let expectation = expectationWithDescription("Scout PNG images")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "png")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "png")!

scout.scoutImageWithURI(imagePath!.absoluteString) { (error, size, type) -> () in
scout.scoutImageWithURL(imagePath) { (error, size, type) -> () in
expectation.fulfill()
XCTAssertEqual(size, CGSize(width: 500, height: 375), "Image size should be 500 by 375")
XCTAssertEqual(type, ScoutedImageType.PNG, "Image type should be PNG")
Expand All @@ -45,9 +45,9 @@ class ImageScoutTests: XCTestCase {
func testScoutingGIF() {
let scout = ImageScout()
let expectation = expectationWithDescription("Scout GIF images")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "gif")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "gif")!

scout.scoutImageWithURI(imagePath!.absoluteString) { (error, size, type) -> () in
scout.scoutImageWithURL(imagePath) { (error, size, type) -> () in
expectation.fulfill()
XCTAssertEqual(size, CGSize(width: 500, height: 375), "Image size should be 500 by 375")
XCTAssertEqual(type, ScoutedImageType.GIF, "Image type should be GIF")
Expand All @@ -60,9 +60,9 @@ class ImageScoutTests: XCTestCase {
func testScoutingUnsupported() {
let scout = ImageScout()
let expectation = expectationWithDescription("Ignore unsupported formats")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "bmp")
let imagePath = NSBundle(forClass: ImageScoutTests.self).URLForResource("scout", withExtension: "bmp")!

scout.scoutImageWithURI(imagePath!.absoluteString) { (error, size, type) -> () in
scout.scoutImageWithURL(imagePath) { (error, size, type) -> () in
expectation.fulfill()
XCTAssertEqual(size, CGSizeZero, "Image size should be 0 by 0")
XCTAssertEqual(type, ScoutedImageType.Unsupported ,"Image type should be Unsupported")
Expand Down

0 comments on commit 92118e6

Please sign in to comment.