|
| 1 | +public struct StatsPostDetails { |
| 2 | + public let fetchedDate: Date |
| 3 | + public let totalViewsCount: Int |
| 4 | + |
| 5 | + public let recentWeeks: [StatsWeeklyBreakdown] |
| 6 | + public let dailyAveragesPerMonth: [StatsPostViews] |
| 7 | + public let monthlyBreakdown: [StatsPostViews] |
| 8 | + public let lastTwoWeeks: [StatsPostViews] |
| 9 | +} |
| 10 | + |
| 11 | +public struct StatsWeeklyBreakdown { |
| 12 | + public let startDay: DateComponents |
| 13 | + public let endDay: DateComponents |
| 14 | + |
| 15 | + public let totalViewsCount: Int |
| 16 | + public let averageViewsCount: Int |
| 17 | + public let changePercentage: Double |
| 18 | + |
| 19 | + public let days: [StatsPostViews] |
| 20 | +} |
| 21 | + |
| 22 | +public struct StatsPostViews { |
| 23 | + public let period: StatsPeriodUnit |
| 24 | + public let date: DateComponents |
| 25 | + public let viewsCount: Int |
| 26 | +} |
| 27 | + |
| 28 | +extension StatsPostDetails { |
| 29 | + init?(jsonDictionary: [String: AnyObject]) { |
| 30 | + guard |
| 31 | + let fetchedDateString = jsonDictionary["date"] as? String, |
| 32 | + let date = type(of: self).dateFormatter.date(from: fetchedDateString), |
| 33 | + let totalViewsCount = jsonDictionary["views"] as? Int, |
| 34 | + let monthlyBreakdown = jsonDictionary["years"] as? [String: AnyObject], |
| 35 | + let monthlyAverages = jsonDictionary["averages"] as? [String: AnyObject], |
| 36 | + let recentWeeks = jsonDictionary["weeks"] as? [[String: AnyObject]], |
| 37 | + let data = jsonDictionary["data"] as? [[Any]] |
| 38 | + else { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + self.fetchedDate = date |
| 43 | + self.totalViewsCount = totalViewsCount |
| 44 | + |
| 45 | + // It's very hard to describe the format of this response. I tried to make the parsing |
| 46 | + // as nice and readable as possible, but in all honestly it's still pretty nasty. |
| 47 | + // If you want to see an example response to see how weird this response is, check out |
| 48 | + // `stats-post-details.json`. |
| 49 | + self.recentWeeks = StatsPostViews.mapWeeklyBreakdown(jsonDictionary: recentWeeks) |
| 50 | + self.monthlyBreakdown = StatsPostViews.mapMonthlyBreakdown(jsonDictionary: monthlyBreakdown) |
| 51 | + self.dailyAveragesPerMonth = StatsPostViews.mapMonthlyBreakdown(jsonDictionary: monthlyAverages) |
| 52 | + self.lastTwoWeeks = StatsPostViews.mapDailyData(data: Array(data.suffix(14))) |
| 53 | + } |
| 54 | + |
| 55 | + static var dateFormatter: DateFormatter { |
| 56 | + let df = DateFormatter() |
| 57 | + df.locale = Locale(identifier: "en_US_POS") |
| 58 | + df.dateFormat = "yyyy-MM-dd" |
| 59 | + return df |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +extension StatsPostViews { |
| 64 | + static func mapMonthlyBreakdown(jsonDictionary: [String: AnyObject]) -> [StatsPostViews] { |
| 65 | + return jsonDictionary.flatMap { yearKey, value -> [StatsPostViews] in |
| 66 | + guard |
| 67 | + let yearInt = Int(yearKey), |
| 68 | + let monthsDict = value as? [String: AnyObject], |
| 69 | + let months = monthsDict["months"] as? [String: Int] |
| 70 | + else { |
| 71 | + return [] |
| 72 | + } |
| 73 | + |
| 74 | + return months.compactMap { monthKey, value in |
| 75 | + guard |
| 76 | + let month = Int(monthKey) |
| 77 | + else { |
| 78 | + return nil |
| 79 | + } |
| 80 | + |
| 81 | + return StatsPostViews(period: .month, |
| 82 | + date: DateComponents(year: yearInt, month: month), |
| 83 | + viewsCount: value) |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +extension StatsPostViews { |
| 90 | + static func mapWeeklyBreakdown(jsonDictionary: [[String: AnyObject]]) -> [StatsWeeklyBreakdown] { |
| 91 | + return jsonDictionary.compactMap { |
| 92 | + guard |
| 93 | + let totalViews = $0["total"] as? Int, |
| 94 | + let averageViews = $0["average"] as? Int, |
| 95 | + let days = $0["days"] as? [[String: AnyObject]] |
| 96 | + else { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + let change = ($0["change"] as? Double) ?? 0.0 |
| 101 | + |
| 102 | + let mappedDays: [StatsPostViews] = days.compactMap { |
| 103 | + guard |
| 104 | + let dayString = $0["day"] as? String, |
| 105 | + let date = StatsPostDetails.dateFormatter.date(from: dayString), |
| 106 | + let viewsCount = $0["count"] as? Int |
| 107 | + else { |
| 108 | + return nil |
| 109 | + } |
| 110 | + |
| 111 | + return StatsPostViews(period: .day, |
| 112 | + date: Calendar.autoupdatingCurrent.dateComponents([.year, .month, .day], from: date), |
| 113 | + viewsCount: viewsCount) |
| 114 | + } |
| 115 | + |
| 116 | + guard !mappedDays.isEmpty else { |
| 117 | + return nil |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + return StatsWeeklyBreakdown(startDay: mappedDays.first!.date, |
| 122 | + endDay: mappedDays.last!.date, |
| 123 | + totalViewsCount: totalViews, |
| 124 | + averageViewsCount: averageViews, |
| 125 | + changePercentage: change, |
| 126 | + days: mappedDays) |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +extension StatsPostViews { |
| 133 | + static func mapDailyData(data: [[Any]]) -> [StatsPostViews] { |
| 134 | + return data.compactMap { |
| 135 | + guard |
| 136 | + let dateString = $0[0] as? String, |
| 137 | + let date = StatsPostDetails.dateFormatter.date(from: dateString), |
| 138 | + let viewsCount = $0[1] as? Int |
| 139 | + else { |
| 140 | + return nil |
| 141 | + } |
| 142 | + |
| 143 | + return StatsPostViews(period: .day, |
| 144 | + date: Calendar.autoupdatingCurrent.dateComponents([.year, .month, .day], from: date), |
| 145 | + viewsCount: viewsCount) |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments