Ruby Code Example
This Ruby code performs the following steps:
- Sets the FraudGuard.io API credentials (username and password).
- Defines the IP address to query.
- Constructs the API endpoint URL.
- Creates a URI object with the API URL.
- Creates an HTTP object and enables SSL for secure communication.
- Creates a GET request object.
- Sets the basic authentication header using Base64 encoding.
- Sends the HTTP request to the API endpoint.
- Checks the response code and prints the response body if the request was successful.
require 'net/http'
require 'base64'
# FraudGuard.io API credentials
username = 'username'
password = 'password'
# IP address to query
ip_address = '8.8.8.8'
# API endpoint
api_url = "https://api.fraudguard.io/ip/#{ip_address}"
# Create URI object
uri = URI(api_url)
# Create HTTP object
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# Create request
request = Net::HTTP::Get.new(uri)
# Set basic authentication header
auth_string = "#{username}:#{password}"
encoded_auth_string = Base64.strict_encode64(auth_string)
request['Authorization'] = "Basic #{encoded_auth_string}"
# Send request
response = http.request(request)
# Check response
if response.code == '200'
puts response.body
else
puts "Error: #{response.code} - #{response.message}"
end
Make sure to replace ‘username’ and ‘password’ with your actual API credentials.