-
Notifications
You must be signed in to change notification settings - Fork 6
/
internet.scala
356 lines (310 loc) · 11.7 KB
/
internet.scala
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (c) 2020 etspaceman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package faker
import scala.util.Try
import java.net.IDN
import org.apache.commons.lang3.StringUtils
import org.scalacheck.{Arbitrary, Gen}
import pureconfig.ConfigReader
import faker.lorem.{LoremCharacters, LoremWord}
import faker.name.{FirstName, LastName, UserName}
object internet {
object FakerIDN {
def toASCII(in: String): String =
Try(IDN.toASCII(in)).getOrElse(
in.toList
.flatMap(x => Try(IDN.toASCII(x.toString)).toOption.toList)
.mkString
)
}
/** Generates a random avatar url based on a collection of profile pictures of
* real people. All this avatar have been authorized by its awesome users to
* be used on live websites (not just mockups). For more information, please
* visit: http://uifaces.com/authorized
*
* @see
* <a href="http://uifaces.com/authorized">Authorized UI Faces</a>
*/
type Avatar = Avatar.Type
object Avatar extends Newtype[String] { self =>
def avatars(implicit loader: ResourceLoader): Seq[String] =
loader.loadKey[Seq[String]]("internet.avatars")
implicit def avatarArbitrary(implicit
loader: ResourceLoader
): Arbitrary[Avatar] =
Arbitrary(
Gen
.oneOf(avatars)
.map(x => s"https://s3.amazonaws.com/uifaces/faces/twitter/$x")
.map(self.apply)
)
}
type DomainSuffix = DomainSuffix.Type
object DomainSuffix extends Newtype[String] { self =>
def domainSuffixes(implicit loader: ResourceLoader): Seq[DomainSuffix] =
loader.loadKey[Seq[DomainSuffix]]("internet.domain-suffixes")
implicit def domainSuffixArbitrary(implicit
loader: ResourceLoader
): Arbitrary[DomainSuffix] =
Arbitrary(Gen.oneOf(domainSuffixes))
implicit val domainSuffixConfigReader: ConfigReader[DomainSuffix] =
ConfigReader[String].map(self.apply)
}
type DomainWord = DomainWord.Type
object DomainWord extends Newtype[String] { self =>
implicit def domainWordArbitrary(implicit
loader: ResourceLoader
): Arbitrary[DomainWord] =
Arbitrary(
Arbitrary
.arbitrary[LastName]
.map(ln =>
FakerIDN.toASCII(
ln.value.toLowerCase().replaceAll("'", "").replaceAll(" ", "")
)
)
.map(self.apply)
)
}
type DomainName = DomainName.Type
object DomainName extends Newtype[String] { self =>
implicit def domainNameArbitrary(implicit
loader: ResourceLoader
): Arbitrary[DomainName] =
Arbitrary {
(for {
word <- Arbitrary.arbitrary[DomainWord]
suffix <- Arbitrary.arbitrary[DomainSuffix]
} yield s"${word.value}.${suffix.value}").map(self.apply)
}
}
type EmailAddress = EmailAddress.Type
object EmailAddress extends Newtype[String] { self =>
def freeEmailDomains(implicit loader: ResourceLoader): Seq[String] =
loader.loadKey[Seq[String]]("internet.free-email-domains")
implicit def emailAddressArbitrary(implicit
loader: ResourceLoader
): Arbitrary[EmailAddress] =
Arbitrary(
(for {
localPart <-
Arbitrary
.arbitrary[UserName]
.map(x => StringUtils.stripAccents(x.value))
domain <-
Gen
.oneOf(freeEmailDomains)
.map(FakerIDN.toASCII)
} yield s"$localPart@$domain").map(self.apply)
)
}
/** Generates a random image url based on the lorempixel service. All the
* images provided by this service are released under the creative commons
* license (CC BY-SA). For more information, please visit:
* http://lorempixel.com/
*
* @see
* <a href="http://loremflickr.com/">lorempixel</a>
*/
type Image = Image.Type
object Image extends Newtype[String] { self =>
def imageDimensions(implicit loader: ResourceLoader): Seq[String] =
loader.loadKey[Seq[String]]("internet.image.dimensions")
def imageCategories(implicit loader: ResourceLoader): Seq[String] =
loader.loadKey[Seq[String]]("internet.image.categories")
implicit def imageArbitrary(implicit
loader: ResourceLoader
): Arbitrary[Image] =
Arbitrary {
(for {
dimension <- Gen.oneOf(imageDimensions)
category <- Gen.oneOf(imageCategories)
split = dimension.split("x").map(_.trim)
} yield s"https://loremflickr.com/${split.head}/${split(1)}/$category")
.map(self.apply)
}
}
type IpV4Address = IpV4Address.Type
object IpV4Address extends Newtype[String] { self =>
implicit val ipV4AddressArbitrary: Arbitrary[IpV4Address] =
Arbitrary(
(for {
part1 <- Gen.choose(2, 255)
part2 <- Gen.choose(0, 255)
part3 <- Gen.choose(0, 255)
part4 <- Gen.choose(0, 255)
} yield s"$part1.$part2.$part3.$part4").map(self.apply)
)
}
type IpV4Cidr = IpV4Cidr.Type
object IpV4Cidr extends Newtype[String] { self =>
implicit val ipV4CidrArbitrary: Arbitrary[IpV4Cidr] =
Arbitrary(
(for {
ipV4Address <- Arbitrary.arbitrary[IpV4Address]
cidrPart <- Gen.choose(1, 31)
} yield s"${ipV4Address.value}/$cidrPart").map(self.apply)
)
}
type IpV6Address = IpV6Address.Type
object IpV6Address extends Newtype[String] { self =>
def ipV6Char: Gen[String] = Gen.choose(0, 15).map(Integer.toHexString)
def ipV6Part: Gen[String] = Gen.listOfN(4, ipV6Char).map(_.mkString)
implicit val ipV6AddressArbitrary: Arbitrary[IpV6Address] = Arbitrary(
Gen.listOfN(8, ipV6Part).map(x => x.mkString(":")).map(self.apply)
)
}
type IpV6Cidr = IpV6Cidr.Type
object IpV6Cidr extends Newtype[String] { self =>
implicit val ipV6CidrArbitrary: Arbitrary[IpV6Cidr] = Arbitrary(
(for {
ipv6 <- Arbitrary.arbitrary[IpV6Address]
cidrPart <- Gen.choose(1, 127)
} yield s"${ipv6.value}/$cidrPart").map(self.apply)
)
}
type MacAddress = MacAddress.Type
object MacAddress extends Newtype[String] { self =>
private def macAddressChar: Gen[String] =
Gen.choose(0, 15).map(Integer.toHexString)
private def macAddressSection =
macAddressChar.flatMap(x => macAddressChar.map(y => x + y))
implicit val macAddressArbitrary: Arbitrary[MacAddress] = Arbitrary(
Gen
.listOfN(6, macAddressSection)
.map(x => x.mkString(":"))
.map(self.apply)
)
}
type Password = Password.Type
object Password extends Newtype[String] { self =>
def passwordSpecialCharacters(implicit
loader: ResourceLoader
): Seq[Char] =
loader.loadKey[Seq[Char]]("internet.password.special-characters")
implicit def passwordArbitrary(implicit
loader: ResourceLoader
): Arbitrary[Password] =
Arbitrary(
(for {
charNum <- Gen.choose(8, 20)
pass <-
Gen
.listOfN(
charNum,
Gen.frequency(
4 -> Gen.oneOf(LoremCharacters.loremCharacters),
1 -> Gen.oneOf(passwordSpecialCharacters)
)
)
.map(_.mkString)
} yield pass).map(self.apply)
)
}
type PrivateIpV4Address = PrivateIpV4Address.Type
object PrivateIpV4Address extends Newtype[String] { self =>
private val privateFirstParts: Seq[Int] = Seq(10, 127, 169, 192, 172)
private val privateSecondPartsFor172: Seq[Int] =
Seq(16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31)
implicit val privateIpV4AddressArbitrary: Arbitrary[PrivateIpV4Address] =
Arbitrary(
(for {
part1 <- Gen.oneOf(privateFirstParts)
part2 <- part1 match {
case 172 => Gen.oneOf(privateSecondPartsFor172)
case 192 => Gen.const(168)
case 169 => Gen.const(254)
case _ => Gen.choose(2, 255)
}
part3 <- Gen.choose(0, 255)
part4 <- Gen.choose(0, 255)
} yield s"$part1.$part2.$part3.$part4").map(self.apply)
)
}
type PublicIpV4Address = PublicIpV4Address.Type
object PublicIpV4Address extends Newtype[String] { self =>
private val privateFirstParts: Seq[Int] = Seq(10, 127, 169, 192, 172)
private val publicIpV4FirstPart: Gen[Int] =
Gen.choose(2, 255).retryUntil(!privateFirstParts.contains(_))
implicit val publicIpV4AddressArbitrary: Arbitrary[PublicIpV4Address] =
Arbitrary(
(for {
ipAddress <- Arbitrary.arbitrary[IpV4Address]
origParts = ipAddress.value.split("\\.").toList.map(_.toInt)
publicFirstPart <- publicIpV4FirstPart
newParts = publicFirstPart +: origParts.drop(1)
} yield newParts.mkString(".")).map(self.apply)
)
}
type SafeEmailAddress = SafeEmailAddress.Type
object SafeEmailAddress extends Newtype[String] { self =>
def safeEmailDomains(implicit loader: ResourceLoader): Seq[String] =
loader.loadKey[Seq[String]]("internet.safe-email-domains")
implicit def safeEmailAddressArbitrary(implicit
loader: ResourceLoader
): Arbitrary[SafeEmailAddress] =
Arbitrary(
(for {
localPart <-
Arbitrary
.arbitrary[UserName]
.map(x => StringUtils.stripAccents(x.value))
domain <- Gen.oneOf(safeEmailDomains).map(FakerIDN.toASCII)
} yield s"$localPart@$domain").map(self.apply)
)
}
type Slug = Slug.Type
object Slug extends Newtype[String] { self =>
implicit def slugArbitrary(implicit
loader: ResourceLoader
): Arbitrary[Slug] =
Arbitrary(
Gen
.listOfN(2, Arbitrary.arbitrary[LoremWord])
.map(x => x.map(_.value).mkString("_"))
.map(self.apply)
)
}
type Url = Url.Type
object Url extends Newtype[String] { self =>
implicit def urlArbitrary(implicit loader: ResourceLoader): Arbitrary[Url] =
Arbitrary(
(for {
firstName <- Arbitrary.arbitrary[FirstName]
firstNameProcessed = firstName.value.toLowerCase().replaceAll("'", "")
domainWord <- Arbitrary.arbitrary[DomainWord]
domainSuffix <- Arbitrary.arbitrary[DomainSuffix]
} yield s"www.$firstNameProcessed-${domainWord.value}.${domainSuffix.value}")
.map(self.apply)
)
}
type UserAgent = UserAgent.Type
object UserAgent extends Newtype[String] { self =>
def userAgents(loader: ResourceLoader): Map[String, Seq[String]] =
loader.loadKey[Map[String, Seq[String]]](s"internet.user-agents")
implicit def userAgentArbitrary(implicit
loader: ResourceLoader
): Arbitrary[UserAgent] =
Arbitrary(
Gen.oneOf(userAgents(loader).values.flatten).map(self.apply)
)
}
}