Skip to content

Commit

Permalink
Merge 1e6089f into 8483ec4
Browse files Browse the repository at this point in the history
  • Loading branch information
ninoseki committed Jul 4, 2019
2 parents 8483ec4 + 1e6089f commit a055241
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
9 changes: 7 additions & 2 deletions lib/kokki/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ def initialize(input)
def convert
flag = nil

flag = dict.lookup_by_alpha_2_code(upcase) if input.length == 2
flag = dict.lookup_by_alpha_3_code(upcase) if input.length == 3
case input.length
when 2
flag = dict.lookup_by_alpha_2_code(upcase)
when 3
flag = dict.lookup_by_alpha_3_code(upcase)
end

flag ||= dict.lookup_by_name(upcase)
flag ||= convert_as_ip_address(input)

Expand Down
4 changes: 2 additions & 2 deletions lib/kokki/ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def country_code
@country_code ||=
[].tap do |out|
out << Geocoder.search(address)&.first&.country_code&.upcase
rescue Geocoder::Error => _
rescue Geocoder::Error => _e
out << nil
end.first
end
Expand All @@ -27,7 +27,7 @@ def country_code
def ip_address?(address)
IPAddr.new(address)
true
rescue IPAddr::InvalidAddressError => _
rescue IPAddr::InvalidAddressError => _e
false
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

describe ".start" do
context "when given a valid input" do
it "should return a country flag of a given country" do
it "returns a country flag of a given country" do
output = capture(:stdout) { subject.start "JP" }
expect(output).to eq("🇯🇵")
end
end

context "when given an invalid input" do
it "should return a country flag of a given country" do
it "returns a country flag of a given country" do
output = capture(:stdout) { subject.start "hoge" }.chomp
expect(output).to eq("invalid input: hoge")
end
Expand Down
10 changes: 5 additions & 5 deletions spec/converter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

describe ".convert" do
context "when given an alpha-2-code" do
it "should return a country flag of a given country" do
it "returns a country flag of a given country" do
flag = subject.convert("US")
expect(flag).to eq("🇺🇸")

Expand All @@ -15,7 +15,7 @@
end

context "when given an alpha-3-code" do
it "should return a country flag of a given country" do
it "returns a country flag of a given country" do
flag = subject.convert("USA")
expect(flag).to eq("🇺🇸")

Expand All @@ -25,7 +25,7 @@
end

context "when given a country name" do
it "should return a country flag of a given country" do
it "returns a country flag of a given country" do
flag = subject.convert("United States")
expect(flag).to eq("🇺🇸")

Expand All @@ -35,14 +35,14 @@
end

context "when given an ip address" do
it "should return a country flag of a given country" do
it "returns a country flag of a given country" do
flag = subject.convert("1.0.16.0")
expect(flag).to eq("🇯🇵")
end
end

context "when given an invalid input" do
it "should raise an InvalidError" do
it "raises an InvalidError" do
expect { subject.convert("test") }.to raise_error(Kokki::InvalidInputError)
expect { subject.convert("zz") }.to raise_error(Kokki::InvalidInputError)
expect { subject.convert("127.0.0.1") }.to raise_error(Kokki::InvalidInputError)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions spec/kokki_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe Kokki do
subject { Kokki }
subject { described_class }

it "has a version number" do
expect(Kokki::VERSION).not_to be nil
Expand All @@ -12,8 +12,8 @@
allow(Kokki::Converter).to receive(:convert)
end

it "should call Converter.convert" do
Kokki.flagize "US"
it "calls Converter.convert" do
described_class.flagize "US"

expect(Kokki::Converter).to have_received(:convert).once
end
Expand Down

0 comments on commit a055241

Please sign in to comment.