-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
223 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Copyright (c) [2021] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
|
||
module Y2Network | ||
# Represents a bitrate | ||
# | ||
# This class is not as generic as Y2Storage::DiskSize and it is used to support | ||
# parsing bitrates information from `iwlist`. However, it could be extended | ||
# in the future if needed. | ||
class Bitrate | ||
include Comparable | ||
|
||
# Exception when trying to parse a string as a bitrate | ||
class ParseError < StandardError; end | ||
|
||
UNITS = ["b", "kb", "Mb", "Gb"].freeze | ||
private_constant :UNITS | ||
|
||
PARSE_REGEXP = /\A(\d+(?:.\d+)?)\ *(\w+)?/.freeze | ||
private_constant :PARSE_REGEXP | ||
|
||
class << self | ||
# Parses a string and converts the value to a string | ||
# | ||
# @example Parsing Mb/s | ||
# bitrate = Bitrate.parse("54 Mb/s") | ||
# bitrate.to_i #=> 54000000 | ||
# bitrate.to_s #=> "54 Mb/s" | ||
# | ||
# @param str [String] String to parse | ||
# @return [Bitrate] | ||
# @raise ParseError | ||
def parse(str) | ||
match = PARSE_REGEXP.match(str) | ||
raise ParseError unless match | ||
|
||
number, unit = match.captures | ||
unit ||= "b" | ||
power = UNITS.index(unit) | ||
raise ParseError unless power | ||
|
||
new(number.to_i * (1000**power)) | ||
end | ||
end | ||
|
||
# @param bits [Integer] Bits | ||
def initialize(bits) | ||
@bits = bits | ||
end | ||
|
||
# @return [Integer] Return the bits | ||
def to_i | ||
@bits | ||
end | ||
|
||
# Returns the string representation of the bitrate | ||
# | ||
# It automatically selects the unit to use depending on the bitrate value. | ||
# | ||
# @return [String] String representation | ||
def to_s | ||
power = (0..UNITS.length - 1).to_a.reverse.find do |u| | ||
to_i > (1000**u) | ||
end | ||
|
||
units = UNITS[power] | ||
number = @bits.to_f / (1000**power) | ||
number_str = number.to_s.sub(".0", "") # strip insignificant zeroes | ||
|
||
"#{number_str} #{units}/s" | ||
end | ||
|
||
# Compare two bitrates | ||
# | ||
# @param other [Bitrate] | ||
# @return [Integer] | ||
def <=>(other) | ||
to_i <=> other.to_i | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Copyright (c) [2021] SUSE LLC | ||
# | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can redistribute it and/or modify it | ||
# under the terms of version 2 of the GNU General Public License as published | ||
# by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, but WITHOUT | ||
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
# more details. | ||
# | ||
# You should have received a copy of the GNU General Public License along | ||
# with this program; if not, contact SUSE LLC. | ||
# | ||
# To contact SUSE LLC about this file by physical or electronic mail, you may | ||
# find current contact information at www.suse.com. | ||
|
||
require_relative "../test_helper" | ||
require "y2network/bitrate" | ||
|
||
describe Y2Network::Bitrate do | ||
describe ".parse" do | ||
context "when the string contains a 'kb/s'" do | ||
it "returns an object representing the same value" do | ||
bitrate = described_class.parse("54 kb/s") | ||
expect(bitrate.to_i).to eq(54_000) | ||
end | ||
end | ||
|
||
context "when the string contains a 'Mb/s'" do | ||
it "returns an object representing the same value" do | ||
bitrate = described_class.parse("54 Mb/s") | ||
expect(bitrate.to_i).to eq(54_000_000) | ||
end | ||
end | ||
|
||
context "when the string contains a 'Gb/s'" do | ||
it "returns an object representing the same value" do | ||
bitrate = described_class.parse("1 Gb/s") | ||
expect(bitrate.to_i).to eq(1_000_000_000) | ||
end | ||
end | ||
|
||
context "when the unit is missing from the string" do | ||
it "returns an object representing the same value" do | ||
bitrate = described_class.parse("64") | ||
expect(bitrate.to_i).to eq(64) | ||
end | ||
end | ||
|
||
context "when the string cannot be parsed" do | ||
it "raises a ParseError exception" do | ||
expect { described_class.parse("something") } | ||
.to raise_error(Y2Network::Bitrate::ParseError) | ||
end | ||
end | ||
end | ||
|
||
describe "#to_s" do | ||
context "Gb/s" do | ||
it "returns" do | ||
bitrate = described_class.new(54000000000) | ||
expect(bitrate.to_s).to eq("54 Gb/s") | ||
end | ||
end | ||
|
||
context "Mb/s" do | ||
it "returns" do | ||
bitrate = described_class.new(54000000) | ||
expect(bitrate.to_s).to eq("54 Mb/s") | ||
end | ||
end | ||
|
||
context "kb/s" do | ||
it "returns" do | ||
bitrate = described_class.new(54500) | ||
expect(bitrate.to_s).to eq("54.5 kb/s") | ||
end | ||
end | ||
|
||
context "bits" do | ||
it "returns" do | ||
bitrate = described_class.new(128) | ||
expect(bitrate.to_s).to eq("128 b/s") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.