Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/curly/string.rb,
lib/curly/curl/html.rb
Overview
String helpers
Instance Method Summary collapse
-
#clean ⇒ String
Remove extra spaces and newlines, compress space between tags.
-
#clean! ⇒ Object
Destructive version of #clean.
-
#clean_output ⇒ Array
Clean up output and return a single-item array.
-
#ensure_array ⇒ Array
Ensure that an object is an array.
-
#normalize_browser_type(default = :none) ⇒ Symbol
Convert a browser type string to a symbol.
-
#normalize_image_type(default = :all) ⇒ Symbol
Convert an image type string to a symbol.
-
#normalize_screenshot_type(default = :none) ⇒ Symbol
Convert a screenshot type string to a symbol.
- #remove_entities ⇒ Object
-
#strip_tags ⇒ String
Remove HTML tags from a string.
-
#strip_tags! ⇒ Object
Destructive version of #strip_tags.
-
#utf8 ⇒ String
Discard invalid characters and output a UTF-8 String.
-
#utf8! ⇒ String
Destructive version of #utf8.
Instance Method Details
#clean ⇒ String
Remove extra spaces and newlines, compress space between tags
32 33 34 |
# File 'lib/curly/string.rb', line 32 def clean gsub(/[\t\n ]+/m, ' ').gsub(/> +</, '><') end |
#clean! ⇒ Object
Destructive version of #clean
50 51 52 |
# File 'lib/curly/string.rb', line 50 def clean! replace clean end |
#clean_output ⇒ Array
Clean up output and return a single-item array
120 121 122 123 |
# File 'lib/curly/string.rb', line 120 def clean_output output = ensure_array output.clean_output end |
#ensure_array ⇒ Array
Ensure that an object is an array
130 131 132 |
# File 'lib/curly/string.rb', line 130 def ensure_array return [self] end |
#normalize_browser_type(default = :none) ⇒ Symbol
Convert a browser type string to a symbol
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/curly/string.rb', line 86 def normalize_browser_type(default = :none) case self.to_s when /^c/i :chrome when /^f/i :firefox else default.is_a?(Symbol) ? default.to_sym : default.normalize_browser_type end end |
#normalize_image_type(default = :all) ⇒ Symbol
Convert an image type string to a symbol
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/curly/string.rb', line 68 def normalize_image_type(default = :all) case self.to_s when /^[sp]/i :srcset when /^i/i :img when /^o/i :opengraph else default.is_a?(Symbol) ? default.to_sym : default.normalize_image_type end end |
#normalize_screenshot_type(default = :none) ⇒ Symbol
Convert a screenshot type string to a symbol
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/curly/string.rb', line 102 def normalize_screenshot_type(default = :none) case self.to_s when /^f/i :full_page when /^p/i :print_page when /^v/i :visible else default.is_a?(Symbol) ? default.to_sym : default.normalize_browser_type end end |
#remove_entities ⇒ Object
6 7 8 |
# File 'lib/curly/curl/html.rb', line 6 def remove_entities gsub(/ /, ' ') end |
#strip_tags ⇒ String
Remove HTML tags from a string
41 42 43 |
# File 'lib/curly/string.rb', line 41 def gsub(%r{</?.*?>}, '') end |
#strip_tags! ⇒ Object
Destructive version of #strip_tags
59 60 61 |
# File 'lib/curly/string.rb', line 59 def replace end |
#utf8 ⇒ String
Discard invalid characters and output a UTF-8 String
14 15 16 |
# File 'lib/curly/string.rb', line 14 def utf8 encode('utf-16', invalid: :replace).encode('utf-8') end |
#utf8! ⇒ String
Destructive version of #utf8
23 24 25 |
# File 'lib/curly/string.rb', line 23 def utf8! replace utf8 end |