diff --git a/README.md b/README.md index 5577aa4..030bf96 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,11 @@ $ kokki japan 🇯🇵 $ kokki 202.214.194.147 🇯🇵 +# it suports a defanged ip as an input +$ kokki 1.1.1[.]1 +🇦🇺 +$ kokki "1.1.1(.)1" +🇦🇺 ``` ### As a library diff --git a/lib/kokki/converter.rb b/lib/kokki/converter.rb index b95361d..2e5039b 100644 --- a/lib/kokki/converter.rb +++ b/lib/kokki/converter.rb @@ -36,8 +36,12 @@ def self.convert(input) private + def refang(input) + input.gsub("[.]", ".").gsub("(.)", ".") + end + def convert_as_ip_address(ip_address) - ip = IPAddress.new(ip_address) + ip = IPAddress.new(refang(ip_address)) dict.lookup_by_alpha_2_code ip.country_code end end diff --git a/spec/converter_spec.rb b/spec/converter_spec.rb index db1b140..efee1cb 100644 --- a/spec/converter_spec.rb +++ b/spec/converter_spec.rb @@ -41,6 +41,13 @@ end end + context "when given a defanged ip address" do + it do + flag = subject.convert("1[.]0[.]16[.]0") + expect(flag).to eq("🇯🇵") + end + end + context "when given an invalid input" do it "raises an InvalidError" do expect { subject.convert("test") }.to raise_error(Kokki::InvalidInputError) diff --git a/spec/fixtures/vcr_cassettes/Kokki_Converter/_convert/when_given_a_defanged_ip_address/1_1_5_1.yml b/spec/fixtures/vcr_cassettes/Kokki_Converter/_convert/when_given_a_defanged_ip_address/1_1_5_1.yml new file mode 100644 index 0000000..e6189a2 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/Kokki_Converter/_convert/when_given_a_defanged_ip_address/1_1_5_1.yml @@ -0,0 +1,56 @@ +--- +http_interactions: +- request: + method: get + uri: https://ipinfo.io/1.0.16.0/geo + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + Host: + - ipinfo.io + response: + status: + code: 200 + message: OK + headers: + Date: + - Sat, 28 Sep 2019 11:15:53 GMT + Content-Type: + - application/json; charset=utf-8 + Vary: + - Accept-Encoding + Access-Control-Allow-Origin: + - "*" + X-Frame-Options: + - DENY + X-Xss-Protection: + - 1; mode=block + X-Content-Type-Options: + - nosniff + Referrer-Policy: + - strict-origin-when-cross-origin + Via: + - 1.1 google + Transfer-Encoding: + - chunked + body: + encoding: ASCII-8BIT + string: |- + { + "ip": "1.0.16.0", + "city": "", + "region": "", + "country": "JP", + "loc": "35.6900,139.6900", + "readme": "https://ipinfo.io/missingauth" + } + http_version: + recorded_at: Sat, 28 Sep 2019 11:15:51 GMT +recorded_with: VCR 5.0.0