This guide describes how to use Nightfall with the Ruby programming language.
The example below will demonstrate how to use Nightfall’s text scanning functionality to verify whether a string contains sensitive PII using the Nightfall Python SDK.
To follow along, you will need:
A Nightfall API Key
An existing Detection Rule
Data to scan. Note that the API interprets data as plaintext, so you may pass it in any structured or unstructured format.
A local Ruby 2.6 or greater environment.
Start by creating a new file called nightfall_demo.rb
Now we will walk through the code step by step. If you'd like to skip ahead you can see the complete code sample at the bottom of this page.
We will be using a few built-in Ruby libraries to run this sample API script.
First, we will load some environment variables that will be used to interact with the Nightfall API. NIGHTFALL_API_KEY should be your Nightfall API Key, and NIGHTFALL_DETECTION_RULE_UUID should be the UUID for your existing Nightfall condition set.
# Load environment variables for Nightfall APInightfall_api_key =ENV['NIGHTFALL_API_KEY']detection_rule_uuid =ENV['NIGHTFALL_DETECTION_RULE_UUID']
Next, we will construct our payload to scan as an array. You can replace this with any data you'd like, or read plaintext from a file.
# Text data to scanpayload = ["The customer social security number is 458-02-6124","No PII in this string","My credit card number is 4916-6734-7572-5015"]
Next, we build the HTTP request headers and body using the environment variables that we previously defined.
Lastly, we make the API request and process the response from Nightfall. If there are sensitive findings in the response we pretty-print them to the console. If there are no findings, we print a message to the console. Otherwise, if there is a problem with the HTTP request we print the status code and message to the console.
# Make API requestresponse = http.request(request)# Parse responseif response.code.to_i ==200and response.body['findings']puts"This text contains sensitive data.\n\n"putsJSON.pretty_generate(JSON.parse(response.body))elsif response.code.to_i ==200puts"No sensitive data found. Hooray!"elseputs"Something went wrong -- Response #{response.code}."end
Usage
Now we can run our script:
rubynightfall_demo.rb
If there are sensitive findings based on your Nightfall detection rule, you should see output similar to this in your console, corresponding to each of the 3 items inputted to scan in the payload.
For your convenience, the complete Ruby code sample is shown below.
# nightfall_demo.rb# Load dependenciesrequire'open-uri'require'net/http'require'json'# Load environment variables for Nightfall APInightfall_api_key =ENV['NIGHTFALL_API_KEY']detection_rule_uuid =ENV['NIGHTFALL_DETECTION_RULE_UUID']# Text data to scanpayload = ["The customer social security number is 458-02-6124","No PII in this string","My credit card number is 4916-6734-7572-5015"]# Configure detection settingsconfig = { "config": {"detectionRuleUUIDs": [detection_rule_uuid] },"payload": payload}# Build API requesturl =URI("https://api.nightfall.ai/v3/scan")http =Net::HTTP.new(url.host, url.port)http.use_ssl =truerequest =Net::HTTP::Post.new(url)request["Accept"] ='application/json'request["Content-Type"] ='application/json'request["Authorization"] ="Bearer #{nightfall_api_key}"request.body = config.to_json# Make API requestresponse = http.request(request)# Parse responseif response.code.to_i ==200and response.body['findings']puts"This text contains sensitive data.\n\n"putsJSON.pretty_generate(JSON.parse(response.body))elsif response.code.to_i ==200puts"No sensitive data found. Hooray!"elseputs"Something went wrong -- Response #{response.code}."end