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

.currency(amount, currency) ⇒ 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, Fixnum)

    The currency amount to format.

  • currency (String)

    The currency.

Returns:

  • (String)

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



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

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

  if ['JPY', '¥'].include? currency
    "#{currency} #{format '%i', amount.to_i}"
  else
    "#{currency} #{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”`



11
12
13
14
15
# File 'lib/ig_markets/format.rb', line 11

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 (Fixnum)

    The number of seconds to format.

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
# File 'lib/ig_markets/format.rb', line 39

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)


55
56
57
# File 'lib/ig_markets/format.rb', line 55

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