Module: Rodbot::Refinements

Extended by:
Memoize
Defined in:
lib/rodbot/refinements.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Memoize

included

Class Method Details

.inflectorZeitwerk::Inflector

Reusable inflector instance

Returns:

  • (Zeitwerk::Inflector)


112
113
114
# File 'lib/rodbot/refinements.rb', line 112

memoize def inflector
  Zeitwerk::Inflector.new
end

Instance Method Details

#camelizeString

Convert from under_scores to CamelCase

Examples:

camelize('foo_bar')   # => 'FooBar'

Returns:

  • (String)

    CamelCased string



16
17
18
19
20
# File 'lib/rodbot/refinements.rb', line 16

refine String do
  def camelize
    Rodbot::Refinements.inflector.camelize(self, nil)
  end
end

#constantizeClass, Module

Convert module or class path to module or class

Examples:

'foo/bar_baz'.constantize   # => Foo::BarBaz

Returns:

  • (Class, Module)

    class or module



29
30
31
32
33
# File 'lib/rodbot/refinements.rb', line 29

refine String do
  def constantize
    Module.const_get(self.split('/').map(&:camelize).join('::'))
  end
end

#html_to_textString

Converts HTML to plain text by removing all tags

Examples:

'<strong>important</strong>'.html_to_text   # => 'important'

Returns:

  • (String)

    text



82
83
84
85
86
# File 'lib/rodbot/refinements.rb', line 82

refine String do
  def html_to_text
    self.gsub(/<.*?>/, '')
  end
end

#md_to_htmlString

Converts Markdown in the string to HTML

Examples:

'**important**'.md_to_html   # => '<strong>important</strong>'

Returns:

  • (String)

    HTML



69
70
71
72
73
# File 'lib/rodbot/refinements.rb', line 69

refine String do
  def md_to_html
    Kramdown::Document.new(self, input: 'GFM').to_html.strip
  end
end

#psubString

Replace placeholders

Placeholders are all UPCASE and wrapped in [[ and ]]. They must match keys in the placeholder hash, however, these keys are Symbols and all downcase.

Examples:

placeholders = { sender: 'Oggy' }
'Hi, [[SENDER]]!'.psub(placeholders)   # => 'Hi, Oggy!'

Returns:

  • (String)

    string without placeholders



100
101
102
103
104
# File 'lib/rodbot/refinements.rb', line 100

refine String do
  def psub(placeholders)
    self.gsub(/\[\[.*?\]\]/) { placeholders[_1[2..-3].downcase.to_sym] }
  end
end

#uri_concatString

Safely concat path segments to a URI string

URI#join is ultimately used to add the given segments which has a maybe counter-intuitive API at first. Check out the docs of URI#join and the examples below.

Examples:

s = 'http://example.com'
s.uri_concat('foo')               # => "http://example.com/foo"
s.uri_concat('foo/')              # => "http://example.com/foo/"
s.uri_concat('foo', 'bar')        # => "http://example.com/bar"   <- sic!
s.uri_concat('foo/, 'bar')        # => "http://example.com/foo/bar"
s.uri_concat('foo/, 'bar.html')   # => "http://example.com/foo/bar.html"
s.uri_concat('föö')               # => "http://example.com/f%C3%B6%C3%B6"

Parameters:

  • segments (Array<String>)

    path segments

Returns:

  • (String)

    concatted URI



53
54
55
56
57
58
59
60
# File 'lib/rodbot/refinements.rb', line 53

refine String do
  def uri_concat(*segments)
    parser = URI::Parser.new
    segments.inject(URI(self)) do |uri, segment|
      uri + parser.escape(segment)
    end.to_s
  end
end