Module: IGMarkets::Format

Defined in:
lib/ig_markets/format.rb

Overview

This module contains shared methods for formatting different content types for display.

Class Method Summary collapse

Class Method Details

.colored_currency(amount, currency_name) ⇒ String

Returns a formatted string for the specified currency amount and currency, and colors it red for negative values and green for positive values. Two decimal places are used for all currencies except the Japanese Yen.

Parameters:

  • amount (Float, Integer)

    The currency amount to format.

  • currency_name (String)

    The currency.

Returns:

  • (String)

    The formatted and colored currency amount, e.g. ‘“USD -130.40”`, `“AUD 539.10”`, `“JPY 3560”`.



43
44
45
46
47
48
49
# File 'lib/ig_markets/format.rb', line 43

def colored_currency(amount, currency_name)
  return '' unless amount

  color = amount.negative? ? :red : :green

  ColorizedString[currency(amount, currency_name)].colorize color
end

.currency(amount, currency_name) ⇒ String

Returns a formatted string for the specified currency amount and currency. Two decimal places are used for all currencies except the Japanese Yen.

Parameters:

  • amount (Float, Integer)

    The currency amount to format.

  • currency_name (String)

    The name or symbol of the currency.

Returns:

  • (String)

    The formatted currency amount, e.g. ‘“USD -130.40”`, `“AUD 539.10”`, `“JPY 3560”`.



26
27
28
29
30
31
32
33
34
# File 'lib/ig_markets/format.rb', line 26

def currency(amount, currency_name)
  return '' unless amount

  if ['JPY', '¥'].include? currency_name
    "#{currency_name} #{format '%i', amount.to_i}"
  else
    "#{currency_name} #{format '%.2f', amount.to_f}"
  end
end

.level(value) ⇒ String

Returns a formatted string for the specified level. At most four decimal places are used to format levels.

Parameters:

  • value (Float)

    The level to format.

Returns:

  • (String)

    The formatted level, e.g. ‘“-130.4055”`



13
14
15
16
17
# File 'lib/ig_markets/format.rb', line 13

def level(value)
  return '' unless value

  Float(format('%.4f', value.to_f)).to_s
end

.seconds(value) ⇒ String

Returns a formatted string for the specified number of seconds in the format ‘[<hours>:]<minutes>:<seconds>`.

Parameters:

  • value (Integer)

    The number of seconds to format.

Returns:

  • (String)


56
57
58
59
60
61
62
63
64
# File 'lib/ig_markets/format.rb', line 56

def seconds(value)
  result = if value >= 60 * 60
             "#{value / 60 / 60}:#{Kernel.format('%02i', (value / 60) % 60)}"
           else
             (value / 60).to_s
           end

  "#{result}:#{Kernel.format('%02i', value % 60)}"
end

.symbol(value) ⇒ String

Formats the passed symbol into a human-readable string, replacing underscores with spaces and capitalizing the first letter.

Parameters:

  • value (Symbol)

    The symbol to format.

Returns:

  • (String)


72
73
74
# File 'lib/ig_markets/format.rb', line 72

def symbol(value)
  value.to_s.capitalize.tr '_', ' '
end