Parse Giphy API responses.

// FREEBIE
pull/1/head
Matthew Chen 8 years ago
parent b4d29bd5d8
commit 30a77c5977

@ -17,11 +17,11 @@ enum GiphyFormat {
let url: NSURL let url: NSURL
init(format: GiphyFormat, init(format: GiphyFormat,
name: String, name: String,
width: UInt, width: UInt,
height: UInt, height: UInt,
fileSize: UInt, fileSize: UInt,
url: NSURL) { url: NSURL) {
self.format = format self.format = format
self.name = name self.name = name
self.width = width self.width = width
@ -86,7 +86,7 @@ enum GiphyFormat {
Logger.error("\(GifManager.TAG) Couldn't create session manager.") Logger.error("\(GifManager.TAG) Couldn't create session manager.")
return return
} }
guard let baseUrl = NSURL(string:kGiphyBaseURL) else { guard NSURL(string:kGiphyBaseURL) != nil else {
Logger.error("\(GifManager.TAG) Invalid base URL.") Logger.error("\(GifManager.TAG) Invalid base URL.")
return return
} }
@ -112,11 +112,11 @@ enum GiphyFormat {
parameters: {}, parameters: {},
progress:nil, progress:nil,
success: { _, value in success: { _, value in
Logger.error("\(GifManager.TAG) ---- success: \(value)") Logger.error("\(GifManager.TAG) search request succeeded")
self.parseGiphyImages(responseJson:value) self.parseGiphyImages(responseJson:value)
}, },
failure: { _, error in failure: { _, error in
Logger.error("\(GifManager.TAG) ---- failure: \(error)") Logger.error("\(GifManager.TAG) search request failed: \(error)")
}) })
} }
@ -145,21 +145,21 @@ enum GiphyFormat {
private func parseGiphyImage(imageDict: [String:Any]) -> GiphyImageInfo? { private func parseGiphyImage(imageDict: [String:Any]) -> GiphyImageInfo? {
guard let giphyId = imageDict["id"] as? String else { guard let giphyId = imageDict["id"] as? String else {
Logger.error("\(GifManager.TAG) Image dict missing id.") Logger.warn("\(GifManager.TAG) Image dict missing id.")
return nil return nil
} }
guard giphyId.characters.count > 0 else { guard giphyId.characters.count > 0 else {
Logger.error("\(GifManager.TAG) Image dict has invalid id.") Logger.warn("\(GifManager.TAG) Image dict has invalid id.")
return nil return nil
} }
guard let renditionDicts = imageDict["images"] as? [String:Any] else { guard let renditionDicts = imageDict["images"] as? [String:Any] else {
Logger.error("\(GifManager.TAG) Image dict missing renditions.") Logger.warn("\(GifManager.TAG) Image dict missing renditions.")
return nil return nil
} }
var renditions = [GiphyRendition]() var renditions = [GiphyRendition]()
for (renditionName, renditionDict) in renditionDicts { for (renditionName, renditionDict) in renditionDicts {
guard let renditionDict = renditionDict as? [String:Any] else { guard let renditionDict = renditionDict as? [String:Any] else {
Logger.error("\(GifManager.TAG) Invalid rendition dict.") Logger.warn("\(GifManager.TAG) Invalid rendition dict.")
continue continue
} }
guard let rendition = parseGiphyRendition(renditionName:renditionName, guard let rendition = parseGiphyRendition(renditionName:renditionName,
@ -169,7 +169,7 @@ enum GiphyFormat {
renditions.append(rendition) renditions.append(rendition)
} }
guard renditions.count > 0 else { guard renditions.count > 0 else {
Logger.error("\(GifManager.TAG) Image has no valid renditions.") Logger.warn("\(GifManager.TAG) Image has no valid renditions.")
return nil return nil
} }
Logger.debug("\(GifManager.TAG) Image successfully parsed.") Logger.debug("\(GifManager.TAG) Image successfully parsed.")
@ -189,24 +189,24 @@ enum GiphyFormat {
return nil return nil
} }
guard let urlString = renditionDict["url"] as? String else { guard let urlString = renditionDict["url"] as? String else {
Logger.error("\(GifManager.TAG) Rendition missing url.") Logger.debug("\(GifManager.TAG) Rendition missing url.")
return nil return nil
} }
guard urlString.characters.count > 0 else { guard urlString.characters.count > 0 else {
Logger.error("\(GifManager.TAG) Rendition has invalid url.") Logger.warn("\(GifManager.TAG) Rendition has invalid url.")
return nil return nil
} }
guard let url = NSURL(string:urlString) else { guard let url = NSURL(string:urlString) else {
Logger.error("\(GifManager.TAG) Rendition url could not be parsed.") Logger.warn("\(GifManager.TAG) Rendition url could not be parsed.")
return nil return nil
} }
guard let fileExtension = url.pathExtension else { guard let fileExtension = url.pathExtension else {
Logger.error("\(GifManager.TAG) Rendition url missing file extension.") Logger.warn("\(GifManager.TAG) Rendition url missing file extension.")
return nil return nil
} }
Logger.error("\(GifManager.TAG) fileExtension: \(fileExtension).") Logger.error("\(GifManager.TAG) fileExtension: \(fileExtension).")
guard fileExtension.lowercased() == "gif" else { guard fileExtension.lowercased() == "gif" else {
Logger.error("\(GifManager.TAG) Rendition has invalid type: \(fileExtension).") Logger.debug("\(GifManager.TAG) Rendition has invalid type: \(fileExtension).")
return nil return nil
} }
@ -233,19 +233,19 @@ enum GiphyFormat {
// } // }
private func parsePositiveUInt(dict: [String:Any], key: String, typeName: String) -> UInt? { private func parsePositiveUInt(dict: [String:Any], key: String, typeName: String) -> UInt? {
guard let value = dict[key] else { guard let value = dict[key] else {
Logger.error("\(GifManager.TAG) \(typeName) missing \(key).") Logger.debug("\(GifManager.TAG) \(typeName) missing \(key).")
return nil return nil
} }
guard let stringValue = value as? String else { guard let stringValue = value as? String else {
Logger.error("\(GifManager.TAG) \(typeName) has invalid \(key): \(value).") Logger.warn("\(GifManager.TAG) \(typeName) has invalid \(key): \(value).")
return nil return nil
} }
guard let parsedValue = UInt(stringValue) else { guard let parsedValue = UInt(stringValue) else {
Logger.error("\(GifManager.TAG) \(typeName) has invalid \(key): \(stringValue).") Logger.warn("\(GifManager.TAG) \(typeName) has invalid \(key): \(stringValue).")
return nil return nil
} }
guard parsedValue > 0 else { guard parsedValue > 0 else {
Logger.error("\(GifManager.TAG) \(typeName) has non-positive \(key): \(parsedValue).") Logger.debug("\(GifManager.TAG) \(typeName) has non-positive \(key): \(parsedValue).")
return nil return nil
} }
return parsedValue return parsedValue

Loading…
Cancel
Save