Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/curly/string.rb,
lib/curly/curl/html.rb

Overview

String helpers

Instance Method Summary collapse

Instance Method Details

#cleanString

Remove extra spaces and newlines, compress space between tags

Returns:



32
33
34
# File 'lib/curly/string.rb', line 32

def clean
  gsub(/[\t\n ]+/m, ' ').gsub(/> +</, '><')
end

#clean!Object

Destructive version of #clean

See Also:



50
51
52
# File 'lib/curly/string.rb', line 50

def clean!
  replace clean
end

#clean_outputArray

Clean up output and return a single-item array

Returns:

  • (Array)

    output array



120
121
122
123
# File 'lib/curly/string.rb', line 120

def clean_output
  output = ensure_array
  output.clean_output
end

#ensure_arrayArray

Ensure that an object is an array

Returns:

  • (Array)

    object as 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

Returns:

  • (Symbol)

    :chrome, :firefox



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

Returns:

  • (Symbol)

    :srcset, :img, :opengraph, :all



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

Returns:

  • (Symbol)

    :full_page, :print_page, :visible



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_entitiesObject



6
7
8
# File 'lib/curly/curl/html.rb', line 6

def remove_entities
  gsub(/&nbsp;/, ' ')
end

#strip_tagsString

Remove HTML tags from a string

Returns:

  • (String)

    stripped string



41
42
43
# File 'lib/curly/string.rb', line 41

def strip_tags
  gsub(%r{</?.*?>}, '')
end

#strip_tags!Object

Destructive version of #strip_tags

See Also:



59
60
61
# File 'lib/curly/string.rb', line 59

def strip_tags!
  replace strip_tags
end

#utf8String

Discard invalid characters and output a UTF-8 String

Returns:

  • (String)

    UTF-8 encoded 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

Returns:

  • (String)

    UTF-8 encoded string, in place



23
24
25
# File 'lib/curly/string.rb', line 23

def utf8!
  replace utf8
end