Class: NumberFormatter

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/number_formatter.rb

Overview

class used for formatting numbers class used to download badges from shields.io

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

available_extension?, clean_image_label, dispatch_http_response, display_total, display_type, env_production?, fetch_content_type, find_version, force_utf8_encoding, format_error, get_latest_stable_version_details, get_string_from_cookie_data, http_valid_content_types?, http_valid_status_code?, last_version, metric_power, metric_prefixes, non_empty_http_response?, options_base_url, parse_gem_version, parse_json, parsed_url_property, print_to_output_buffer, root, rubygems_valid_response?, setup_options_for_url, shields_io_valid_response?, sorted_versions, stable_gem_versions, valid_http_code_returned?, valid_http_response?

Constructor Details

#initialize(number, params) ⇒ void

Method used for instantiating the NumberFormatter with the number that will be used and a boolean value that will decide if we format in metric format or using delimiters If the number is blank the number will be set to 0

Parameters:

  • number (Number)

    The number that will be formatted in different format

  • params (Hash)

    A boolean value that will decide if we format using metrics or delimiters


22
23
24
25
# File 'lib/number_formatter.rb', line 22

def initialize(number, params)
  @number = number.present? ? number : 0
  @params = params
end

Instance Attribute Details

#numberNumber

Returns The number that will be formatted in different format.

Returns:

  • (Number)

    The number that will be formatted in different format


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/number_formatter.rb', line 11

class NumberFormatter
  include Helper
  attr_reader :number, :params

  # Method used for instantiating the NumberFormatter with the number that will be used
  # and a boolean value that will decide if we format in metric format or using delimiters
  # If the number is blank the number will be set to 0
  #
  # @param [Number] number  The number that will be formatted in different format
  # @param [Hash] params A boolean value that will decide if we format using metrics or delimiters
  # @return [void]
  def initialize(number, params)
    @number = number.present? ? number : 0
    @params = params
  end

  # Method that is used to decide if the number is from a rubygems API call
  #
  # @return [Boolean] Returns true or false depending if params have key 'api' with value 'rubygems'
  def for_rubygems_api?
    @params['api'] == 'rubygems'
  end

  # Method that is used to decide if number can be displayed using metrics
  # @see #for_rubygems_api
  #
  # @return [Boolean] Returns true or false depending if action is available
  def can_display_metric?
    for_rubygems_api? && @params.fetch('metric', false).present?
  end

  # Method that is used to decide if total can be shown on badge
  # @see #for_rubygems_api
  # @see #display_total
  #
  # @return [Boolean] Returns true or false depending if action is available
  def can_display_total?
    for_rubygems_api? && display_total
  end

  # Method that is used to append '_total' to number if action is available
  # @see #can_display_total
  #
  # @return [String] Returns string '_total' or from params (if has key total_label), or empty string otherwise
  def number_text
    can_display_total? ? "_#{@params.fetch('total_label', 'total').tr('-', '_')}" : ''
  end

  # Method that is used to decide which format to use depending on the instance
  # variable display_metric. if the variable is true will display using metrics otherwise
  # using delimiters
  # @see #number_with_metric
  # @see #number_with_delimiter
  #
  # @return [String] Returns the number formatted with metrics if 'display_metric' instance variable is true, otherwise using delimiters
  def to_s
    nr = can_display_metric? ? number_with_metric : number_with_delimiter
    "#{nr}#{number_text}"
  end

  # Method used to print a number using metrics
  #
  # @return [String] Returns the formatted number in metrics format
  def number_with_metric
    index = metric_prefixes.size - 1
    while index >= 0
      limit = metric_power[index]
      if @number > limit
        nr = "#{(@number / limit).to_f.round}#{metric_prefixes[index]}"
        return nr
      end
      index -= 1
    end
    @number if nr.blank?
  end

  # Description of method
  #
  # @param [String] delimiter = ',' The delimiter that is used for number greater than 1000
  # @param [String] separator = '.' The separator is used for float numbers to separate zecimals
  # @return [String] Returns the formatted number using delimiters and separators
  def number_with_delimiter(delimiter = ',', separator = '.')
    parts = @number.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
    parts.join separator
  rescue
    @number
  end
end

#paramsHash

Returns The params that are used for formatting the number display.

Returns:

  • (Hash)

    The params that are used for formatting the number display


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/number_formatter.rb', line 11

class NumberFormatter
  include Helper
  attr_reader :number, :params

  # Method used for instantiating the NumberFormatter with the number that will be used
  # and a boolean value that will decide if we format in metric format or using delimiters
  # If the number is blank the number will be set to 0
  #
  # @param [Number] number  The number that will be formatted in different format
  # @param [Hash] params A boolean value that will decide if we format using metrics or delimiters
  # @return [void]
  def initialize(number, params)
    @number = number.present? ? number : 0
    @params = params
  end

  # Method that is used to decide if the number is from a rubygems API call
  #
  # @return [Boolean] Returns true or false depending if params have key 'api' with value 'rubygems'
  def for_rubygems_api?
    @params['api'] == 'rubygems'
  end

  # Method that is used to decide if number can be displayed using metrics
  # @see #for_rubygems_api
  #
  # @return [Boolean] Returns true or false depending if action is available
  def can_display_metric?
    for_rubygems_api? && @params.fetch('metric', false).present?
  end

  # Method that is used to decide if total can be shown on badge
  # @see #for_rubygems_api
  # @see #display_total
  #
  # @return [Boolean] Returns true or false depending if action is available
  def can_display_total?
    for_rubygems_api? && display_total
  end

  # Method that is used to append '_total' to number if action is available
  # @see #can_display_total
  #
  # @return [String] Returns string '_total' or from params (if has key total_label), or empty string otherwise
  def number_text
    can_display_total? ? "_#{@params.fetch('total_label', 'total').tr('-', '_')}" : ''
  end

  # Method that is used to decide which format to use depending on the instance
  # variable display_metric. if the variable is true will display using metrics otherwise
  # using delimiters
  # @see #number_with_metric
  # @see #number_with_delimiter
  #
  # @return [String] Returns the number formatted with metrics if 'display_metric' instance variable is true, otherwise using delimiters
  def to_s
    nr = can_display_metric? ? number_with_metric : number_with_delimiter
    "#{nr}#{number_text}"
  end

  # Method used to print a number using metrics
  #
  # @return [String] Returns the formatted number in metrics format
  def number_with_metric
    index = metric_prefixes.size - 1
    while index >= 0
      limit = metric_power[index]
      if @number > limit
        nr = "#{(@number / limit).to_f.round}#{metric_prefixes[index]}"
        return nr
      end
      index -= 1
    end
    @number if nr.blank?
  end

  # Description of method
  #
  # @param [String] delimiter = ',' The delimiter that is used for number greater than 1000
  # @param [String] separator = '.' The separator is used for float numbers to separate zecimals
  # @return [String] Returns the formatted number using delimiters and separators
  def number_with_delimiter(delimiter = ',', separator = '.')
    parts = @number.to_s.split('.')
    parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
    parts.join separator
  rescue
    @number
  end
end

Instance Method Details

#can_display_metric?Boolean

Method that is used to decide if number can be displayed using metrics

Returns:

  • (Boolean)

    Returns true or false depending if action is available

See Also:

  • #for_rubygems_api

38
39
40
# File 'lib/number_formatter.rb', line 38

def can_display_metric?
  for_rubygems_api? && @params.fetch('metric', false).present?
end

#can_display_total?Boolean

Method that is used to decide if total can be shown on badge

Returns:

  • (Boolean)

    Returns true or false depending if action is available

See Also:

  • #for_rubygems_api
  • Helper#display_total

47
48
49
# File 'lib/number_formatter.rb', line 47

def can_display_total?
  for_rubygems_api? && display_total
end

#for_rubygems_api?Boolean

Method that is used to decide if the number is from a rubygems API call

Returns:

  • (Boolean)

    Returns true or false depending if params have key 'api' with value 'rubygems'


30
31
32
# File 'lib/number_formatter.rb', line 30

def for_rubygems_api?
  @params['api'] == 'rubygems'
end

#number_textString

Method that is used to append '_total' to number if action is available

Returns:

  • (String)

    Returns string '_total' or from params (if has key total_label), or empty string otherwise

See Also:

  • #can_display_total

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

def number_text
  can_display_total? ? "_#{@params.fetch('total_label', 'total').tr('-', '_')}" : ''
end

#number_with_delimiter(delimiter = ',', separator = '.') ⇒ String

Description of method

Parameters:

  • delimiter (String) (defaults to: ',')

    ',' The delimiter that is used for number greater than 1000

  • separator (String) (defaults to: '.')

    '.' The separator is used for float numbers to separate zecimals

Returns:

  • (String)

    Returns the formatted number using delimiters and separators


92
93
94
95
96
97
98
# File 'lib/number_formatter.rb', line 92

def number_with_delimiter(delimiter = ',', separator = '.')
  parts = @number.to_s.split('.')
  parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
  parts.join separator
rescue
  @number
end

#number_with_metricString

Method used to print a number using metrics

Returns:

  • (String)

    Returns the formatted number in metrics format


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/number_formatter.rb', line 74

def number_with_metric
  index = metric_prefixes.size - 1
  while index >= 0
    limit = metric_power[index]
    if @number > limit
      nr = "#{(@number / limit).to_f.round}#{metric_prefixes[index]}"
      return nr
    end
    index -= 1
  end
  @number if nr.blank?
end

#to_sString

Method that is used to decide which format to use depending on the instance variable display_metric. if the variable is true will display using metrics otherwise using delimiters

Returns:

  • (String)

    Returns the number formatted with metrics if 'display_metric' instance variable is true, otherwise using delimiters

See Also:


66
67
68
69
# File 'lib/number_formatter.rb', line 66

def to_s
  nr = can_display_metric? ? number_with_metric : number_with_delimiter
  "#{nr}#{number_text}"
end