Class: Dovado::Router::Traffic::Amount

Inherits:
Numeric
  • Object
show all
Defined in:
lib/dovado/router/traffic/amount.rb

Overview

Data amount data type. Extends Numeric and can be used as an Integer with the addition of getting the amount as bytes, kilobytes, megabytes or gigabytes.

Since:

  • 1.0.2

Constant Summary collapse

DEFAULT_KILO_BASE =

The default base of a kilobyte.

Since:

  • 1.0.2

1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0, base = DEFAULT_KILO_BASE) ⇒ Amount

Create a new Amount object.

Note: You can set the default base for a kilobyte in the router admin interface to 1000 or 1024, you should use the same base here to get the right figures. The base can differ from one operator to another.

Parameters:

  • value (Numeric) (defaults to: 0)

    value of this Dovado::Router::Traffic::Amount, defaults to 0.

  • base (Integer) (defaults to: DEFAULT_KILO_BASE)

    the base of a kilobyte.

Since:

  • 1.0.2



25
26
27
28
29
# File 'lib/dovado/router/traffic/amount.rb', line 25

def initialize(value=0, base=DEFAULT_KILO_BASE)
  raise ArgumentError.new "Argument is not numeric: #{value}" unless value.is_a? Numeric
  @value = value
  @base = base
end

Instance Attribute Details

#sim_idObject

Since:

  • 1.0.2



14
15
16
# File 'lib/dovado/router/traffic/amount.rb', line 14

def sim_id
  @sim_id
end

Instance Method Details

#bObject

Shortcut to #bytes.

Since:

  • 1.0.2



37
38
39
# File 'lib/dovado/router/traffic/amount.rb', line 37

def b
  bytes
end

#bytesObject

Since:

  • 1.0.2



32
33
34
# File 'lib/dovado/router/traffic/amount.rb', line 32

def bytes
  @value * @base
end

#gbObject

Shortcut to #gigabytes.

Since:

  • 1.0.2



67
68
69
# File 'lib/dovado/router/traffic/amount.rb', line 67

def gb
  gigabytes
end

#gigabytesObject

Since:

  • 1.0.2



62
63
64
# File 'lib/dovado/router/traffic/amount.rb', line 62

def gigabytes
  (megabytes / @base.to_f).round(2)
end

#kbObject

Shortcut to #kilobytes.

Since:

  • 1.0.2



47
48
49
# File 'lib/dovado/router/traffic/amount.rb', line 47

def kb
  kilobytes
end

#kilobytesObject

Since:

  • 1.0.2



42
43
44
# File 'lib/dovado/router/traffic/amount.rb', line 42

def kilobytes
  @value
end

#mbObject

Shortcut to #megabytes.

Since:

  • 1.0.2



57
58
59
# File 'lib/dovado/router/traffic/amount.rb', line 57

def mb
  megabytes
end

#megabytesObject

Since:

  • 1.0.2



52
53
54
# File 'lib/dovado/router/traffic/amount.rb', line 52

def megabytes
  (kilobytes / @base.to_f).round(2)
end