Skip to content

Commit

Permalink
Specific location field
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 5, 2024
1 parent 2435d20 commit a363da8
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/ipapi.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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/"

Expand All @@ -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
Expand Down

0 comments on commit a363da8

Please sign in to comment.