ipaddr-ext 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 84e45c02d4f47c83919da49e0ed33015bec5b70b0f520925ae08e5a7fb2005c3
4
+ data.tar.gz: 3c146451e5fc613b3aacf1fd5db662c253b2653b12c32a40c9f91fc18b11f67f
5
+ SHA512:
6
+ metadata.gz: 9860c9fa616cd56c56094f563410aae1f2ea4a956c63c7b520fa13503b8245af5934075b817df5c2f18d8a6db79182d24abfba4f1b011778b1e120754ed8b371
7
+ data.tar.gz: a80046f2c50c372008890e3ca8095b60d1189ee00182bb3a95784167a58d1c7456d1e26b41baee6b0de58dd97efb76b9b6584d4b664e085f672c52d70a4e9fb0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Interop Tokyo ShowNet NOC team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # IPAddrExt
2
+
3
+ IPAddrExt provides extensions of IPAddr class.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add ipaddr-ext
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install ipaddr-ext
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'ipaddr-ext'
19
+
20
+ # to_s_with_prefix
21
+ ipaddr1 = IPAddr.new("3ffe:505:2::1")
22
+ p ipaddr1.to_s_with_prefix
23
+ #=> "3ffe:505:2::1/128"
24
+
25
+ ipaddr2 = IPAddr.new("3ffe:505:2::/64")
26
+ p ipaddr2.to_s_with_prefix
27
+ #=> "3ffe:505:2::/64"
28
+
29
+ # +/-
30
+ ipaddr3 = IPAddr.new("3ffe:505:2::1")
31
+ p (ipaddr3 + 5)
32
+ #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0006/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
33
+ p (ipaddr3 + 5).to_s_with_prefix
34
+ #=> "3ffe:505:2::6/128"
35
+
36
+ ipaddr4 = IPAddr.new("3ffe:505:2::9")
37
+ p (ipaddr4 - 5)
38
+ #=> #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0004/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
39
+ p (ipaddr4 - 5).to_s_with_prefix
40
+ #=> "3ffe:505:2::4/128"
41
+
42
+ # broadcast / wildcard_mask
43
+ ipaddr5 = IPAddr.new("192.0.2.0/24")
44
+ ipaddr5.broadcast
45
+ => #<IPAddr: IPv4:192.0.2.255/255.255.255.0>
46
+ ipaddr5.wildcard_mask
47
+ => "0.0.0.255"
48
+
49
+ # to_host
50
+ ipaddr5 = IPAddr.new("3ffe:505:2::/64")
51
+ ipaddr5.to_host
52
+ => #<IPAddr: IPv6:3ffe:0505:0002:0000:0000:0000:0000:0000/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff>
53
+ ipaddr5.to_host.to_s_with_prefix
54
+ => "3ffe:505:2::/128"
55
+ ipaddr5.to_host.succ.to_s_with_prefix
56
+ => "3ffe:505:2::1/128"
57
+
58
+ # to_json: export with prefix string
59
+ IPAddr.new("3ffe:505:2::/64").to_json
60
+ => "\"3ffe:505:2::/64\""
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/interop-tokyo-shownet/ipaddr-ext. This project is intended to be a safe, welcoming space for collaboration.
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ipaddr-ext/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ipaddr-ext"
7
+ spec.version = IPAddrExt::VERSION
8
+ spec.authors = ["Taketo Takashima"]
9
+ spec.email = ["t.taketo1113@gmail.com"]
10
+
11
+ spec.summary = "Extensions for IPAddr Class"
12
+ spec.description = "Extensions for IPAddr Class"
13
+ spec.homepage = "https://github.com/interop-tokyo-shownet/ipaddr-ext"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/interop-tokyo-shownet/ipaddr-ext"
19
+ spec.metadata["changelog_uri"] = "https://github.com/interop-tokyo-shownet/ipaddr-ext/releases"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "rake", ">= 13.0"
33
+ spec.add_development_dependency "rspec", ">= 3.0"
34
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IPAddrExt
4
+ module Extensions
5
+ # Returns the broadcast address
6
+ def broadcast
7
+ case @family
8
+ when Socket::AF_INET
9
+ unless prefix == 32
10
+ broadcast_addr = @addr + (IPAddr::IN4MASK ^ @mask_addr)
11
+ return self.clone.set(broadcast_addr, @family)
12
+ else
13
+ return self.clone
14
+ end
15
+
16
+ when Socket::AF_INET6
17
+ nil
18
+
19
+ else
20
+ raise AddressFamilyError, "unsupported address family"
21
+ end
22
+ end
23
+
24
+ # Returns the wildcard mask in string format e.g. 0.0.255.255
25
+ # ref: https://github.com/ruby/ipaddr/pull/44
26
+ def wildcard_mask
27
+ case @family
28
+ when Socket::AF_INET
29
+ mask = IPAddr::IN4MASK ^ @mask_addr
30
+ when Socket::AF_INET6
31
+ mask = IPAddr::IN6MASK ^ @mask_addr
32
+ else
33
+ raise AddressFamilyError, "unsupported address family"
34
+ end
35
+
36
+ _to_string(mask)
37
+ end
38
+
39
+ # Returns a address greater than the original address by offset
40
+ # @param offset [Integer]
41
+ def +(offset)
42
+ self.clone.set(@addr + offset, @family)
43
+ end
44
+
45
+ # Returns a address less than the original address by offset
46
+ # @param offset [Integer]
47
+ def -(offset)
48
+ self.clone.set(@addr - offset, @family)
49
+ end
50
+
51
+ # Returns the host address
52
+ def to_host
53
+ case @family
54
+ when Socket::AF_INET
55
+ self.mask(32)
56
+ when Socket::AF_INET6
57
+ self.mask(128)
58
+ else
59
+ raise AddressFamilyError, "unsupported address family"
60
+ end
61
+ end
62
+
63
+ # Returns the address with prefix
64
+ def to_s_with_prefix
65
+ "#{self.to_s}/#{prefix}"
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IPAddrExt
4
+ module JSON
5
+ def to_json
6
+ if ipv4? && prefix == 32
7
+ format("\"%s\"", to_s)
8
+ elsif ipv6? && prefix == 128
9
+ format("\"%s\"", to_s)
10
+ else
11
+ format("\"%s/%s\"", to_s, prefix)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module IPAddrExt
4
+ VERSION = "0.1.0"
5
+ end
data/lib/ipaddr-ext.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "ipaddr"
2
+ require 'json'
3
+
4
+ require "ipaddr-ext/version"
5
+ require "ipaddr-ext/extensions"
6
+ require "ipaddr-ext/json"
7
+
8
+ IPAddr.send(:include, IPAddrExt::Extensions)
9
+ IPAddr.send(:include, IPAddrExt::JSON)
@@ -0,0 +1,4 @@
1
+ module IPAddrExt
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ipaddr-ext
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Taketo Takashima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: Extensions for IPAddr Class
42
+ email:
43
+ - t.taketo1113@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - ipaddr-ext.gemspec
54
+ - lib/ipaddr-ext.rb
55
+ - lib/ipaddr-ext/extensions.rb
56
+ - lib/ipaddr-ext/json.rb
57
+ - lib/ipaddr-ext/version.rb
58
+ - sig/ipaddr-ext.rbs
59
+ homepage: https://github.com/interop-tokyo-shownet/ipaddr-ext
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ homepage_uri: https://github.com/interop-tokyo-shownet/ipaddr-ext
64
+ source_code_uri: https://github.com/interop-tokyo-shownet/ipaddr-ext
65
+ changelog_uri: https://github.com/interop-tokyo-shownet/ipaddr-ext/releases
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 2.7.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.3.26
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Extensions for IPAddr Class
85
+ test_files: []