Module: Birdwatcher::Concerns::Util

Included in:
Birdwatcher::Command, Module
Defined in:
lib/birdwatcher/concerns/util.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/birdwatcher/concerns/util.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#escape_html(string) ⇒ String

Escape special HTML characters with HTML entities

Parameters:

  • string (String)

    String to HTML escape

Returns:

  • (String)

    HTML escaped string



86
87
88
# File 'lib/birdwatcher/concerns/util.rb', line 86

def escape_html(string)
  Birdwatcher::Util.escape_html(string)
end

#excerpt(text, max_length, omission = "...") ⇒ String

Create an excerpt of potentially long text at a fixed length

Examples:

excerpt("The quick brown fox jumps over the lazy dog", 80)
#=> "The quick brown fox jumps over the lazy dog"

excerpt("The quick brown fox jumps over the lazy dog", 40)
#=> "The quick brown fox jumps over the lazy d..."

Parameters:

  • text (String)

    Text to excerpt

  • max_length (Integer)

    Maximum length of text before being excerpted

  • omission (String) (defaults to: "...")

    OPTIONAL: String to append to text if excerpted

Returns:

  • (String)

    excerpted text



113
114
115
# File 'lib/birdwatcher/concerns/util.rb', line 113

def excerpt(text, max_length, omission = "...")
  Birdwatcher::Util.excerpt(text, max_length, omission)
end

#parse_time(time) ⇒ Time

Natural language parsing of time

Uses the Chronic gem to perform natural language parsing of time. See the examples in Chronic’s documentation for strings that can be parsed.

All modules that can be configured with times, should perform natural language parsing on the option setting for better user experience.

Parameters:

  • time (String)

    A string representing a time (e.g. “yesterday at 4:00”)

Returns:

  • (Time)

See Also:



38
39
40
# File 'lib/birdwatcher/concerns/util.rb', line 38

def parse_time(time)
  Birdwatcher::Util.parse_time(time)
end

#pluralize(count, singular, plural) ⇒ Object

Correct pluralization of word depending on count

pluralizes the singular word unless count is 1.

Examples:

pluralize(1, "user", "users")
#=> "1 user"

pluralize(5, "user", "users")
#=> "5 users"

pluralize(0, "user", "users")
#=> "0 users"

Parameters:

  • count (Integer)

    The amount

  • singular (String)

    The singular word

  • plural (String)

    The plural word



59
60
61
# File 'lib/birdwatcher/concerns/util.rb', line 59

def pluralize(count, singular, plural)
  Birdwatcher::Util.pluralize(count, singular, plural)
end

#strip_control_characters(string) ⇒ String

Strip out control characters and color codes from a string

Parameters:

  • string (String)

    String to strip control characters from

Returns:

  • (String)

    String without control characters



77
78
79
# File 'lib/birdwatcher/concerns/util.rb', line 77

def strip_control_characters(string)
  Birdwatcher::Util.strip_control_characters(string)
end

#strip_html(string) ⇒ String

Strip out HTML tags from a string

Parameters:

  • string (String)

    String to strip HTML tags from

Returns:

  • (String)

    HTML stripped string



68
69
70
# File 'lib/birdwatcher/concerns/util.rb', line 68

def strip_html(string)
  Birdwatcher::Util.strip_html(string)
end

#suppress_output(&block) ⇒ Object

Suppress any potential output to STDOUT

Used in cases where certain libraries or methods might output unwanted text to STDOUT without any possibility of disabling it.

Parameters:

  • block

    code block to run with output suppression



123
124
125
# File 'lib/birdwatcher/concerns/util.rb', line 123

def suppress_output(&block)
  Birdwatcher::Util.suppress_output(&block)
end

#suppress_warnings(&block) ⇒ Object

Suppress any warning messages to STDOUT

Used in cases where certain libraries or methods might output unwanted warning messages to STDOUT.

Parameters:

  • block

    code block to run with warning suppression



133
134
135
# File 'lib/birdwatcher/concerns/util.rb', line 133

def suppress_warnings(&block)
  Birdwatcher::Util.suppress_warnings(&block)
end

#time_ago_in_words(time) ⇒ String

Get the relative time for a timestamp

Examples:

getting relative time of a status

status = current_workspace.statuses.last
output time_ago_in_words(status.posted_at)
#=> "1 day and 15 hours ago"

Parameters:

  • time (Time)

    Timestamp to be converted

Returns:

  • (String)

    relative time in words



21
22
23
# File 'lib/birdwatcher/concerns/util.rb', line 21

def time_ago_in_words(time)
  Birdwatcher::Util.time_ago_in_words(time)
end

#unescape_html(string) ⇒ String

Unescape special HTML characters in a string

Parameters:

  • string (String)

    String to unescape

Returns:

  • (String)

    string with escaped special HTML characters unescaped



95
96
97
# File 'lib/birdwatcher/concerns/util.rb', line 95

def unescape_html(string)
  Birdwatcher::Util.unescape_html(string)
end