forked from elixirs/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternet.ex
More file actions
331 lines (277 loc) · 8 KB
/
Copy pathinternet.ex
File metadata and controls
331 lines (277 loc) · 8 KB
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
defmodule Faker.Internet do
alias Faker.Lorem
alias Faker.Person.En, as: Person
alias Faker.Util
import Faker.Util, only: [pick: 1]
import Faker, only: [localize: 1]
@moduledoc """
Functions for generating internet related data
"""
@doc """
Returns a complete random domain name
## Examples
iex> Faker.Internet.domain_name()
"blick.org"
iex> Faker.Internet.domain_name()
"schumm.info"
iex> Faker.Internet.domain_name()
"sipes.com"
iex> Faker.Internet.domain_name()
"hane.info"
"""
@spec domain_name() :: String.t()
def domain_name do
"#{domain_word()}.#{domain_suffix()}"
end
@doc """
Returns a random domain suffix
## Examples
iex> Faker.Internet.domain_suffix()
"com"
iex> Faker.Internet.domain_suffix()
"org"
iex> Faker.Internet.domain_suffix()
"name"
iex> Faker.Internet.domain_suffix()
"info"
"""
@spec domain_suffix() :: String.t()
localize(:domain_suffix)
@doc """
Returns a random username
## Examples
iex> Faker.Internet.user_name()
"elizabeth2056"
iex> Faker.Internet.user_name()
"reese1921"
iex> Faker.Internet.user_name()
"aniya1972"
iex> Faker.Internet.user_name()
"bianka2054"
"""
@spec user_name() :: String.t()
def user_name, do: user_name(Faker.random_between(0, 1))
defp user_name(0) do
"#{remove_special_characters(Person.first_name()) |> String.replace(~s( ), ~s()) |> String.downcase()}#{Faker.random_between(1900, 2100)}"
end
defp user_name(1) do
[Person.first_name(), Person.last_name()]
|> Stream.map(&remove_special_characters/1)
|> Enum.map_join(Util.pick(~w(. _)), &String.replace(&1, ~s( ), ~s()))
|> String.downcase()
end
@doc """
Returns a random domain word
## Examples
iex> Faker.Internet.domain_word()
"blick"
iex> Faker.Internet.domain_word()
"hayes"
iex> Faker.Internet.domain_word()
"schumm"
iex> Faker.Internet.domain_word()
"rolfson"
"""
@spec domain_word() :: String.t()
def domain_word do
"#{remove_special_characters(Person.last_name()) |> String.split(["'"]) |> Enum.join() |> String.downcase()}"
end
@doc """
Returns a complete email based on a domain name
## Examples
iex> Faker.Internet.email()
"elizabeth2056@rolfson.net"
iex> Faker.Internet.email()
"conor2058@schiller.com"
iex> Faker.Internet.email()
"frederique2063@metz.name"
iex> Faker.Internet.email()
"jana2042@price.biz"
"""
@spec email() :: String.t()
def email do
"#{remove_special_characters(user_name())}@#{domain_name()}"
end
@doc """
Returns a complete free email based on a free email service [gmail, yahoo, hotmail]
## Examples
iex> Faker.Internet.free_email()
"elizabeth2056@hotmail.com"
iex> Faker.Internet.free_email()
"trycia1982@hotmail.com"
iex> Faker.Internet.free_email()
"delphine_hartmann@yahoo.com"
iex> Faker.Internet.free_email()
"mitchel_rutherford@yahoo.com"
"""
@spec free_email() :: String.t()
def free_email do
"#{remove_special_characters(user_name())}@#{free_email_service()}"
end
@doc """
Returns a safe email
## Examples
iex> Faker.Internet.safe_email()
"elizabeth2056@example.net"
iex> Faker.Internet.safe_email()
"trycia1982@example.net"
iex> Faker.Internet.safe_email()
"delphine_hartmann@example.com"
iex> Faker.Internet.safe_email()
"mitchel_rutherford@example.com"
"""
@spec safe_email() :: String.t()
def safe_email do
"#{remove_special_characters(user_name())}@example.#{Util.pick(~w(org com net))}"
end
@doc """
Returns a free email service
## Examples
iex> Faker.Internet.free_email_service()
"gmail.com"
iex> Faker.Internet.free_email_service()
"hotmail.com"
iex> Faker.Internet.free_email_service()
"gmail.com"
iex> Faker.Internet.free_email_service()
"hotmail.com"
"""
@spec free_email_service() :: String.t()
localize(:free_email_service)
@doc """
Returns a random url
## Examples
iex> Faker.Internet.url()
"http://hayes.name"
iex> Faker.Internet.url()
"http://sipes.com"
iex> Faker.Internet.url()
"http://padberg.name"
iex> Faker.Internet.url()
"http://hartmann.biz"
"""
@spec url() :: String.t()
def url, do: url(Faker.random_between(0, 1))
defp url(0), do: "http://#{domain_name()}"
defp url(1), do: "https://#{domain_name()}"
@doc """
Returns a random image url from placekitten.com | placehold.it | dummyimage.com
## Examples
iex> Faker.Internet.image_url()
"https://placekitten.com/131/131"
iex> Faker.Internet.image_url()
"https://placekitten.com/554/554"
iex> Faker.Internet.image_url()
"https://picsum.photos/936"
iex> Faker.Internet.image_url()
"https://picsum.photos/541"
"""
@spec image_url() :: String.t()
def image_url, do: image_url(Faker.random_between(0, 2))
defp image_url(0) do
size = Faker.random_between(10, 1024)
"https://placekitten.com/#{size}/#{size}"
end
defp image_url(1) do
size = Faker.random_between(10, 1024)
"https://picsum.photos/#{size}"
end
defp image_url(2) do
size = Faker.random_between(10, 1024)
"https://dummyimage.com/#{size}x#{size}"
end
@doc """
Generates an ipv4 address
## Examples
iex> Faker.Internet.ip_v4_address()
"214.217.139.136"
iex> Faker.Internet.ip_v4_address()
"200.102.244.150"
iex> Faker.Internet.ip_v4_address()
"219.212.222.123"
iex> Faker.Internet.ip_v4_address()
"164.141.15.82"
"""
@spec ip_v4_address() :: String.t()
def ip_v4_address do
Enum.map_join(1..4, ".", fn _part ->
Faker.random_between(0, 255)
end)
end
@doc """
Generates an ipv6 address
## Examples
iex> Faker.Internet.ip_v6_address()
"A2D6:F5D9:BD8B:C588:0DC8:9566:43F4:B196"
iex> Faker.Internet.ip_v6_address()
"05DB:FAD4:88DE:397B:75A4:A98D:DF0F:1F52"
iex> Faker.Internet.ip_v6_address()
"6229:4EFA:2F7B:FEF9:EB07:0128:85B2:DC72"
iex> Faker.Internet.ip_v6_address()
"E867:34BC:715B:FB7C:7E96:AF4F:C733:A82D"
"""
@spec ip_v6_address() :: String.t()
def ip_v6_address do
Enum.map_join(1..8, ":", fn _part ->
Faker.random_between(0, 65535)
|> Integer.to_string(16)
|> String.pad_leading(4, ["0"])
end)
end
@doc """
Generates a mac address
## Examples
iex> Faker.Internet.mac_address()
"d6:d9:8b:88:c8:66"
iex> Faker.Internet.mac_address()
"f4:96:db:d4:de:7b"
iex> Faker.Internet.mac_address()
"a4:8d:0f:52:29:fa"
iex> Faker.Internet.mac_address()
"7b:f9:07:28:b2:72"
"""
@spec mac_address() :: String.t()
def mac_address do
1..6
|> Enum.map_join(":", &format_mac_address/1)
|> String.downcase()
end
@doc """
Generates a slug
If no words are provided it will generate 2 to 5 random words
If no glue is provided it will use one of -, _ or .
## Examples
iex> Faker.Internet.slug()
"sint-deleniti-consequatur-ut"
iex> Faker.Internet.slug()
"sit_et"
iex> Faker.Internet.slug(["foo", "bar"])
"foo-bar"
iex> Faker.Internet.slug(["foo", "bar"], ["."])
"foo.bar"
"""
@spec slug() :: String.t()
def slug do
slug(Lorem.words(2..5))
end
@spec slug([String.t()]) :: String.t()
def slug(words) do
slug(words, ["-", "_", "."])
end
@spec slug([String.t()], [String.t()]) :: String.t()
def slug(words, glue) do
words
|> Enum.take_random(length(words))
|> Enum.join(pick(glue))
|> String.downcase()
end
defp format_mac_address(_part) do
Faker.random_between(0, 255)
|> Integer.to_string(16)
|> String.pad_leading(2, ["0"])
end
defp remove_special_characters(string) do
special_characters_pattern = :binary.compile_pattern(["'", "\""])
String.replace(string, special_characters_pattern, "")
end
end