faker 3.2.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 96ec3715aa40eae20241ea0235f17234c691cbd808a5a5f523e2600979bc1c18
4
- data.tar.gz: e57effe991fbdbc74562d35066f8db66f46be5d3d441e7b650c720a072c23a94
3
+ metadata.gz: 5d53bbdb52aa714d065ea68bcddd91aad8ca346b3e00ce828e5a95a7f32fb9cc
4
+ data.tar.gz: 05fa5f30f28a2b00825253650e23ea433f26457c43bf822117628f7494b660a1
5
5
  SHA512:
6
- metadata.gz: 9a12b535d55c2e3d5559228f6b9eafd98a383692165670c7a80f4a986d1c3b23637d59faa69fcc2b06142ee2445e88ce4b8057bdf72f71eafd0c86baeb9147ed
7
- data.tar.gz: 844caa61e4c4be1975f0fc8355302627ff9ead7cf7d423dd78313a4876190482fcb471a95fb43509e0e761d0bac671484851edbaf3cf98325d7864c591b83e6e
6
+ metadata.gz: 253c37f13596516e56f85bfbd7e509ececcfa1dd77d9ceec3cd92d127fed1249a4197dbd1ccbb0699ee9895870eaa30d61f59288bc0d954fa9805f3526346539
7
+ data.tar.gz: 2e1f76f104cd9b0a3852366c26847692620129e795bb321ada197829b2d981a0a0e21f723597c741971a78a540597a84d0c86a2b9bda5fdf059e05f1f2b57c7f
data/README.md CHANGED
@@ -169,9 +169,7 @@ Faker::Config.locale = 'es'
169
169
  Faker::Config.locale = :es
170
170
  ```
171
171
 
172
- Note: Overriding the default locale might not be thread-safe. See [Locale setting can be ignored #2563](https://github.com/faker-ruby/faker/issues/2563) for more details.
173
-
174
- To override Faker's locales,
172
+ To override Faker's locales, and set it on threaded server environments
175
173
  check out the [locales README](lib/locales/README.md).
176
174
 
177
175
  ### Minitest and Faker >= 2.22
@@ -325,6 +323,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main
325
323
  <summary>Travel</summary>
326
324
 
327
325
  - [Faker:Travel::Airport](doc/travel/airport.md)
326
+ - [Faker:Travel::TrainStation](doc/travel/train_station.md)
328
327
  </details>
329
328
 
330
329
  <details>
@@ -346,6 +345,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main
346
345
  - [Faker::Games::Dota](doc/games/dota.md)
347
346
  - [Faker::Games::ElderScrolls](doc/games/elder_scrolls.md)
348
347
  - [Faker::Games::Fallout](doc/games/fallout.md)
348
+ - [Faker::Games::FinalFantasyXIV](doc/games/final_fantasy_xiv.md)
349
349
  - [Faker::Games::HalfLife](doc/games/half_life.md)
350
350
  - [Faker::Games::Heroes](doc/games/heroes.md)
351
351
  - [Faker::Games::HeroesOfTheStorm](doc/games/heroes_of_the_storm.md)
@@ -438,6 +438,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'main
438
438
  <summary>Tv Shows</summary>
439
439
 
440
440
  - [Faker::TvShows::AquaTeenHungerForce](doc/tv_shows/aqua_teen_hunger_force.md)
441
+ - [Faker::TvShows::Archer](doc/tv_shows/archer.md)
441
442
  - [Faker::TvShows::BigBangTheory](doc/tv_shows/big_bang_theory.md)
442
443
  - [Faker::TvShows::BojackHorseman](doc/tv_shows/bojack_horseman.md)
443
444
  - [Faker::TvShows::BreakingBad](doc/tv_shows/breaking_bad.md)
@@ -8,18 +8,19 @@ module Faker
8
8
  ##
9
9
  # Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits).
10
10
  #
11
- # @param min_rut [Integer] Specifies the minimum value of the rut.
12
- # @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
11
+ # @param min_rut [Integer] Specifies the minimum value of the RUT.
12
+ # @param max_rut [Integer] Specifies the maximum value of the RUT.
13
+ # @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
13
14
  # @return [Number]
14
15
  #
15
16
  # @example
16
17
  # Faker::ChileRut.rut #=> 11235813
17
- # Faker::ChileRut.rut(min_rut: 20890156) #=> 31853211
18
- # Faker::ChileRut.rut(min_rut: 20890156, fixed: true) #=> 20890156
18
+ # Faker::ChileRut.rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> 21853211
19
+ # Faker::ChileRut.rut(min_rut: 20_890_156, fixed: true) #=> 20890156
19
20
  #
20
- # @faker.version 1.9.2
21
- def rut(min_rut: 1, fixed: false)
22
- @last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999)
21
+ # @faker.version next
22
+ def rut(min_rut: 1, max_rut: 99_999_999, fixed: false)
23
+ @last_rut = fixed ? min_rut : rand_in_range(min_rut, max_rut)
23
24
  end
24
25
 
25
26
  ##
@@ -68,25 +69,30 @@ module Faker
68
69
  ##
69
70
  # Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits) with a dv (digito verificador, check-digit).
70
71
  #
71
- # @param min_rut [Integer] Specifies the minimum value of the rut.
72
- # @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
72
+ # @param min_rut [Integer] Specifies the minimum value of the RUT.
73
+ # @param max_rut [Integer] Specifies the maximum value of the RUT.
74
+ # @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
73
75
  # @return [String]
74
76
  #
75
77
  # @example
76
78
  # Faker::ChileRut.full_rut #=> "30686957-4"
77
- # Faker::ChileRut.full_rut(min_rut: 20890156) #=> "30686957-4"
78
- # Faker::ChileRut.full_rut(min_rut: 30686957, fixed: true) #=> "30686957-4"
79
+ # Faker::ChileRut.full_rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> "20686957-4"
80
+ # Faker::ChileRut.full_rut(min_rut: 30_686_957, fixed: true) #=> "30686957-4"
79
81
  #
80
82
  # @faker.version next
81
- def full_rut(min_rut: 0, fixed: false, formatted: false)
82
- if formatted
83
- "#{rut(min_rut: min_rut, fixed: fixed).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse}-#{dv}"
84
- else
85
- "#{rut(min_rut: min_rut, fixed: fixed)}-#{dv}"
86
- end
83
+ def full_rut(min_rut: 1, max_rut: 99_999_999, fixed: false, formatted: false)
84
+ this_rut = rut(min_rut: min_rut, max_rut: max_rut, fixed: fixed)
85
+ this_rut = format_rut(this_rut) if formatted
86
+ "#{this_rut}-#{dv}"
87
87
  end
88
88
 
89
89
  attr_reader :last_rut
90
+
91
+ private
92
+
93
+ def format_rut(rut)
94
+ rut.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse
95
+ end
90
96
  end
91
97
  end
92
98
  end
@@ -93,11 +93,23 @@ module Faker
93
93
  # @faker.version 2.2.0
94
94
  def nric(min_age: 18, max_age: 65)
95
95
  birthyear = Date.birthday(min_age: min_age, max_age: max_age).year
96
- prefix = birthyear < 2000 ? 'S' : 'T'
97
- values = birthyear.to_s[-2..]
98
- values << regexify(/\d{5}/)
99
- check_alpha = generate_nric_check_alphabet(values, prefix)
100
- "#{prefix}#{values}#{check_alpha}"
96
+
97
+ generate(:string) do |g|
98
+ g.computed(name: :prefix) do
99
+ if birthyear < 2000
100
+ 'S'
101
+ else
102
+ 'T'
103
+ end
104
+ end
105
+ g.computed(name: :yy) do
106
+ birthyear.to_s[-2..]
107
+ end
108
+ g.int(name: :values, length: 5)
109
+ g.computed(name: :check, deps: %i[prefix yy values]) do |prefix, yy, values|
110
+ generate_nric_check_alphabet("#{yy}#{values}", prefix)
111
+ end
112
+ end
101
113
  end
102
114
 
103
115
  ##
@@ -197,15 +209,29 @@ module Faker
197
209
  end
198
210
 
199
211
  def generate_base10_isbn
200
- values = regexify(/\d{9}/)
201
- remainder = sum(values) { |value, index| (index + 1) * value.to_i } % 11
202
- values << "-#{remainder == 10 ? 'X' : remainder}"
212
+ generate(:string) do |g|
213
+ g.int(name: :values, length: 9)
214
+ g.lit('-')
215
+ g.computed(name: :checksum, deps: [:values]) do |values|
216
+ remainder = sum(values.to_s) { |value, offset| (offset + 1) * value.to_i } % 11
217
+ if remainder == 10
218
+ 'X'
219
+ else
220
+ remainder.to_s
221
+ end
222
+ end
223
+ end
203
224
  end
204
225
 
205
226
  def generate_base13_isbn
206
- values = regexify(/\d{12}/)
207
- remainder = sum(values) { |value, index| index.even? ? value.to_i : value.to_i * 3 } % 10
208
- values << "-#{(10 - remainder) % 10}"
227
+ generate(:string) do |g|
228
+ g.int(name: :values, length: 12)
229
+ g.lit('-')
230
+ g.computed(name: :checksum, deps: [:values]) do |values|
231
+ remainder = sum(values.to_s) { |value, offset| offset.even? ? value.to_i : value.to_i * 3 } % 10
232
+ (10 - remainder) % 10
233
+ end
234
+ end
209
235
  end
210
236
 
211
237
  def sum(values)
@@ -215,15 +241,31 @@ module Faker
215
241
  end
216
242
 
217
243
  def generate_base8_ean
218
- values = regexify(/\d{7}/)
219
- check_digit = 10 - values.chars.each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT8[i] } % 10
220
- values << (check_digit == 10 ? 0 : check_digit).to_s
244
+ generate(:string) do |g|
245
+ g.int(name: :values, length: 7)
246
+ g.computed(name: :checksum, deps: [:values]) do |values|
247
+ check_digit = 10 - values.to_s.chars.each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT8[i] } % 10
248
+ if check_digit == 10
249
+ 0
250
+ else
251
+ check_digit
252
+ end
253
+ end
254
+ end
221
255
  end
222
256
 
223
257
  def generate_base13_ean
224
- values = regexify(/\d{12}/)
225
- check_digit = 10 - values.chars.each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT13[i] } % 10
226
- values << (check_digit == 10 ? 0 : check_digit).to_s
258
+ generate(:string) do |g|
259
+ g.int(name: :values, length: 12)
260
+ g.computed(name: :checksum, deps: [:values]) do |values|
261
+ check_digit = 10 - values.to_s.chars.each_with_index.inject(0) { |s, (v, i)| s + v.to_i * EAN_CHECK_DIGIT13[i] } % 10
262
+ if check_digit == 10
263
+ 0
264
+ else
265
+ check_digit
266
+ end
267
+ end
268
+ end
227
269
  end
228
270
 
229
271
  EAN_CHECK_DIGIT8 = [3, 1, 3, 1, 3, 1, 3].freeze
@@ -231,7 +273,7 @@ module Faker
231
273
 
232
274
  def rut_verificator_digit(rut)
233
275
  total = rut.to_s.rjust(8, '0').chars.zip(%w[3 2 7 6 5 4 3 2]).collect { |a, b| a.to_i * b.to_i }.inject(:+)
234
- (11 - total % 11).to_s.gsub(/10/, 'k').gsub(/11/, '0')
276
+ (11 - total % 11).to_s.gsub('10', 'k').gsub('11', '0')
235
277
  end
236
278
 
237
279
  def generate_nric_check_alphabet(values, prefix)
@@ -343,7 +343,12 @@ module Faker
343
343
  #
344
344
  # @faker.version 1.9.2
345
345
  def south_african_pty_ltd_registration_number
346
- regexify(%r{\d{4}/\d{4,10}/07})
346
+ generate(:string) do |g|
347
+ g.int(length: 4)
348
+ g.lit('/')
349
+ g.int(ranges: [1000..9_999_999_999])
350
+ g.lit('/07')
351
+ end
347
352
  end
348
353
 
349
354
  ##
@@ -356,7 +361,18 @@ module Faker
356
361
  #
357
362
  # @faker.version 1.9.2
358
363
  def south_african_close_corporation_registration_number
359
- regexify(%r{(CK\d{2}|\d{4})/\d{4,10}/23})
364
+ generate(:string) do |g|
365
+ g.oneof do |one|
366
+ one.group do |g_|
367
+ g_.lit('CK')
368
+ g_.int(length: 2)
369
+ end
370
+ one.int(length: 4)
371
+ end
372
+ g.lit('/')
373
+ g.int(ranges: [1000..9_999_999_999])
374
+ g.lit('/23')
375
+ end
360
376
  end
361
377
 
362
378
  ##
@@ -369,7 +385,12 @@ module Faker
369
385
  #
370
386
  # @faker.version 1.9.2
371
387
  def south_african_listed_company_registration_number
372
- regexify(%r{\d{4}/\d{4,10}/06})
388
+ generate(:string) do |g|
389
+ g.int(length: 4)
390
+ g.lit('/')
391
+ g.int(ranges: [1000..9_999_999_999])
392
+ g.lit('/06')
393
+ end
373
394
  end
374
395
 
375
396
  ##
@@ -382,7 +403,12 @@ module Faker
382
403
  #
383
404
  # @faker.version 1.9.2
384
405
  def south_african_trust_registration_number
385
- regexify(%r{IT\d{2,4}/\d{2,10}})
406
+ generate(:string) do |g|
407
+ g.lit('IT')
408
+ g.int(ranges: [10..9999])
409
+ g.lit('/')
410
+ g.int(ranges: [10..9_999_999_999])
411
+ end
386
412
  end
387
413
 
388
414
  ##
@@ -567,7 +593,7 @@ module Faker
567
593
  end
568
594
 
569
595
  control = control.to_s[-1].to_i
570
- control = control.zero? ? control : 10 - control
596
+ control = 10 - control unless control.zero?
571
597
 
572
598
  %w[A B C D E F G H J U V].include?(organization_type) ? control : letters[control]
573
599
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Faker
4
4
  class Date < Base
5
+ DAYS_OF_WEEK = %i[sunday monday tuesday wednesday thursday friday saturday].freeze
6
+
5
7
  class << self
6
8
  ##
7
9
  # Produce a random date between two dates.
@@ -53,18 +55,26 @@ module Faker
53
55
  ##
54
56
  # Produce a random date in the future (up to N days).
55
57
  #
58
+ # @param from [Integer] The start of the usable forward date range.
56
59
  # @param days [Integer] The maximum number of days to go into the future.
57
60
  # @return [Date]
58
61
  #
59
- # @example
62
+ # @example if used with or without Rails (Active Support)
60
63
  # Faker::Date.forward(days: 23) #=> #<Date: 2014-10-03>
61
64
  #
65
+ # @example if used with Rails (Active Support)
66
+ # Faker::Date.forward(from: Date.current, days: 17) #=> #<Date: 2022-06-22>
67
+ #
68
+ # @example if used with or without Rails (Active Support)
69
+ # Faker::Date.forward(from: '2022-06-03', days: 10) #=> #<Date: 2022-10-13>
70
+ #
62
71
  # @faker.version 1.0.0
63
- def forward(days: 365)
64
- from = ::Date.today + 1
65
- to = ::Date.today + days
72
+ def forward(from: ::Date.today, days: 365)
73
+ start_date = get_date_object(from)
74
+ since = start_date + 1
75
+ to = start_date + days
66
76
 
67
- between(from: from, to: to).to_date
77
+ between(from: since, to: to).to_date
68
78
  end
69
79
 
70
80
  ##
@@ -128,6 +138,52 @@ module Faker
128
138
  between(from: from, to: to).to_date
129
139
  end
130
140
 
141
+ ##
142
+ # Produce a random date at given day(s) of the week between two dates.
143
+ #
144
+ # @param day [Symbol, Array<Symbol>] # The day(s) of the week. See {DAYS_OF_WEEK}.
145
+ # @param from [Date, String] The start of the usable date range.
146
+ # @param to [Date, String] The end of the usable date range.
147
+ # @return [Date]
148
+ #
149
+ # @example if used with or without Rails (Active Support)
150
+ # Faker::Date.on_day_of_week_between(day: :tuesday, from: '2023-01-01', to: '2023-02-01') #=> #<Date: 2032-01-10>
151
+ #
152
+ # @example if used with Rails (Active Support)
153
+ # Faker::Date.on_day_of_week_between(day: [:saturday, :sunday], from: 1.month.ago, to: Date.today) #=> #<Date: 2014-09-24>
154
+ #
155
+ # @faker.version next
156
+ def on_day_of_week_between(day:, from:, to:)
157
+ days = [day].flatten
158
+ raise ArgumentError, 'Day of week cannot be empty' if days.empty?
159
+
160
+ # Convert given days of the week to numbers used by `Date#wday` method
161
+ numeric_weekdays = days.map do |d|
162
+ DAYS_OF_WEEK.index(d.to_sym.downcase) || raise(ArgumentError, "#{d} is not a valid day of the week")
163
+ end
164
+
165
+ from = get_date_object(from)
166
+ to = get_date_object(to)
167
+ date = Faker::Base.rand_in_range(from, to)
168
+
169
+ # If the initial date is not on one of the wanted days of the week...
170
+ unless numeric_weekdays.include? date.wday
171
+ # ...pick a date nearby that is on one of the wanted days of the week instead
172
+ date += sample(numeric_weekdays) - date.wday
173
+
174
+ # Move date 1 week earlier or later if the adjusted date is now outside the date range
175
+ date += 7 if date < from
176
+ date -= 7 if date > to
177
+
178
+ if date > to || date < from
179
+ raise ArgumentError,
180
+ "There is no #{DAYS_OF_WEEK[date.wday].capitalize} between #{from} and #{to}. Increase the from/to date range or choose a different day of the week."
181
+ end
182
+ end
183
+
184
+ date
185
+ end
186
+
131
187
  private
132
188
 
133
189
  def birthday_date(date, age)
@@ -98,17 +98,28 @@ module Faker
98
98
  end
99
99
 
100
100
  def gb_licence_year(dob, gender)
101
- decade = (dob.year / 10) % 10
102
- year = dob.year % 10
103
- month = gender == :female ? dob.month + 50 : dob.month
104
- # Rubocop's preferred formatting is pretty gory
105
- # rubocop:disable Style/FormatString
106
- "#{decade}#{'%02d' % month}#{'%02d' % dob.day}#{year}"
107
- # rubocop:enable Style/FormatString
101
+ generate(:string) do |g|
102
+ g.computed do
103
+ (dob.year / 10) % 10
104
+ end
105
+ g.computed do
106
+ gender_marker = gender == :female ? 50 : 0
107
+ format('%02d', (dob.month + gender_marker))
108
+ end
109
+ g.computed do
110
+ format('%02d', dob.day)
111
+ end
112
+ g.computed do
113
+ dob.year % 10
114
+ end
115
+ end
108
116
  end
109
117
 
110
118
  def gb_licence_checksum
111
- regexify(/[0-9][A-Z][A-Z]/)
119
+ generate(:string) do |g|
120
+ g.int
121
+ g.letter(ranges: ['A'..'Z'], length: 2)
122
+ end
112
123
  end
113
124
  end
114
125
  end
@@ -0,0 +1,230 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class HTML < Base
5
+ class << self
6
+ ##
7
+ # Produces a random HTML header format.
8
+ #
9
+ # @return [String]
10
+ #
11
+ # @example
12
+ # Faker::HTML.heading #=> "<h5>Autem</h5>"
13
+ #
14
+ # @faker.version 3.2.1
15
+ def heading
16
+ level = rand(1..6)
17
+ "<h#{level}>#{Lorem.word.capitalize}</h#{level}>"
18
+ end
19
+
20
+ ##
21
+ # Produces a random HTML paragraph format.
22
+ #
23
+ # @param sentence_count [Integer] The number of sentences in the paragraph.
24
+ # @param supplemental [Boolean] Include supplemental text.
25
+ # @param random_sentences_to_add [Integer] The number of random sentences to add to the paragraph.
26
+ # @param exclude_words [Array<String>] Words to exclude from the generated paragraph.
27
+ # @return [String]
28
+ #
29
+ # @example
30
+ # Faker::HTML.paragraph #=> "<p>Incidunt atque quis</p>"
31
+ #
32
+ # @faker.version 3.2.1
33
+ def paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil)
34
+ "<p>#{Faker::Lorem.paragraph(sentence_count: sentence_count, supplemental: supplemental, random_sentences_to_add: random_sentences_to_add, exclude_words: exclude_words)}</p>"
35
+ end
36
+
37
+ ##
38
+ # Produces a random emphasis formatting on a random word in two HTML paragraphs.
39
+ #
40
+ # @return [String]
41
+ #
42
+ # @example
43
+ # Faker::HTML.emphasis #=> "<em>repellat id impedit</em>"
44
+ #
45
+ # @faker.version 3.2.1
46
+ def emphasis
47
+ "<em>#{Faker::Lorem.paragraph(sentence_count: 1)}</em>"
48
+ end
49
+
50
+ ##
51
+ # Produces a random ordered list in HTML format, with at least one element.
52
+ #
53
+ # @return [String]
54
+ #
55
+ # @example
56
+ # Faker::HTML.ordered_list #=> "<ol>\n<li>Qui reiciendis non consequatur atque.</li>\n<li>Quo doloremque veritatis tempora aut.</li>\n<li>Aspernatur.</li>\n<li>Ea ab.</li>\n<li>Qui.</li>\n<li>Sit pariatur nemo eveniet.</li>\n<li>Molestiae aut.</li>\n<li>Nihil molestias iure placeat.</li>\n<li>Dolore autem quisquam.</li>\n</ol>"
57
+ #
58
+ # @faker.version 3.2.1
59
+ def ordered_list
60
+ number = rand(1..10)
61
+
62
+ items = []
63
+ number.times do
64
+ items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>"
65
+ end
66
+
67
+ "<ol>\n#{items.join("\n")}\n</ol>"
68
+ end
69
+
70
+ ##
71
+ # Produces a random unordered list of items between 1 and 10 randomly in HTML format.
72
+ #
73
+ # @return [String]
74
+ #
75
+ # @example
76
+ # Faker::HTML.unordered_list #=> "<ul>\n<li>Voluptatum aliquid tempora molestiae facilis non sed.</li>\n<li>Nostrum omnis iste impedit voluptatum dolor.</li>\n<li>Esse quidem et facere.</li>\n</ul>"
77
+ #
78
+ # @faker.version 3.2.1
79
+ def unordered_list
80
+ number = rand(1..10)
81
+
82
+ items = []
83
+ number.times do
84
+ items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>"
85
+ end
86
+
87
+ "<ul>\n#{items.join("\n")}\n</ul>"
88
+ end
89
+
90
+ ##
91
+ # Produces a random code block formatted in HTML.
92
+ #
93
+ # @return [String]
94
+ #
95
+ # @example
96
+ # Faker::HTML.code #=> "<code>Eos quasi qui.</code>"
97
+ #
98
+ # @faker.version 3.2.1
99
+ def code
100
+ "<code>#{Lorem.sentence(word_count: 1)}</code>"
101
+ end
102
+
103
+ ##
104
+ # Produces a random HTML table.
105
+ #
106
+ # @return [String]
107
+ #
108
+ # @example
109
+ # Faker::HTML.table #=> "<table>\n<thead>\n<th>ad</th>\n<th>similique</th>\n<th>voluptatem</th>\n</thead>\n<tbody>\n<td>corrupti</td>\n<td>est</td>\n<td>rerum</td>\n<td>molestiae</td>\n<td>quidem</td>\n<td>et</td>\n<td>in</td>\n<td>tempora</td>\n<td>at</td>\n<\tbody>\n<tfoot>\n<td>voluptatem</td>\n<td>debitis</td>\n<td>rem</td>\n</tfoot>\n</table>"
110
+ #
111
+ # @faker.version 3.2.1
112
+ def table
113
+ header_row = generate_table_row('th', 3)
114
+ footer_row = generate_table_row('td', 3)
115
+
116
+ body_rows = []
117
+ 3.times do
118
+ row = generate_table_row('td', 3)
119
+ body_rows << row
120
+ end
121
+
122
+ thead = "<thead>\n#{header_row}</thead>"
123
+ tbody = "<tbody>\n#{body_rows.join("\n")}</tbody>"
124
+ tfoot = "<tfoot>\n#{footer_row}</tfoot>"
125
+
126
+ "<table>\n#{thead}\n#{tbody}\n#{tfoot}\n</table>"
127
+ end
128
+
129
+ ##
130
+ # Generates a random <script> tag with the `src` attribute set to a random URL.
131
+ #
132
+ # @return [String]
133
+ #
134
+ # @example
135
+ # Faker::HTML.script #=> "<script src=\"http://gulgowski.name/jordan.weimann.js\"></script>"
136
+ #
137
+ # @faker.version 3.2.1
138
+ def script
139
+ "<script src=\"#{Faker::Internet.url}.js\"></script>"
140
+ end
141
+
142
+ ##
143
+ # Generates a random <link> tag with the `rel` attribute set to "stylesheet" and the `href` attribute set to a random URL.
144
+ #
145
+ # @param rel [String] The rel of the link tag.
146
+ # @return [String]
147
+ #
148
+ # @example
149
+ # Faker::HTML.link #=> "<link rel=\"stylesheet\" href=\"http://fay.io/darryl.barrows.css\">"
150
+ #
151
+ # @faker.version 3.2.1
152
+ def link(rel: 'stylesheet')
153
+ "<link rel=\"#{rel}\" href=\"#{Faker::Internet.url}.css\">"
154
+ end
155
+
156
+ ##
157
+ # Generates HTML content with customizable attributes for any HTML tag.
158
+ #
159
+ # @param tag [String] The HTML tag to generate.
160
+ # @param content [String] The Content of the HTML tag.
161
+ # @param attributes [Hash] The attributes to include in the tag.
162
+ # @return [String]
163
+ #
164
+ # @example
165
+ # Faker::HTML.element(tag: 'div', content: "This is a div with XSS attributes.", attributes: {class: 'xss', onclick: "alert('XSS')"}) #=> "<div class=\"xss\" onclick=\"alert('XSS')\">This is a div with XSS attributes.</div>"
166
+ #
167
+ # @faker.version 3.2.1
168
+ def element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" })
169
+ attribute_string = attributes.map { |key, value| "#{key}=\"#{value}\"" }.join(' ')
170
+ "<#{tag} #{attribute_string}>#{content}</#{tag}>"
171
+ end
172
+
173
+ ##
174
+ # Produces a random method from the methods above, excluding the methods listed in the arguments.
175
+ #
176
+ # @overload random(methods)
177
+ # @param methods [Symbol] Specify which methods to exclude.
178
+ #
179
+ # @return [String]
180
+ #
181
+ # @example
182
+ # Faker::HTML.random #=> returns output from a single method outlined above
183
+ # Faker::HTML.random(exclude: [:table]) #=> returns output from any single method outlined above except for "table"
184
+ # Faker::HTML.random(exclude: [:ordered_list, :unordered_list]) #=> returns output from any single method outlined above except for either ordered_list and unordered_list
185
+ #
186
+ # @faker.version 3.2.1
187
+ def random(exclude: [])
188
+ method_list = available_methods
189
+ exclude.each { |ex| method_list.delete_if { |meth| meth == ex.to_sym } }
190
+ send(method_list[Faker::Config.random.rand(0..method_list.length - 1)])
191
+ end
192
+
193
+ ##
194
+ # Generates a random HTML content sandwich, starting with a header, followed by paragraphs, and random elements.
195
+ #
196
+ # @param sentences [Integer] The number of sentences in each paragraph.
197
+ # @param repeat [Integer] The number of times to repeat the pattern (header, paragraph, random).
198
+ # @return [String]
199
+ #
200
+ # @example
201
+ # Faker::HTML.sandwich(sentences: 3, repeat: 2) #=> returns a sandwich of HTML content with 2 repetitions, each having a header, paragraph, and random element
202
+ #
203
+ # @faker.version 3.2.1
204
+ def sandwich(sentences: 3, repeat: 1)
205
+ text_block = []
206
+ text_block << heading
207
+ repeat.times do
208
+ text_block << paragraph(sentence_count: sentences)
209
+ text_block << random(exclude: %i[script link])
210
+ end
211
+ text_block.join("\n")
212
+ end
213
+
214
+ private
215
+
216
+ def available_methods
217
+ (HTML.public_methods(false) - Base.methods).sort
218
+ end
219
+
220
+ def generate_table_row(tag, cell_count)
221
+ row = "<tr>\n"
222
+ cell_count.times do
223
+ row += "<#{tag == 'th' ? 'th' : 'td'}>#{Lorem.word}</#{tag == 'th' ? 'th' : 'td'}>\n"
224
+ end
225
+ row += "</tr>\n"
226
+ row
227
+ end
228
+ end
229
+ end
230
+ end