diff --git a/README.md b/README.md index 3892b94..1c9b364 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,14 @@ identity_number.registered?("Ahmet", "Yılmaz", 1987) #=> false There is also a convenience method called `not_in_registry?` which is the logical equivalent of `!registered?`. +Use ```foreigner_registered?``` method to query the foreigner registry: + +```rb +identity_number.foreigner_registered?("Yukihiro", "Matsumoto", 14, 4, 1965) #=> false +``` + +There is also a convenience method called `foreigner_not_in_registry?` which is the logical equivalent of `!foreigner_registered?`. + ### Generating Relatives You can generate ID numbers for your younger or elder relatives. @@ -177,7 +185,7 @@ Yes. ## License -Copyright © 2015-2023 [Kerem Bozdas][Personal Webpage] +Copyright © 2015-2024 [Kerem Bozdas][Personal Webpage] This gem is available under the terms of the [MIT License][License]. diff --git a/lib/turkish_id.rb b/lib/turkish_id.rb index b6c301c..177e2ff 100644 --- a/lib/turkish_id.rb +++ b/lib/turkish_id.rb @@ -28,6 +28,14 @@ def not_in_registry?(given_name, surname, year_of_birth) !valid? || !query_government_registry(given_name, surname, year_of_birth) end + def foreigner_registered?(given_name, surname, year_of_birth, month_of_birth, day_of_birth) + valid? && query_foreigner_registry(given_name, surname, year_of_birth, month_of_birth, day_of_birth) + end + + def foreigner_not_in_registry?(given_name, surname, day_of_birth, month_of_birth, year_of_birth) + !valid? || !query_foreigner_registry(given_name, surname, day_of_birth, month_of_birth, year_of_birth) + end + private def calculate_checksum(id_array) @@ -99,4 +107,23 @@ def query_government_registry(given_name, surname, year_of_birth) rescue TypeError, ArgumentError, NoMethodError false end + + def query_foreigner_registry(given_name, surname, day_of_birth, month_of_birth, year_of_birth) + return false unless Integer(day_of_birth).between?(1, 31) + return false unless Integer(month_of_birth).between?(1, 12) + + client = Savon.client(wsdl: "https://tckimlik.nvi.gov.tr/Service/KPSPublicYabanciDogrula.asmx?WSDL") + response = client.call(:yabanci_kimlik_no_dogrula, message: { + "KimlikNo" => join_num(@id_number), + "Ad" => given_name.upcase(:turkic), + "Soyad" => surname.upcase(:turkic), + "DogumGun" => String(Integer(day_of_birth)), + "DogumAy" => String(Integer(month_of_birth)), + "DogumYil" => String(Date.new(Integer(year_of_birth)).year) + }) + + response.body.dig(:yabanci_kimlik_no_dogrula_response, :yabanci_kimlik_no_dogrula_result).is_a?(TrueClass) + rescue TypeError, ArgumentError, NoMethodError + false + end end diff --git a/spec/fixtures/vcr_cassettes/TurkishId/queries_foreigner_registry_for_numerically_valid_but_unregistered_id_number_and_returns_false.yml b/spec/fixtures/vcr_cassettes/TurkishId/queries_foreigner_registry_for_numerically_valid_but_unregistered_id_number_and_returns_false.yml new file mode 100644 index 0000000..ece0353 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/TurkishId/queries_foreigner_registry_for_numerically_valid_but_unregistered_id_number_and_returns_false.yml @@ -0,0 +1,171 @@ +--- +http_interactions: +- request: + method: get + uri: https://tckimlik.nvi.gov.tr/Service/KPSPublicYabanciDogrula.asmx?WSDL + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - private, max-age=0 + Content-Type: + - text/xml; charset=utf-8 + Vary: + - Accept-Encoding + Access-Control-Allow-Headers: + - Origin, X-Requested-With, Content-Type, Accept + Access-Control-Allow-Methods: + - POST,GET + Strict-Transport-Security: + - max-age=16070400;includeSubDomains + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1;mode=block + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self' + 'unsafe-eval' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/; + img-src 'self' 'unsafe-inline' data:;frame-src https://www.google.com/recaptcha/ + https://tckimliktest.nvi.gov.tr https://tckimlik.nvi.gov.tr; style-src 'self' + 'unsafe-inline' + Referer-Policy: + - no-referer + Date: + - Fri, 08 Nov 2024 20:44:33 GMT + Content-Length: + - '3852' + Set-Cookie: + - TS0193588c=01e4b3044250979e339679e5bc13a199177935a3be6c9798a60f8703d373dfd065556959a8ea0d311d10111f1c868b1bad0596049e; + Path=/; Domain=.tckimlik.nvi.gov.tr + body: + encoding: ASCII-8BIT + string: "\r\n\r\n \r\n \r\n + \ \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n" + recorded_at: Fri, 08 Nov 2024 20:44:34 GMT +- request: + method: post + uri: https://tckimlik.nvi.gov.tr/Service/KPSPublicYabanciDogrula.asmx + body: + encoding: UTF-8 + string: 99854496442JANEDOE111985 + headers: + Soapaction: + - '"http://tckimlik.nvi.gov.tr/WS/YabanciKimlikNoDogrula"' + Content-Type: + - text/xml;charset=UTF-8 + Content-Length: + - '929' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - private, max-age=0 + Content-Type: + - text/xml; charset=utf-8 + Vary: + - Accept-Encoding + Access-Control-Allow-Headers: + - Origin, X-Requested-With, Content-Type, Accept + Access-Control-Allow-Methods: + - POST,GET + Strict-Transport-Security: + - max-age=16070400;includeSubDomains + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1;mode=block + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self' + 'unsafe-eval' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/; + img-src 'self' 'unsafe-inline' data:;frame-src https://www.google.com/recaptcha/ + https://tckimliktest.nvi.gov.tr https://tckimlik.nvi.gov.tr; style-src 'self' + 'unsafe-inline' + Referer-Policy: + - no-referer + Date: + - Fri, 08 Nov 2024 20:44:34 GMT + Content-Length: + - '415' + Set-Cookie: + - TS0193588c=01e4b304428397fc512b7e8445a3583c25fc3775b663cfb291f8551772400ead7db55ae5f1db12cea2b5c75cf1ec847a49734e6ec8; + Path=/; Domain=.tckimlik.nvi.gov.tr + body: + encoding: ASCII-8BIT + string: false + recorded_at: Fri, 08 Nov 2024 20:44:34 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/fixtures/vcr_cassettes/TurkishId/queries_foreigner_registry_for_valid_id_number_and_returns_true.yml b/spec/fixtures/vcr_cassettes/TurkishId/queries_foreigner_registry_for_valid_id_number_and_returns_true.yml new file mode 100644 index 0000000..a1b2e51 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/TurkishId/queries_foreigner_registry_for_valid_id_number_and_returns_true.yml @@ -0,0 +1,172 @@ +--- +http_interactions: +- request: + method: get + uri: https://tckimlik.nvi.gov.tr/Service/KPSPublicYabanciDogrula.asmx?WSDL + body: + encoding: US-ASCII + string: '' + headers: + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - private, max-age=0 + Content-Type: + - text/xml; charset=utf-8 + Vary: + - Accept-Encoding + Access-Control-Allow-Headers: + - Origin, X-Requested-With, Content-Type, Accept + Access-Control-Allow-Methods: + - POST,GET + Strict-Transport-Security: + - max-age=16070400;includeSubDomains + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1;mode=block + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self' + 'unsafe-eval' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/; + img-src 'self' 'unsafe-inline' data:;frame-src https://www.google.com/recaptcha/ + https://tckimliktest.nvi.gov.tr https://tckimlik.nvi.gov.tr; style-src 'self' + 'unsafe-inline' + Referer-Policy: + - no-referer + Date: + - Fri, 08 Nov 2024 20:44:33 GMT + Content-Length: + - '3852' + Set-Cookie: + - TS0193588c=01e4b3044228649b4281a6d0992ca1c3b161ab4a7ab0a69a2be24a082e09abc209e91ccb4865911c98074064289c09c0c6aae8ecbc; + Path=/; Domain=.tckimlik.nvi.gov.tr + body: + encoding: ASCII-8BIT + string: "\r\n\r\n \r\n \r\n + \ \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n \r\n \r\n + \ \r\n + \ \r\n \r\n \r\n \r\n \r\n \r\n" + recorded_at: Fri, 08 Nov 2024 20:44:33 GMT +- request: + method: post + uri: https://tckimlik.nvi.gov.tr/Service/KPSPublicYabanciDogrula.asmx + body: + encoding: UTF-8 + string: 99911494534YUKIHIRO + MATZMATSUMOTO1441965 + headers: + Soapaction: + - '"http://tckimlik.nvi.gov.tr/WS/YabanciKimlikNoDogrula"' + Content-Type: + - text/xml;charset=UTF-8 + Content-Length: + - '941' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - private, max-age=0 + Content-Type: + - text/xml; charset=utf-8 + Vary: + - Accept-Encoding + Access-Control-Allow-Headers: + - Origin, X-Requested-With, Content-Type, Accept + Access-Control-Allow-Methods: + - POST,GET + Strict-Transport-Security: + - max-age=16070400;includeSubDomains + X-Frame-Options: + - SAMEORIGIN + X-Xss-Protection: + - 1;mode=block + X-Content-Type-Options: + - nosniff + Content-Security-Policy: + - default-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self' + 'unsafe-eval' 'unsafe-inline' https://www.google.com/recaptcha/ https://www.gstatic.com/recaptcha/; + img-src 'self' 'unsafe-inline' data:;frame-src https://www.google.com/recaptcha/ + https://tckimliktest.nvi.gov.tr https://tckimlik.nvi.gov.tr; style-src 'self' + 'unsafe-inline' + Referer-Policy: + - no-referer + Date: + - Fri, 08 Nov 2024 20:44:33 GMT + Content-Length: + - '414' + Set-Cookie: + - TS0193588c=01e4b304421bad68b7cbe32f51690295765b5e1b3d1fe6d100b9124c4a5f560ffa19014f709b74c4ec1b6224838f92e45cc1afc123; + Path=/; Domain=.tckimlik.nvi.gov.tr + body: + encoding: ASCII-8BIT + string: true + recorded_at: Fri, 08 Nov 2024 20:44:33 GMT +recorded_with: VCR 6.3.1 diff --git a/spec/turkish_id_spec.rb b/spec/turkish_id_spec.rb index 4561c8a..30ad971 100644 --- a/spec/turkish_id_spec.rb +++ b/spec/turkish_id_spec.rb @@ -135,4 +135,87 @@ expect(identity_number.registered?("Ayşe", nil, 2000)).to eq(false) assert_not_requested(stub_any) end + + it "queries foreigner registry for valid id number and returns true" do + identity_number = TurkishId.new("99911494534") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("Yukihiro Matz", "Matsumoto", 14, 4, 1965)).to eq(true) + end + + it "queries foreigner registry for numerically valid but unregistered id number and returns false" do + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_not_in_registry?("Jane", "Doe", 1, 1, 1985)).to eq(true) + end + + it "does not query foreigner registry for invalid id number" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("10000000000") + expect(identity_number.valid?).to eq(false) + expect(identity_number.foreigner_registered?("Invalid", "Person", 1, 1, 1989)).to eq(false) + assert_not_requested(stub_any) + end + + it "does not query foreigner registry for invalid day of birth" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("John", "Doe", 32, 1, 1966)).to eq(false) + assert_not_requested(stub_any) + end + + it "does not query foreigner registry for invalid month of birth" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("John", "Doe", 1, 13, 1983)).to eq(false) + assert_not_requested(stub_any) + end + + it "does not query foreigner registry for invalid year of birth" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("John", "Doe", 1, 1, "2nd Millennium")).to eq(false) + assert_not_requested(stub_any) + end + + it "does not query foreigner registry for nil given name" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?(nil, "Doe", 1, 1, 1980)).to eq(false) + assert_not_requested(stub_any) + end + + it "does not query foreigner registry for nil surname" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("John", nil, 1, 1, 1980)).to eq(false) + assert_not_requested(stub_any) + end + it "does not query foreigner registry for nil day of birth" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("John", "Doe", nil, 1, 1954)).to eq(false) + assert_not_requested(stub_any) + end + + it "does not query foreigner registry for nil month of birth" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("John", "Doe", 1, nil, 1966)).to eq(false) + assert_not_requested(stub_any) + end + + it "does not query foreigner registry for nil year of birth" do + stub_any = stub_request(:any, "tckimlik.nvi.gov.tr") + identity_number = TurkishId.new("99854496442") + expect(identity_number.valid?).to eq(true) + expect(identity_number.foreigner_registered?("John", "Doe", 1, 1, nil)).to eq(false) + assert_not_requested(stub_any) + end end