Class: IpwhoApi::Client
- Inherits:
-
Object
- Object
- IpwhoApi::Client
- Defined in:
- lib/ipwho.rb
Overview
── Client ──────────────────────────────────────────────────────────
Constant Summary collapse
- DEFAULT_BASE_URL =
"https://api.ipwho.org".freeze
Instance Method Summary collapse
-
#bulk(ips) ⇒ IpwhoApi::IpGeoResponse
Batch-lookup multiple IP addresses.
-
#initialize(api_key, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#lookup(ip, options = {}) ⇒ IpwhoApi::IpGeoResponse
Look up geolocation for a specific IPv4 or IPv6 address.
-
#me(options = {}) ⇒ IpwhoApi::IpGeoResponse
Look up geolocation for the caller's own IP address.
Constructor Details
#initialize(api_key, options = {}) ⇒ Client
Returns a new instance of Client.
216 217 218 219 220 221 222 |
# File 'lib/ipwho.rb', line 216 def initialize(api_key, = {}) raise APIResponseError, "api_key is required" if api_key.nil? || api_key.empty? @api_key = api_key @base_url = ([:base_url] || DEFAULT_BASE_URL).sub(%r{/+$}, "") @timeout = [:timeout] || 30 end |
Instance Method Details
#bulk(ips) ⇒ IpwhoApi::IpGeoResponse
Batch-lookup multiple IP addresses.
260 261 262 263 264 265 266 |
# File 'lib/ipwho.rb', line 260 def bulk(ips) raise APIResponseError, "ips must not be empty" if ips.nil? || ips.empty? bulk_param = ips.join(",") params = { apiKey: @api_key } get("/bulk/#{ERB::Util.url_encode(bulk_param)}", params, "json") end |
#lookup(ip, options = {}) ⇒ IpwhoApi::IpGeoResponse
Look up geolocation for a specific IPv4 or IPv6 address.
236 237 238 239 240 |
# File 'lib/ipwho.rb', line 236 def lookup(ip, = {}) format = [:format] || "json" params = build_params(format, [:fields]) get("/ip/#{ERB::Util.url_encode(ip)}", params, format) end |
#me(options = {}) ⇒ IpwhoApi::IpGeoResponse
Look up geolocation for the caller's own IP address.
249 250 251 252 253 |
# File 'lib/ipwho.rb', line 249 def me( = {}) format = [:format] || "json" params = build_params(format, [:fields]) get("/me", params, format) end |