-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from taketo1113/option-ipv4-ipv6
Add ipv4/ipv6 options
- Loading branch information
Showing
9 changed files
with
192 additions
and
46 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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
/tmp/ | ||
|
||
/Gemfile.lock | ||
.ruby-version | ||
|
||
# rspec failure tracking | ||
.rspec_status |
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ddig | ||
class Ip | ||
attr_reader :ip_type | ||
|
||
def initialize(use_ipv4: nil, use_ipv6: nil) | ||
@use_ipv4 = use_ipv4 | ||
@use_ipv6 = use_ipv6 | ||
|
||
set_ip_type | ||
end | ||
|
||
def set_ip_type | ||
if @use_ipv4.nil? && self.class.enable_ipv4? | ||
@use_ipv4 = true | ||
end | ||
|
||
if @use_ipv6.nil? && self.class.enable_ipv6? | ||
@use_ipv6 = true | ||
end | ||
|
||
if @use_ipv4 && @use_ipv6 | ||
@ip_type = :all | ||
elsif @use_ipv4 | ||
@ip_type = :ipv4 | ||
elsif @use_ipv6 | ||
@ip_type = :ipv6 | ||
end | ||
end | ||
|
||
def self.enable_ipv4? | ||
ip_list.any? { |addr| addr.ipv4? } | ||
end | ||
|
||
def self.enable_ipv6? | ||
ip_list.any? { |addr| addr.ipv6? } | ||
end | ||
|
||
private | ||
|
||
def self.ip_list | ||
Socket.ip_address_list.map do |addrinfo| | ||
if RUBY_VERSION < '3.1' | ||
# for ipaddr gem <= v1.2.2, Not support zone identifiers in IPv6 addresses | ||
addr = IPAddr.new(addrinfo.ip_address.split('%').first) | ||
else | ||
addr = IPAddr.new(addrinfo.ip_address) | ||
end | ||
|
||
if !addr.loopback? && !addr.link_local? | ||
addr | ||
end | ||
end.compact | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Ddig::Ip do | ||
it "#enable_ipv4?" do | ||
skip "IPv4 enabled: #{Ddig::Ip.enable_ipv4?}" unless enable_ipv4? | ||
expect(Ddig::Ip.enable_ipv4?).to be true | ||
end | ||
|
||
it "#enable_ipv6?" do | ||
skip "IPv6 enabled: #{Ddig::Ip.enable_ipv6?}" unless enable_ipv6? | ||
expect(Ddig::Ip.enable_ipv6?).to be true | ||
end | ||
|
||
context "ip_type" do | ||
it "return :all with use_ipv4 & use_ipv6 is true" do | ||
use_ipv4 = true | ||
use_ipv6 = true | ||
|
||
expect(Ddig::Ip.new(use_ipv4: use_ipv4, use_ipv6: use_ipv6).ip_type).to be :all | ||
end | ||
|
||
it "return :ipv4 with use_ipv4: true & use_ipv6: false" do | ||
use_ipv4 = true | ||
use_ipv6 = false | ||
|
||
expect(Ddig::Ip.new(use_ipv4: use_ipv4, use_ipv6: use_ipv6).ip_type).to be :ipv4 | ||
end | ||
|
||
it "return :all with use_ipv4: false & use_ipv6: true" do | ||
use_ipv4 = false | ||
use_ipv6 = true | ||
|
||
expect(Ddig::Ip.new(use_ipv4: use_ipv4, use_ipv6: use_ipv6).ip_type).to be :ipv6 | ||
end | ||
|
||
context "use_ipv4/use_ipv6: nil" do | ||
before(:each) do | ||
@use_ipv4 = nil | ||
@use_ipv6 = nil | ||
end | ||
|
||
it "return ip_type via enable_ipv4?/enable_ipv6?" do | ||
if enable_ipv4? && enable_ipv6? | ||
ip_type = :all | ||
elsif enable_ipv4? | ||
ip_type = :ipv4 | ||
elsif enable_ipv6? | ||
ip_type = :ipv6 | ||
end | ||
|
||
expect(Ddig::Ip.new(use_ipv4: @use_ipv4, use_ipv6: @use_ipv6).ip_type).to be ip_type | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Ddig | ||
module Ipv6Helper | ||
def enable_ipv4? | ||
Ddig::Ip.enable_ipv4? | ||
end | ||
|
||
def enable_ipv6? | ||
Ddig::Ip.enable_ipv6? | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.