speed_format 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/speed_format.rb +61 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzBkYzMzZGNjYjkxNmI2NjhlNDg3NzQ3MmRkNzZmNzAwM2FjMzI4MA==
5
+ data.tar.gz: !binary |-
6
+ YTllNWMwYjBkMjViMTFjMTgxMTBkMzE5NzRlNjg3MWZjYmI1NzY1Nw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDllZGZlOTFiMjczNjM1YWVmMDViZGUyZmY2ODhkZjIyZjY5ZmJlYjEyM2Yw
10
+ ODQwMmJhOTBiYmEwMmMyMGJmYTE2OWMxNDFlZWNlN2VmMzExODVlNmM0N2Ri
11
+ MTFjZmQ5YzRmZmM0NDdjOTA3ODIyNjczNGFmNWVhMzQ4OTQ1NDU=
12
+ data.tar.gz: !binary |-
13
+ ZDM0NmVmYjk3ZTg3YzI0ODMzZmYzMTdlMzQxNDIwYjQ4NTUyY2I2NTZjMTlk
14
+ ODFlZmM0NDZlZWEwZmIxMmJjYzc4MTc3ZjFmZTE5OWRlZDQ0YjI0ZWJmYWM4
15
+ N2UzMjhiZWZlZWQ2ZjYzZjYxMDE3OTFkNDBmMDIxMjUwYjYxNjY=
@@ -0,0 +1,61 @@
1
+ # Transform unreadable speed to beautiful ones with the right prefix
2
+ # Copyright (C) 2016 Julien Wolflisberg
3
+
4
+ # This program is free software: you can redistribute it and/or modify
5
+ # it under the terms of the GNU General Public License as published by
6
+ # the Free Software Foundation, either version 3 of the License, or
7
+ # (at your option) any later version.
8
+
9
+ # This program is distributed in the hope that it will be useful,
10
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ # GNU General Public License for more details.
13
+
14
+ require 'bigdecimal'
15
+
16
+ class SpeedFormat
17
+ @@units = [nil, :k, :M, :G, :T, :P, :E]
18
+
19
+ # Get *bps* in *unit* and format it, if *bytes* is provided, the output is in bytes instead of bits
20
+ def self.format(bps, unit=nil, bytes=nil)
21
+ if not @@units.include?(unit)
22
+ raise ArgumentError, "Not a valid prefix"
23
+ end
24
+ bps = BigDecimal.new(bps.to_s).abs
25
+ if bytes == :bytes
26
+ bps /= 8
27
+ end
28
+ index = @@units.index(unit)
29
+ d = bps % 1 != 0.0 ? 1 : -1 # If there's right side digits, should move the comma right, otherwise left
30
+ if bps != 0.0
31
+ if d == 1 # Move to the right (non-integers)
32
+ while (bps % 1 != 0.0) and (index-d >= 0) and (index-d < @@units.length)
33
+ bps *= 10.0**(d*3)
34
+ index -= d
35
+ end
36
+ else # Move to the left
37
+ next_v = (bps*10.0**(d*3)) # Calculating the next
38
+ while (next_v % 1 == 0.0) and (index-d >= 0) and (index-d < @@units.length)
39
+ bps *= 10.0**(d*3)
40
+ index -= d
41
+ next_v = bps*10.0**(d*3)
42
+ end
43
+ end
44
+ else
45
+ index = 0
46
+ end
47
+ bps = bps.to_s("F")
48
+ return bps[-2,2] == ".0" ? bps[0..-3].to_i : Float(bps), @@units[index]
49
+ end
50
+
51
+ def self.format_string(*args)
52
+ out = self.format(*args)
53
+ out[0] = out[0] % 1 == 0.0 ? out[0].to_i : out[0]
54
+ suffix = args.include?(:bytes) ? "B/s" : "bit/s"
55
+ out.join(" ") + suffix
56
+ end
57
+
58
+ def self.units
59
+ @@units
60
+ end
61
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: speed_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Julien Wolflisberg
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Format various bit rate to human readable and more convenient-sized digits
14
+ and prefixes
15
+ email: julien.wolflisberg@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/speed_format.rb
21
+ homepage: https://github.com/julienwolflisberg/speed_format
22
+ licenses:
23
+ - GPL-3.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.8
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Bit rate for humans
45
+ test_files: []