Skip to content

Commit

Permalink
Add support for additional Giphy APIs, and a small bit of refactoring…
Browse files Browse the repository at this point in the history
… for the API layer.
  • Loading branch information
amyleecodes committed Mar 15, 2017
1 parent aa7fbdc commit ab7201e
Show file tree
Hide file tree
Showing 12 changed files with 847 additions and 274 deletions.
46 changes: 8 additions & 38 deletions Library/GiphyImageSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,15 @@ public struct GiphyImageSet: Mappable {

public fileprivate(set) var height: Int = 0

public fileprivate(set) var size: UInt64 = 0
public fileprivate(set) var size: Int = 0

public fileprivate(set) var mp4URL: URL?

public fileprivate(set) var mp4Size: UInt64 = 0
public fileprivate(set) var mp4Size: Int = 0

public fileprivate(set) var webpURL: URL?

public fileprivate(set) var webpSize: UInt64 = 0

fileprivate var widthString: String = "" {
didSet {
width = Int(widthString) ?? 0
}
}

fileprivate var heightString: String = "" {
didSet {
height = Int(heightString) ?? 0
}
}

fileprivate var sizeString: String = "" {
didSet {
size = UInt64(sizeString) ?? 0
}
}

fileprivate var mp4SizeString: String = "" {
didSet {
mp4Size = UInt64(mp4SizeString) ?? 0
}
}

fileprivate var webpSizeString: String = "" {
didSet {
webpSize = UInt64(webpSizeString) ?? 0
}
}
public fileprivate(set) var webpSize: Int = 0

public init?(map: Map)
{
Expand All @@ -65,12 +35,12 @@ public struct GiphyImageSet: Mappable {
mutating public func mapping(map: Map) {

url <- (map["url"], URLTransform())
widthString <- map["width"]
heightString <- map["height"]
sizeString <- map["size"]
width <- (map["width"], stringToIntTransform)
height <- (map["height"], stringToIntTransform)
size <- (map["size"], stringToIntTransform)
mp4URL <- (map["mp4"], URLTransform())
mp4SizeString <- map["mp4_size"]
mp4Size <- (map["mp4_size"], stringToIntTransform)
webpURL <- (map["webp"], URLTransform())
webpSizeString <- map["webp_size"]
webpSize <- (map["webp_size"], stringToIntTransform)
}
}
2 changes: 1 addition & 1 deletion Library/GiphyMultipleGIFResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct GiphyMultipleGIFResponse: Mappable {
meta <- map["meta"]
}

func gifsSmallerThan(sizeInBytes: UInt64, forWidth: CGFloat) -> [GiphyItem]
func gifsSmallerThan(sizeInBytes: Int, forWidth: CGFloat) -> [GiphyItem]
{
return gifs.filter({
let size = $0.imageSetClosestTo(width: forWidth, animated: true)?.size ?? 0
Expand Down
89 changes: 89 additions & 0 deletions Library/GiphySimpleItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// GiphySimpleItem.swift
// Pods
//
// Created by Brendan Lee on 3/15/17.
//
//

import Foundation
import ObjectMapper

public struct GiphySimpleItem: Mappable {

public fileprivate(set) var type: String = "gif"

public fileprivate(set) var identifier: String = ""

public fileprivate(set) var url: URL?

// Any of these could be nil
public fileprivate(set) var imageOriginalURL: URL?
public fileprivate(set) var imageURL: URL?
public fileprivate(set) var imagemp4URL: URL?
public fileprivate(set) var imageFrameCount: Int = 0
public fileprivate(set) var imageWidth: Int = 0
public fileprivate(set) var imageHeight: Int = 0

public fileprivate(set) var fixedHeightDownsampledURL: URL?
public fileprivate(set) var fixedHeightDownsampledWidth: Int = 0
public fileprivate(set) var fixedHeightDownsampledHeight: Int = 0

public fileprivate(set) var fixedWidthDownsampledURL: URL?
public fileprivate(set) var fixedWidthDownsampledWidth: Int = 0
public fileprivate(set) var fixedWidthDownsampledHeight: Int = 0

public fileprivate(set) var fixedHeightSmallURL: URL?
public fileprivate(set) var fixedHeightSmallStillURL: URL?
public fileprivate(set) var fixedHeightSmallWidth: Int = 0
public fileprivate(set) var fixedHeightSmallHeight: Int = 0

public fileprivate(set) var fixedWidthSmallURL: URL?
public fileprivate(set) var fixedWidthSmallStillURL: URL?
public fileprivate(set) var fixedWidthSmallWidth: Int = 0
public fileprivate(set) var fixedWidthSmallHeight: Int = 0

public init?(map: Map)
{

}

public mutating func mapping(map: Map) {

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

let dateTransformer = DateFormatterTransform(dateFormatter: dateFormatter)

type <- map["type"]
identifier <- map["id"]
url <- (map["url"], URLTransform())

imageOriginalURL <- (map["image_original_url"], URLTransform())
imageURL <- (map["image_url"], URLTransform())
imagemp4URL <- (map["image_mp4_url"], URLTransform())
imageFrameCount <- (map["image_frames"], stringToIntTransform)
imageWidth <- (map["image_width"], stringToIntTransform)
imageHeight <- (map["image_height"], stringToIntTransform)

fixedHeightDownsampledURL <- (map["fixed_height_downsampled_url"], URLTransform())
fixedHeightDownsampledWidth <- (map["fixed_height_downsampled_width"], stringToIntTransform)
fixedHeightDownsampledHeight <- (map["fixed_height_downsampled_height"], stringToIntTransform)

fixedWidthDownsampledURL <- (map["fixed_width_downsampled_url"], URLTransform())
fixedWidthDownsampledHeight <- (map["fixed_width_downsampled_height"], stringToIntTransform)
fixedWidthDownsampledWidth <- (map["fixed_width_downsampled_width"], stringToIntTransform)

fixedHeightSmallURL <- (map["fixed_height_small_url"], URLTransform())
fixedHeightSmallStillURL <- (map["fixed_height_small_still_url"], URLTransform())
fixedHeightSmallWidth <- (map["fixed_height_small_width"], stringToIntTransform)
fixedHeightSmallHeight <- (map["fixed_height_small_height"], stringToIntTransform)

fixedWidthSmallURL <- (map["fixed_width_small_url"], URLTransform())
fixedWidthSmallStillURL <- (map["fixed_width_small_still_url"], URLTransform())
fixedWidthSmallWidth <- (map["fixed_width_small_width"], stringToIntTransform)
fixedWidthSmallHeight <- (map["fixed_width_small_height"], stringToIntTransform)
}

}
28 changes: 28 additions & 0 deletions Library/GiphySimpleSingleGIFResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// GiphySimpleSingleGIFResponse.swift
// Pods
//
// Created by Brendan Lee on 3/15/17.
//
//

import Foundation
import ObjectMapper

public struct GiphySimpleSingleGIFResponse: Mappable {

public fileprivate(set) var gif: GiphySimpleItem?

public fileprivate(set) var meta: GiphyMeta?

public init?(map: Map)
{

}

mutating public func mapping(map: Map) {

gif <- map["data"]
meta <- map["meta"]
}
}
28 changes: 28 additions & 0 deletions Library/GiphySingleGIFResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// GiphySingleGIFResponse.swift
// Pods
//
// Created by Brendan Lee on 3/15/17.
//
//

import Foundation
import ObjectMapper

public struct GiphySingleGIFResponse: Mappable {

public fileprivate(set) var gif: GiphyItem?

public fileprivate(set) var meta: GiphyMeta?

public init?(map: Map)
{

}

mutating public func mapping(map: Map) {

gif <- map["data"]
meta <- map["meta"]
}
}
35 changes: 35 additions & 0 deletions Library/GiphyUser.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// GiphyUser.swift
// Pods
//
// Created by Brendan Lee on 3/15/17.
//
//

import Foundation
import ObjectMapper

public struct GiphyUser: Mappable {

public fileprivate(set) var avatarURL: URL?
public fileprivate(set) var bannerURL: URL?
public fileprivate(set) var profileURL: URL?
public fileprivate(set) var username: String = ""
public fileprivate(set) var displayName: String = ""
public fileprivate(set) var twitterHandle: String?

public init?(map: Map)
{

}

mutating public func mapping(map: Map) {

avatarURL <- (map["avatar_url"], URLTransform())
bannerURL <- (map["banner_url"], URLTransform())
profileURL <- (map["profile_url"], URLTransform())
username <- map["username"]
displayName <- map["display_name"]
twitterHandle <- map["twitter"]
}
}
21 changes: 21 additions & 0 deletions Library/ParsingTransforms.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ParsingTransforms.swift
// Pods
//
// Created by Brendan Lee on 3/15/17.
//
//

import Foundation
import ObjectMapper

let stringToIntTransform = TransformOf<Int, String>(fromJSON: { (value: String?) -> Int in

return Int(value ?? "0") ?? 0
}, toJSON: { (value: Int?) -> String? in
// transform value from Int? to String?
if let value = value {
return String(value)
}
return nil
})
Loading

0 comments on commit ab7201e

Please sign in to comment.