net-macaddress 0.0.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of net-macaddress might be problematic. Click here for more details.

checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8c3d9c660e099d7cb4f748cad151e505f2126895a41fff245360d929d8aec9e8
4
+ data.tar.gz: 12363966cc3d5c125ecf06f78028c5ed85e06b39f7010c23ecbd389116d8b468
5
+ SHA512:
6
+ metadata.gz: 5d2c2488f6cbb04f790740b110fb84780ad31a48682e565c43b9ebea96738f96aab15eb1209a39f68714d05dccbc7734b6e3fc48c81157abbf2cefead25d3047
7
+ data.tar.gz: 4889be15c64843607a9741f18c1c926757a8cf76d34653e971d83b0ad8c7c319fd10b1b2f6c980ef0cb9a1eb3ce7b9033b3e829f6e44476f34a03da30957dd09
data/.gitignore ADDED
@@ -0,0 +1,32 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ coverage.data
7
+ InstalledFiles
8
+ lib/bundler/man
9
+ pkg
10
+ rdoc
11
+ spec/reports
12
+ test/tmp
13
+ test/version_tmp
14
+ tmp
15
+
16
+ # YARD artifacts
17
+ .yardoc
18
+ _yardoc
19
+ doc/
20
+
21
+ # tmp-old
22
+ .old
23
+
24
+ # editor
25
+ *~
26
+ .redcar
27
+
28
+ # other
29
+ *.lock
30
+ *.bak
31
+ tool/
32
+ *\#*
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - ruby-head
5
+ - 2.6
6
+ - 2.5
7
+ - rbx-4
8
+ - jruby-head
9
+
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ - rvm: rbx-4
14
+ - rvm: jruby-head
15
+
16
+ before_install:
17
+ - gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rake'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rake'
11
+ end
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT X11 License)
2
+
3
+ Copyright (c) 2012 Kenichi Kamiya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ net-macaddress
2
+ ==============
3
+
4
+ [![Build Status](https://secure.travis-ci.org/kachick/net-macaddress.png)](http://travis-ci.org/kachick/net-macaddress)
5
+ [![Gem Version](https://badge.fury.io/rb/net-macaddress.png)](http://badge.fury.io/rb/net-macaddress)
6
+
7
+ Description
8
+ ------------
9
+
10
+ Utils for MACaddresses
11
+
12
+ Features
13
+ --------
14
+
15
+ * Usuful methods
16
+ * Immutable
17
+
18
+ Usage
19
+ -----
20
+
21
+ ### Setup
22
+
23
+ ```ruby
24
+ require 'net/macaddress'
25
+ include Net
26
+ ```
27
+
28
+ Requirements
29
+ ------------
30
+
31
+ * [Ruby 2.5 or later](http://travis-ci.org/#!/kachick/net-macaddress)
32
+
33
+ Installation
34
+ -------------
35
+
36
+ ```shell
37
+ $ gem install net-macaddress
38
+ ```
39
+
40
+ Link
41
+ ----
42
+
43
+ * [code](https://github.com/kachick/net-macaddress)
44
+ * [issues](https://github.com/kachick/net-macaddress/issues)
45
+ * [CI](http://travis-ci.org/#!/kachick/net-macaddress)
46
+ * [gem](https://rubygems.org/gems/net-macaddress)
47
+ * [API-documents](http://www.rubydoc.info/github/kachick/net-macaddress)
48
+
49
+ License
50
+ -------
51
+
52
+ The MIT X11 License
53
+ Copyright (c) 2012 Kenichi Kamiya
54
+ See MIT-LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ task default: [:spec]
7
+
8
+ RSpec::Core::RakeTask.new(:spec) do |t|
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ t.ruby_opts = %w[-w]
11
+ end
@@ -0,0 +1,17 @@
1
+ $VERBOSE = true
2
+
3
+ require_relative '../lib/net/macaddress'
4
+
5
+ include Net
6
+
7
+ macaddr = MACAddress.parse('00-f1-11-02-07-9f')
8
+ p macaddr #=> #<Net::MACAddress 00-f1-11-02-07-9f>
9
+ p macaddr.oui #=> [#<Net::MACAddress::Octet 00: 00000000>, #<Net::MACAddress::Octet f1: 11110001>, #<Net::MACAddress::Octet 11: 00010001>]
10
+ p macaddr.globaly? #=> true
11
+ p macaddr.localy? #=> false
12
+ p macaddr.unicast? #=> true
13
+ p macaddr.multicast? #=> false
14
+ p macaddr.vrrp? #=> false
15
+ p MACAddress.parse('00-00-5E-00-01-F7').vrrp? #=> true
16
+ p MACAddress.valid?('00-00-5E-00-01-F7') #=> true
17
+ p MACAddress.valid?('00-00-5E-00-01-FG') #=> false
@@ -0,0 +1,119 @@
1
+ # Copyright (c) 2012 Kenichi Kamiya
2
+
3
+ require_relative 'macaddress/version'
4
+ require_relative 'macaddress/exceptions'
5
+ require_relative 'macaddress/octet'
6
+
7
+ module Net
8
+
9
+ # Immutable
10
+ class MACAddress
11
+
12
+ DELIMITER_PATTERN = /[:\-]/
13
+
14
+ PATTERN = /\A(?:#{Octet::PATTERN_STR}[:\-]){5}#{Octet::PATTERN_STR}\z/
15
+
16
+ class << self
17
+
18
+ def parse(str)
19
+ raise MalformedDataError unless valid? str
20
+
21
+ new str.split(DELIMITER_PATTERN).map{|oct|Octet.parse oct}
22
+ end
23
+
24
+ def valid?(str)
25
+ !!PATTERN.match(str)
26
+ end
27
+
28
+ end
29
+
30
+ # @param octets [Array<Octet>] - [Octet * 6]
31
+ def initialize(octets)
32
+ raise TypeError unless octets.length == 6 and octets.all?{|o|o.kind_of? Octet}
33
+
34
+ @octets = octets.dup.freeze
35
+ end
36
+
37
+ def octets
38
+ @octets.dup
39
+ end
40
+
41
+ def oui
42
+ @octets.take 3
43
+ end
44
+
45
+ alias_method :company_id, :oui
46
+
47
+ def extension_id
48
+ @octets.drop 3
49
+ end
50
+
51
+ alias_method :ei, :extension_id
52
+
53
+ def individual_group_bit
54
+ self[1][1]
55
+ end
56
+
57
+ alias_method :ig_bit, :individual_group_bit
58
+
59
+ def universal_local_bit
60
+ self[1][2]
61
+ end
62
+
63
+ alias_method :ui_bit, :universal_local_bit
64
+
65
+ def unicast?
66
+ individual_group_bit == 0
67
+ end
68
+
69
+ def multicast?
70
+ individual_group_bit == 1
71
+ end
72
+
73
+ def globaly?
74
+ universal_local_bit == 0
75
+ end
76
+
77
+ def localy?
78
+ universal_local_bit == 1
79
+ end
80
+
81
+ def to_s(delimiter='-')
82
+ format_octets @octets, delimiter
83
+ end
84
+
85
+ def inspect
86
+ "#<#{self.class} #{to_s}>"
87
+ end
88
+
89
+ def ==(other)
90
+ self.class == other.class && octets == other.octets
91
+ end
92
+
93
+ def hash
94
+ @octets.hash
95
+ end
96
+
97
+ def eql?(other)
98
+ self.class.eql?(other.class) && octets.eql?(other.octets)
99
+ end
100
+
101
+ # RFC3768
102
+ def vrrp?
103
+ format_octets(@octets.take(5), '-') == '00-00-5e-00-01'
104
+ end
105
+
106
+ private
107
+
108
+ # @param offset [Fixnum] 1..6
109
+ def [](offset)
110
+ @octets[offset - 1]
111
+ end
112
+
113
+ def format_octets(octets, delimiter)
114
+ octets.map(&:to_s).join delimiter
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,12 @@
1
+ # Copyright (c) 2012 Kenichi Kamiya
2
+
3
+ module Net
4
+
5
+ # Immutable
6
+ class MACAddress
7
+
8
+ class MalformedDataError < StandardError; end
9
+
10
+ end
11
+
12
+ end
@@ -0,0 +1,95 @@
1
+ # Copyright (c) 2012 Kenichi Kamiya
2
+
3
+ module Net
4
+
5
+ # Immutable
6
+ class MACAddress
7
+
8
+ # Immutable
9
+ class Octet
10
+
11
+ RADIXIES = [128, 64, 32, 16, 8, 4, 2, 1].freeze
12
+ DEF_BIT = {0 => 0, 1 => 1, true => 1, false => 0}.freeze
13
+ PATTERN_STR = '[a-fA-F0-9]{2}'.freeze
14
+ PATTERN = /\A#{PATTERN_STR}\z/
15
+
16
+ class << self
17
+
18
+ def parse(str)
19
+ raise MalformedDataError unless PATTERN.match str
20
+
21
+ new bits_for(str.to_i(16))
22
+ end
23
+
24
+ def bit_for(obj)
25
+ raise ArgumentError unless DEF_BIT.has_key? obj
26
+
27
+ DEF_BIT[obj]
28
+ end
29
+
30
+ private
31
+
32
+ def bits_for(int)
33
+ int = int.to_int
34
+ raise ArgumentError unless (0..255).cover? int
35
+
36
+ int.to_s(2).rjust(8, '0').split('').map{|c|Integer c}
37
+ end
38
+
39
+ end
40
+
41
+ def initialize(bits)
42
+ raise ArgumentError unless bits.length == 8
43
+
44
+ @bits = bits.map{|bit|bit_for bit}.freeze
45
+ end
46
+
47
+ def bits
48
+ @bits.dup
49
+ end
50
+
51
+ def to_i
52
+ RADIXIES.each_with_index.inject(0) {|sum, radix_index|
53
+ radix, index = *radix_index
54
+ sum + radix * @bits[index]
55
+ }
56
+ end
57
+
58
+ def to_s
59
+ to_i.to_s(16).rjust(2, '0')
60
+ end
61
+
62
+ def inspect
63
+ "#<#{self.class} #{to_s}: #{@bits.join}>"
64
+ end
65
+
66
+ # 1..8
67
+ # The reversing access from Ruby's Array
68
+ def [](pos)
69
+ index = pos.to_int
70
+ raise IndexError unless (1..8).cover? index
71
+
72
+ @bits[8 - index]
73
+ end
74
+
75
+ def eql?(other)
76
+ self.class == other.class && to_i == other.to_i
77
+ end
78
+
79
+ alias_method :==, :eql?
80
+
81
+ def hash
82
+ @bits.hash
83
+ end
84
+
85
+ private
86
+
87
+ def bit_for(obj)
88
+ self.class.__send__ __callee__, obj
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+
95
+ end
@@ -0,0 +1,7 @@
1
+ # coding: us-ascii
2
+
3
+ module Net; class MACAddress
4
+
5
+ VERSION = '0.0.1'.freeze
6
+
7
+ end; end
@@ -0,0 +1,31 @@
1
+ # coding: us-ascii
2
+
3
+ lib_name = 'net-macaddress'.freeze
4
+ require "./lib/net/macaddress/version"
5
+
6
+ Gem::Specification.new do |gem|
7
+ # specific
8
+
9
+ gem.description = %q{Utils for MACAddresses}
10
+ gem.summary = gem.description.dup
11
+ gem.homepage = "https://github.com/kachick/#{lib_name}"
12
+ gem.license = 'MIT'
13
+ gem.name = lib_name
14
+ gem.version = Net::MACAddress::VERSION.dup
15
+
16
+ gem.add_development_dependency 'rspec', '>= 3.8', '< 4'
17
+ gem.add_development_dependency 'yard', '>= 0.9.20', '< 2'
18
+
19
+ if RUBY_ENGINE == 'rbx'
20
+ gem.add_dependency 'rubysl', '~> 2.2'
21
+ end
22
+
23
+ # common
24
+
25
+ gem.authors = ['Kenichi Kamiya']
26
+ gem.email = ['kachick1+ruby@gmail.com']
27
+ gem.files = `git ls-files`.split($\)
28
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
29
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
30
+ gem.require_paths = ['lib']
31
+ end
data/reference.md ADDED
@@ -0,0 +1,5 @@
1
+ # Reference
2
+
3
+ * http://standards.ieee.org/develop/regauth/tut/index.html
4
+ * http://en.wikipedia.org/wiki/MAC_address
5
+ * http://sy2920.s151.xrea.com/network/mac-address.html
@@ -0,0 +1,44 @@
1
+ # coding: us-ascii
2
+
3
+ require_relative 'spec_helper'
4
+
5
+ describe Net::MACAddress do
6
+ describe '.parse' do
7
+ subject { described_class.parse address }
8
+
9
+ context 'with valid address' do
10
+ let!(:address) { '04-A3-43-5F-43-23' }
11
+
12
+ it { is_expected.to be_an_instance_of(Net::MACAddress) }
13
+ end
14
+
15
+ context 'with invalid address' do
16
+ let!(:address) { '04-A3-43-5F-43-23-' }
17
+
18
+ it { expect { subject }.to raise_error(Net::MACAddress::MalformedDataError) }
19
+ end
20
+ end
21
+
22
+ describe '.valid?' do
23
+ subject { described_class.valid? address }
24
+
25
+ context 'with valid address' do
26
+ let!(:address) { '04-A3-43-5F-43-23' }
27
+
28
+ it { is_expected.to eq(true) }
29
+ end
30
+
31
+ context 'with invalid address' do
32
+ let!(:address) { '04-A3-43-5F-43-23-' }
33
+
34
+ it { is_expected.to eq(false) }
35
+ end
36
+ end
37
+
38
+ describe '#==' do
39
+ it do
40
+ expect(described_class.parse('04-A3-43-5F-43-23') == described_class.parse('04-A3-43-5F-43-23')).to eq(true)
41
+ expect(described_class.parse('04-A3-43-5F-43-23') == described_class.parse('04-A3-43-5F-43-24')).to eq(false)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ # coding: us-ascii
2
+ require 'rspec'
3
+
4
+ require_relative '../lib/net/macaddress'
5
+
6
+ module Net::MACAddress::RspecHelpers
7
+ end
8
+
9
+ RSpec.configure do |configuration|
10
+ configuration.include Net::MACAddress::RspecHelpers
11
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: net-macaddress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kenichi Kamiya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.8'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.8'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4'
33
+ - !ruby/object:Gem::Dependency
34
+ name: yard
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 0.9.20
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '2'
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: 0.9.20
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '2'
53
+ description: Utils for MACAddresses
54
+ email:
55
+ - kachick1+ruby@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - ".gitignore"
61
+ - ".travis.yml"
62
+ - Gemfile
63
+ - MIT-LICENSE
64
+ - README.md
65
+ - Rakefile
66
+ - examples/example.rb
67
+ - lib/net/macaddress.rb
68
+ - lib/net/macaddress/exceptions.rb
69
+ - lib/net/macaddress/octet.rb
70
+ - lib/net/macaddress/version.rb
71
+ - net-macaddress.gemspec
72
+ - reference.md
73
+ - spec/overview_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: https://github.com/kachick/net-macaddress
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.7.6
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Utils for MACAddresses
99
+ test_files:
100
+ - spec/overview_spec.rb
101
+ - spec/spec_helper.rb