Getting Started With Ruby and NetDNA API
September 3, 2012 | Robert Gibb
The simplest way to interact with the NetDNA REST API using Ruby is by using the
netdnarws RubyGem. You can install this gem by typing the following command in your shell or command line:
gem install netdnarws
RubyGems is a package manager for Ruby that enables you to download, install, and manage “gems” in your system. And the
gem command is used to perform these tasks. You can verify if the
netdnarws gem is installed in your system by running the following command in your shell:
gem list --local
You should see
netndnarws in the list of gems in your system.
$ gem list --local *** LOCAL GEMS *** multi_json (1.3.6) multipart-post (1.1.5) netdnarws (0.2.1) polyglot (0.3.3) rack (1.4.1)
If you’re still using an older version of RubyGems you can also update it using the following command:
$ gem update --system #you may need to be administrator or root
Note: You only need to do the following steps if you don’t have RubyGems installed on your system. Otherwise, you can just skip this section.
gem command doesn’t work on your terminal or command line, it probably means that you don’t have RubyGems installed. You can get it from RubyGems.org, and install it as administrator.
Once you have downloaded the RubyGems software, you can unpack it to a directory and using a terminal, install it using the following command:
You may need to have administrator or root privilege for this command. To view more options in installing the software, you can type in the following command:
ruby setup.rb --help
ruby setup.rb command will install the RubyGems application files and the “gem” command in your system. If you don’t have administrator privileges and you cannot access the standard installation directory (such as /usr/local/lib/ruby), then you can install RubyGems in a different location.
In such a case, this would also imply that you also cannot install gems in the standard gem repository. You will have to specify a custom location for the gem repository using the
GEM_HOME environment variable.
You can use the following command in your command line to install RubyGems in a user directory:
$ export GEM_HOME=/home/mygemrepository #mygemrepository is a your custom directory where you have write permissions $ ruby setup.rb --prefix=/home/myaccount #myaccount is the user directory
1. The export command depends on the type of shell you use. You need to use the equivalent command depending on the operating system you use. For Windows users, it could be the following syntax:
2. You need to add
/home/myaccount/bin to your account’s path so the
gem can be invoked.
3. You will also need to add the
GEM_HOME setup to your profile for RubyGems to be located by your system
If you already have the
netdnarws gem properly installed, you can now create your own ruby client to interact with the NetDNA REST API. But first you need to register an application and get the consumer key, consumer secret, and
company alias from your account. You can get the company alias by visiting the API Console, and trying out the Get Account information API and get the value of the
Once you have all these values, you can just plug them to the following Ruby code:
api = NetDNARWS::NetDNA.new("myalias", "consumer_key", "consumer_secret")
And here’s a sample Ruby code that displays the JSON response from different NetDNA API calls:
#!/usr/bin/ruby require 'json' require 'netdnarws' api = NetDNARWS::NetDNA.new("jdorfman", "90739dhkhf84bf8786388648gd8738v37974", "23edfbdjbdjg3vrvuf4fvrvufyu47") puts "[/reports/stats.json]n" json_reportstats = api.get("/reports/stats.json") puts JSON.pretty_generate(json_reportstats) puts "n[/zones.json/summary]n" json_zones = api.get("/zones.json/summary") puts JSON.pretty_generate(json_zones) puts "n[/reports/popularfiles.json?date_from=2012-08-01&date_to=2012-08-31]n" json_popfiles = api.get("/reports/popularfiles.json?date_from=2012-08-01&date_to=2012-08-31") puts JSON.pretty_generate(json_popfiles) puts "n[/reports/statuscodes.json/daily]n" json_statuscodes = api.get("/reports/statuscodes.json/daily") puts JSON.pretty_generate(json_statuscodes)
netdnarws gem has support for GET, POST, PUT, and DELETE OAuth API requests. You can verify the required HTTP request for each method by visiting the API Console.