This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathMultireddit.swift
166 lines (148 loc) · 5.27 KB
/
Multireddit.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Multireddit.swift
// reddift
//
// Created by sonson on 2015/05/19.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import Foundation
/**
Type of Multireddit icon.
*/
public enum MultiredditIconName: String {
case artAndDesign = "art and design"
case ask = "ask"
case books = "books"
case business = "business"
case cars = "cars"
case comics = "comics"
case cuteAnimals = "cute animals"
case diy = "diy"
case entertainment = "entertainment"
case foodAndDrink = "food and drink"
case funny = "funny"
case games = "games"
case grooming = "grooming"
case health = "health"
case lifeAdvice = "life advice"
case military = "military"
case modelsPinup = "models pinup"
case music = "music"
case news = "news"
case philosophy = "philosophy"
case picturesAndGifs = "pictures and gifs"
case science = "science"
case shopping = "shopping"
case sports = "sports"
case style = "style"
case tech = "tech"
case travel = "travel"
case unusualStories = "unusual stories"
case video = "video"
case none = "None"
init(_ name: String) {
self = MultiredditIconName(rawValue: name) ?? .none
}
}
/**
Type of Multireddit visibility.
*/
public enum MultiredditVisibility: String {
case `private` = "private"
case `public` = "public"
case hidden = "hidden"
init(_ type: String) {
self = MultiredditVisibility(rawValue: type) ?? .private
}
}
/**
Type of Multireddit weighting scheme.
*/
public enum MultiredditWeightingScheme: String {
case classic = "classic"
case fresh = "fresh"
init(_ type: String) {
self = MultiredditWeightingScheme(rawValue: type) ?? .classic
}
}
/**
Multireddit class.
*/
public struct Multireddit: SubredditURLPath, Created {
public var descriptionMd: String
public var displayName: String
public var iconName: MultiredditIconName
public var keyColor: String
public var subreddits: [String]
public var visibility: MultiredditVisibility
public var weightingScheme: MultiredditWeightingScheme
// can not update following attritubes
public let descriptionHtml: String
public let path: String
public let name: String
public let iconUrl: String
public let canEdit: Bool
public let copiedFrom: String
public let created: Int
public let createdUtc: Int
public init(name: String) {
self.descriptionMd = ""
self.displayName = name
self.iconName = MultiredditIconName("")
self.visibility = MultiredditVisibility("")
self.keyColor = ""
self.subreddits = []
self.weightingScheme = MultiredditWeightingScheme("")
self.descriptionHtml = ""
self.path = name
self.name = name
self.iconUrl = ""
self.canEdit = false
self.copiedFrom = ""
self.created = 0
self.createdUtc = 0
}
public init(json: JSONDictionary) {
descriptionMd = json["description_md"] as? String ?? ""
displayName = json["display_name"] as? String ?? ""
iconName = MultiredditIconName(json["icon_name"] as? String ?? "")
visibility = MultiredditVisibility(json["visibility"] as? String ?? "")
keyColor = json["key_color"] as? String ?? ""
var buf: [String] = []
if let temp = json["subreddits"] as? [JSONDictionary] {
for element in temp {
if let element = element as? [String: String], let name = element["name"] {
buf.append(name)
}
}
}
subreddits = buf
weightingScheme = MultiredditWeightingScheme(json["weighting_scheme"] as? String ?? "")
descriptionHtml = json["description_html"] as? String ?? ""
path = json["path"] as? String ?? ""
name = json["name"] as? String ?? ""
iconUrl = json["icon_url"] as? String ?? ""
canEdit = json["can_edit"] as? Bool ?? false
copiedFrom = json["copied_from"] as? String ?? ""
created = json["created"] as? Int ?? 0
createdUtc = json["created_utc"] as? Int ?? 0
}
/**
Create new multireddit path as String from "/user/sonson_twit/m/testmultireddit12" replacing its name with "newMultiredditName". For example, returns ""/user/sonson_twit/m/newmulti" when path is "/user/sonson_twit/m/testmultireddit12" and newMultiredditName is "newmulti".
- parameter newMultiredditName: New display name for path.
- returns: new path as String.
*/
public func multiredditPathReplacingNameWith(_ newMultiredditName: String) throws -> String {
do {
let regex = try NSRegularExpression(pattern: "^/user/(.+?)/m/", options: .caseInsensitive)
if let match = regex.firstMatch(in: self.path, options: [], range: NSRange(location: 0, length: self.path.utf16.count)) {
if match.numberOfRanges > 1 {
let range = match.range(at: 1)
let userName = (self.path as NSString).substring(with: range)
return "/user/\(userName)/m/\(newMultiredditName)"
}
}
throw NSError(domain: "", code: 0, userInfo: nil)
} catch { throw error }
}
}