Created
March 26, 2012 23:07
-
-
Save rdegges/2210463 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ask the user for a phone number to lookup caller ID information.¬ | |
¬ | |
¬ | |
var prompt = require('prompt');¬ | |
var request = require('request');¬ | |
¬ | |
¬ | |
// Our input prompt properties.¬ | |
var properties = [{¬ | |
message: 'Enter the phone number to look up'.blue,¬ | |
name: 'phone_number',¬ | |
empty: false,¬ | |
}];¬ | |
¬ | |
¬ | |
// Get the phone number to query via the CLI.¬ | |
prompt.start();¬ | |
prompt.get(properties, function (err, result) {¬ | |
var phone_number;¬ | |
¬ | |
// Remove all non-integers from the phone number. Also: only look at the last¬ | |
// 10 numbers in the string.¬ | |
phone_number = result.phone_number.replace(/[^\d.]/g, '');¬ | |
phone_number = phone_number.substr(phone_number.length - 10);¬ | |
¬ | |
// If the phone number is short, error out.¬ | |
if (phone_number.length < 10) {¬ | |
console.log('ERROR: phone number must be 10 digts.'.red);¬ | |
return;¬ | |
};¬ | |
¬ | |
// Let the user know we're looking up their number.¬ | |
console.log('Looking up caller ID name for ' + phone_number.blue + '... ');¬ | |
¬ | |
// Now that we have our user input, query the phone number and get the caller¬ | |
// ID name information.¬ | |
request('https://api.opencnam.com/v1/phone/' + phone_number + '?format=text', function (error, response, body) {¬ | |
if (!error && response.statusCode == 200) {¬ | |
console.log('Found: ' + body.green);¬ | |
} else {¬ | |
console.log('Not found!'.red);¬ | |
};¬ | |
});¬ | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment