Skip to content

Commit

Permalink
feat: add media type for random meme model
Browse files Browse the repository at this point in the history
  • Loading branch information
daoseng33 committed Oct 8, 2024
1 parent 0f7deb1 commit e45c201
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/HumorAPIService/General/Utility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,16 @@ public struct Utility {
}
return data
}

/// type string example: "video/mp4", "image/jpeg"
static func getMediaType(with typeString: String) -> MemeMediaType {
// get "video" or "image"
let components = typeString.components(separatedBy: "/")
guard let firstPart = components.first,
let type = MemeMediaType(rawValue: firstPart) else {
return .image
}

return type
}
}
3 changes: 3 additions & 0 deletions Sources/HumorAPIService/Models/RandomMeme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class RandomMeme: Object, Decodable, TimeSensitive, Favoritable {
}
@Persisted public var urlString: String
@Persisted public var type: String
public var mediaType: MemeMediaType {
return Utility.getMediaType(with: type)
}
@Persisted public var createdAt: Date = Date()
@Persisted public var isFavorite: Bool = false

Expand Down
2 changes: 2 additions & 0 deletions Tests/HumorAPIServiceTests/MemeAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct MemeAPITests: @unchecked Sendable {
case .success(let random):
assert(random.id == 831819)
assert(random.type == "image/png")
assert(random.mediaType == .image)

case .failure(_):
break
Expand All @@ -35,6 +36,7 @@ struct MemeAPITests: @unchecked Sendable {
case .success(let random):
assert(random.id == 12142)
assert(random.type == "video/mp4")
assert(random.mediaType == .video)

case .failure(_):
break
Expand Down

0 comments on commit e45c201

Please sign in to comment.