Class: Faker::HTML
Constant Summary
Constants inherited from Base
Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters
Class Method Summary collapse
-
.code ⇒ String
Produces a random code block formatted in HTML.
-
.element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" }) ⇒ String
Generates HTML content with customizable attributes for any HTML tag.
-
.emphasis ⇒ String
Produces a random emphasis formatting on a random word in two HTML paragraphs.
-
.heading ⇒ String
Produces a random HTML header format.
-
.link(rel: 'stylesheet') ⇒ String
Generates a random <link> tag with the ‘rel` attribute set to “stylesheet” and the `href` attribute set to a random URL.
-
.ordered_list ⇒ String
Produces a random ordered list in HTML format, with at least one element.
-
.paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil) ⇒ String
Produces a random HTML paragraph format.
-
.random(methods) ⇒ String
Produces a random method from the methods above, excluding the methods listed in the arguments.
-
.sandwich(sentences: 3, repeat: 1) ⇒ String
Generates a random HTML content sandwich, starting with a header, followed by paragraphs, and random elements.
-
.script ⇒ String
Generates a random <script> tag with the ‘src` attribute set to a random URL.
-
.table ⇒ String
Produces a random HTML table.
-
.unordered_list ⇒ String
Produces a random unordered list of items between 1 and 10 randomly in HTML format.
Methods inherited from Base
bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale
Class Method Details
.code ⇒ String
Produces a random code block formatted in HTML.
99 100 101 |
# File 'lib/faker/default/html.rb', line 99 def code "<code>#{Lorem.sentence(word_count: 1)}</code>" end |
.element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" }) ⇒ String
Generates HTML content with customizable attributes for any HTML tag.
168 169 170 171 |
# File 'lib/faker/default/html.rb', line 168 def element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" }) attribute_string = attributes.map { |key, value| "#{key}=\"#{value}\"" }.join(' ') "<#{tag} #{attribute_string}>#{content}</#{tag}>" end |
.emphasis ⇒ String
Produces a random emphasis formatting on a random word in two HTML paragraphs.
46 47 48 |
# File 'lib/faker/default/html.rb', line 46 def emphasis "<em>#{Faker::Lorem.paragraph(sentence_count: 1)}</em>" end |
.heading ⇒ String
Produces a random HTML header format.
15 16 17 18 |
# File 'lib/faker/default/html.rb', line 15 def heading level = rand(1..6) "<h#{level}>#{Lorem.word.capitalize}</h#{level}>" end |
.link(rel: 'stylesheet') ⇒ String
Generates a random <link> tag with the ‘rel` attribute set to “stylesheet” and the `href` attribute set to a random URL.
152 153 154 |
# File 'lib/faker/default/html.rb', line 152 def link(rel: 'stylesheet') "<link rel=\"#{rel}\" href=\"#{Faker::Internet.url}.css\">" end |
.ordered_list ⇒ String
Produces a random ordered list in HTML format, with at least one element.
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/faker/default/html.rb', line 59 def ordered_list number = rand(1..10) items = [] number.times do items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>" end "<ol>\n#{items.join("\n")}\n</ol>" end |
.paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil) ⇒ String
Produces a random HTML paragraph format.
33 34 35 |
# File 'lib/faker/default/html.rb', line 33 def paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil) "<p>#{Faker::Lorem.paragraph(sentence_count: sentence_count, supplemental: supplemental, random_sentences_to_add: random_sentences_to_add, exclude_words: exclude_words)}</p>" end |
.random(methods) ⇒ String
Produces a random method from the methods above, excluding the methods listed in the arguments.
187 188 189 190 191 |
# File 'lib/faker/default/html.rb', line 187 def random(exclude: []) method_list = available_methods exclude.each { |ex| method_list.delete_if { |meth| meth == ex.to_sym } } send(method_list[Faker::Config.random.rand(0..method_list.length - 1)]) end |
.sandwich(sentences: 3, repeat: 1) ⇒ String
Generates a random HTML content sandwich, starting with a header, followed by paragraphs, and random elements.
204 205 206 207 208 209 210 211 212 |
# File 'lib/faker/default/html.rb', line 204 def sandwich(sentences: 3, repeat: 1) text_block = [] text_block << heading repeat.times do text_block << paragraph(sentence_count: sentences) text_block << random(exclude: %i[script link]) end text_block.join("\n") end |
.script ⇒ String
Generates a random <script> tag with the ‘src` attribute set to a random URL.
138 139 140 |
# File 'lib/faker/default/html.rb', line 138 def script "<script src=\"#{Faker::Internet.url}.js\"></script>" end |
.table ⇒ String
Produces a random HTML table.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/faker/default/html.rb', line 112 def table header_row = generate_table_row('th', 3) = generate_table_row('td', 3) body_rows = [] 3.times do row = generate_table_row('td', 3) body_rows << row end thead = "<thead>\n#{header_row}</thead>" tbody = "<tbody>\n#{body_rows.join("\n")}</tbody>" tfoot = "<tfoot>\n#{}</tfoot>" "<table>\n#{thead}\n#{tbody}\n#{tfoot}\n</table>" end |
.unordered_list ⇒ String
Produces a random unordered list of items between 1 and 10 randomly in HTML format.
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/faker/default/html.rb', line 79 def unordered_list number = rand(1..10) items = [] number.times do items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>" end "<ul>\n#{items.join("\n")}\n</ul>" end |