<commit_before>//
//  SubscriptionHistoryRequest.swift
//  Rocket.Chat
//
//  Created by <NAME> on 12/26/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import SwiftyJSON
import Foundation

fileprivate extension SubscriptionType {
    var path: String {
        switch self {
        case .channel:
            return "/api/v1/channels.history"
        case .group:
            return "/api/v1/groups.history"
        case .directMessage:
            return "/api/v1/dm.history"
        }
    }
}

fileprivate extension String {
    mutating func appendIfNotNil<T: CustomStringConvertible>(_ stringConvertible: T?, transform: ((T) -> String)?) {
        if let stringConvertible = stringConvertible {
            self += transform?(stringConvertible) ?? stringConvertible.description
        }
    }
}

class SubscriptionHistoryRequest: APIRequest {
    typealias APIResourceType = SubscriptionHistoryResource

    var path: String {
        return roomType.path
    }

    var query: String?

    let roomType: SubscriptionType
    let roomId: String?
    let latest: Date?
    let oldest: Date?
    let inclusive: Bool?
    let count: Int?
    let unreads: Bool?

    init(
        roomType: SubscriptionType,
        roomId: String,
        latest: Date? = nil,
        oldest: Date? = nil,
        inclusive: Bool? = nil,
        count: Int? = nil,
        unreads: Bool? = nil
    ) {
        self.roomType = roomType
        self.roomId = roomId
        self.latest = latest
        self.oldest = oldest
        self.inclusive = inclusive
        self.count = count
        self.unreads = unreads

        var query = "roomId=\(roomId)"

        query.appendIfNotNil(latest) {
            "&latest=\($0.formatted(Date.apiDateFormat, timeZone: TimeZone(abbreviation: "UTC")))"
        }

        query.appendIfNotNil(oldest) {
            "&oldest=\($0.formatted(Date.apiDateFormat, timeZone: TimeZone(abbreviation: "UTC")))"
        }

        query.appendIfNotNil(inclusive) { "&inclusive=\($0)" }
        query.appendIfNotNil(count) { "&count=\($0)" }
        query.appendIfNotNil(unreads) { "&unreads=\($0)" }

        self.query = query
    }
}

class SubscriptionHistoryResource: APIResource {
    var success: Bool? {
        return raw?["success"].bool
    }
}<commit_msg>Improve code<commit_after>//
//  SubscriptionHistoryRequest.swift
//  Rocket.Chat
//
//  Created by <NAME> on 12/26/18.
//  Copyright © 2018 Rocket.Chat. All rights reserved.
//

import SwiftyJSON
import Foundation

fileprivate extension SubscriptionType {
    var path: String {
        switch self {
        case .channel:
            return "/api/v1/channels.history"
        case .group:
            return "/api/v1/groups.history"
        case .directMessage:
            return "/api/v1/dm.history"
        }
    }
}

fileprivate extension String {
    mutating func appendIfNotNil<T: CustomStringConvertible>(_ stringConvertible: T?, transform: ((T) -> String)?) {
        if let stringConvertible = stringConvertible {
            self += transform?(stringConvertible) ?? stringConvertible.description
        }
    }
}

class SubscriptionHistoryRequest: APIRequest {
    typealias APIResourceType = SubscriptionHistoryResource

    var path: String {
        return roomType.path
    }

    var query: String?

    let roomType: SubscriptionType
    let roomId: String?
    let latest: Date?
    let oldest: Date?
    let inclusive: Bool?
    let count: Int?
    let unreads: Bool?

    init(
        roomType: SubscriptionType,
        roomId: String,
        latest: Date? = nil,
        oldest: Date? = nil,
        inclusive: Bool? = nil,
        count: Int? = nil,
        unreads: Bool? = nil
    ) {
        self.roomType = roomType
        self.roomId = roomId
        self.latest = latest
        self.oldest = oldest
        self.inclusive = inclusive
        self.count = count
        self.unreads = unreads

        var query = "roomId=\(roomId)"

        let timeZone = TimeZone(abbreviation: "UTC")

        query.appendIfNotNil(latest) {
            "&latest=\($0.formatted(Date.apiDateFormat, timeZone: timeZone))"
        }

        query.appendIfNotNil(oldest) {
            "&oldest=\($0.formatted(Date.apiDateFormat, timeZone: timeZone))"
        }

        query.appendIfNotNil(inclusive) { "&inclusive=\($0)" }
        query.appendIfNotNil(count) { "&count=\($0)" }
        query.appendIfNotNil(unreads) { "&unreads=\($0)" }

        self.query = query
    }
}

class SubscriptionHistoryResource: APIResource {
    var success: Bool? {
        return raw?["success"].bool
    }
}
{'fraction_non_alphanumeric': 0.11320754716981132, 'fraction_numerical': 0.007807417046193884, 'mean_word_length': 2.247887323943662, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1793816', 'n_tokens_mistral': 1422, 'n_tokens_neox': 1363, 'n_words': 315}