Gravatar.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Gravatar.swift
  2. //
  3. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. import UIKit
  24. private extension String {
  25. var md5_hash: String {
  26. let trimmedString = lowercased().trimmingCharacters(in: CharacterSet.whitespaces)
  27. let utf8String = trimmedString.cString(using: String.Encoding.utf8)!
  28. let stringLength = CC_LONG(trimmedString.lengthOfBytes(using: String.Encoding.utf8))
  29. let digestLength = Int(CC_MD5_DIGEST_LENGTH)
  30. let result = UnsafeMutablePointer<CUnsignedChar>.allocate(capacity: digestLength)
  31. CC_MD5(utf8String, stringLength, result)
  32. var hash = ""
  33. for i in 0..<digestLength {
  34. hash += String(format: "%02x", result[i])
  35. }
  36. result.deallocate()
  37. return String(format: hash)
  38. }
  39. }
  40. // MARK: - QueryItemConvertible
  41. private protocol QueryItemConvertible {
  42. var queryItem: URLQueryItem {get}
  43. }
  44. // MARK: -
  45. public struct Gravatar {
  46. public enum DefaultImage: String, QueryItemConvertible {
  47. case HTTP404 = "404"
  48. case MysteryMan = "mm"
  49. case Identicon = "identicon"
  50. case MonsterID = "monsterid"
  51. case Wavatar = "wavatar"
  52. case Retro = "retro"
  53. case Blank = "blank"
  54. var queryItem: URLQueryItem {
  55. return URLQueryItem(name: "d", value: rawValue)
  56. }
  57. }
  58. public enum Rating: String, QueryItemConvertible {
  59. case G = "g"
  60. case PG = "pg"
  61. case R = "r"
  62. case X = "x"
  63. var queryItem: URLQueryItem {
  64. return URLQueryItem(name: "r", value: rawValue)
  65. }
  66. }
  67. public let email: String
  68. public let forceDefault: Bool
  69. public let defaultImage: DefaultImage
  70. public let rating: Rating
  71. fileprivate static let baseURL = Foundation.URL(string: "https://secure.gravatar.com/avatar")!
  72. public init(
  73. emailAddress: String,
  74. defaultImage: DefaultImage = .MysteryMan,
  75. forceDefault: Bool = false,
  76. rating: Rating = .PG)
  77. {
  78. self.email = emailAddress
  79. self.defaultImage = defaultImage
  80. self.forceDefault = forceDefault
  81. self.rating = rating
  82. }
  83. public func URL(size: CGFloat, scale: CGFloat = UIScreen.main.scale) -> Foundation.URL {
  84. let URL = Gravatar.baseURL.appendingPathComponent(email.md5_hash)
  85. var components = URLComponents(url: URL, resolvingAgainstBaseURL: false)!
  86. var queryItems = [defaultImage.queryItem, rating.queryItem]
  87. queryItems.append(URLQueryItem(name: "f", value: forceDefault ? "y" : "n"))
  88. queryItems.append(URLQueryItem(name: "s", value: String(format: "%.0f",size * scale)))
  89. components.queryItems = queryItems
  90. return components.url!
  91. }
  92. }