From a363da8d8af8e55fbe11782d50b19cd7a5c9b0f2 Mon Sep 17 00:00:00 2001 From: Anton Maminov Date: Fri, 5 Apr 2024 11:59:09 +0300 Subject: [PATCH] Specific location field --- src/ipapi.cr | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/ipapi.cr b/src/ipapi.cr index d1333ce..546dfbf 100644 --- a/src/ipapi.cr +++ b/src/ipapi.cr @@ -7,6 +7,28 @@ require "json" module Ipapi VERSION = {{ `shards version #{__DIR__}`.chomp.stringify }} + FIELDS = [ + "ip", + "city", + "region", + "region_code", + "country", + "country_name", + "continent_code", + "in_eu", + "postal", + "latitude", + "longitude", + "latlong", + "timezone", + "utc_offset", + "languages", + "country_calling_code", + "currency", + "asn", + "org", + ] + class Client API_URL = "https://ipapi.co/" @@ -33,6 +55,24 @@ module Ipapi parse_locate_response(response) end + {% for field in FIELDS %} + def {{field.id}}(ip_address : String) : String + url = "#{API_URL}#{ip_address}/{{field.id}}" + url = url + "?key=#{@api_key}" if @api_key + + response = HTTP::Client.get(url) + response.body + end + + def {{field.id}} : String + url = "#{API_URL}{{field.id}}" + url = url + "?key=#{@api_key}" if @api_key + + response = HTTP::Client.get(url) + response.body + end + {% end %} + private def parse_locate_response(response : HTTP::Client::Response) : Location? case response.status_code when 200