Skip to content

Commit

Permalink
make ip_address optional
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 5, 2024
1 parent 5d4bf88 commit 24ed2c9
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions src/ipapi.cr
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,10 @@ module Ipapi
def initialize(@api_key : String? = nil)
end

# Location of a specific IP
def locate(ip_address : String) : Location?
url = "#{API_URL}#{ip_address}/json"
url = url + "?key=#{@api_key}" if @api_key

response = HTTP::Client.get(url)

parse_locate_response(response)
end

# Location of client’s IP
def locate : Location?
url = "#{API_URL}json"
# Retrive the location of a specific IP address.
# If `ip_address` is `nil`, use the client's IP.
def locate(ip_address : String? = nil) : Location?
url = Path.new([API_URL, ip_address, "json"].compact).to_s
url = url + "?key=#{@api_key}" if @api_key

response = HTTP::Client.get(url)
Expand All @@ -56,19 +47,11 @@ module Ipapi
end

{% for field, description in FIELDS %}
# Retrive {{description.id}} for an IP address
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)

parse_field_response(response)
end
# Retrive {{description.id}} of a specific IP address.
# If `ip_address` is `nil`, use the client's IP.
def {{field.id}}(ip_address : String? = nil) : String
url = Path.new([API_URL, ip_address, "{{field.id}}"].compact).to_s

# Retrive {{description.id}}
def {{field.id}} : String
url = "#{API_URL}{{field.id}}"
url = url + "?key=#{@api_key}" if @api_key

response = HTTP::Client.get(url)
Expand Down

0 comments on commit 24ed2c9

Please sign in to comment.