Skip to content

quandl/quandl-ruby

Repository files navigation

Quandl Ruby Client Build Status

The official ruby gem for all your data needs! The Quandl client can be used to interact with the latest version of the Quandl RESTful API.

Installation

gem 'quandl'

Configuration

Option Explanation Example
api_key Your access key tEsTkEy123456789
api_version The version you wish to access the api with 2015-04-09
require 'quandl'
Quandl::ApiConfig.api_key = 'tEsTkEy123456789'
Quandl::ApiConfig.api_version = '2015-04-09'

Retrieving Data

Dataset

Retrieving dataset data can be done in a similar way to Databases. For example to retrieve a dataset use its full code:

require 'quandl'
Quandl::Dataset.get('WIKI/AAPL')
=> ... dataset ...

You can also retrieve a list of datasets associated to a database by using the Database helper method:

Quandl::Database.get('WIKI').datasets
=> ... datasets results ...

By default, each list query will return page 1 of the first 100 results (Please see the API Documentation for more detail)

Data

Dataset data can be queried through a dataset. For example:

require 'quandl'
Quandl::Dataset.get('WIKI/AAPL').data
=> ... data ...

You can access the data much like you would other lists. In addition all the data column fields are mapped to their column_names for convenience:

Quandl::Dataset.get('WIKI/AAPL').data.first.date
=> ... date ...

Database

To retrieve a database simply use its code with the get parameter:

require 'quandl'
Quandl::Database.get('WIKI')
=> ... wiki database ...

You can also retrieve a list of databases by using:

Quandl::Database.all
=> ... results ...

Download Entire Database (Bulk Download)

To get the url for downloading all dataset data of a database:

require 'quandl'
Quandl::ApiConfig.api_key = 'tEsTkEy123456789'
Quandl::Database.get('ZEA').bulk_download_url
=> "https://www.quandl.com/api/v3/databases/ZEA/data?api_key=tEsTkEy123456789"

To bulk download all dataset data of a database:

Quandl::ApiConfig.api_key = 'tEsTkEy123456789'
Quandl::Database.get('ZEA').bulk_download_to_file('/path/to/destination/file_or_folder')

The file or folder path can either be specified as a string or as a File.

For bulk download of premium databases, please ensure that a valid api_key is set, as authentication is required.

For both bulk_download_url and bulk_download_to_file, an optional download_type query parameter can be passed in:

Quandl::Database.get('ZEA').bulk_download_to_file('.', params: {download_type: 'partial'})

If download_type is not specified, a complete bulk download will be performed. Please see the API Documentation for more detail.

Working with results

Instance

All data once retrieved is abstracted into custom classes. You can get a list of the fields in each class by using the data_fields method.

require 'quandl'
database = Quandl::Database.get('WIKI')
database.data_fields
=> ["id", "name", "database_code", "description", "datasets_count", "downloads", "premium", "image"]

You can then uses these methods in your code. Additionally you can access the data by using the hash equivalent lookup.

database = Quandl::Database.get('WIKI')
database.database_code
=> 'WIKI'
database['database_code']
=> 'WIKI'

In some cases name of the fields returned by the API may not be compatible with the ruby language syntax. These will be converted into compatible field names.

data = Quandl::Dataset.get('WIKI/AAPL').data(params: { limit: 1 }).first
data.column_names
=> ["Date", "Open", "High", "Low", "Close", "Volume", "Ex-Dividend", "Split Ratio", "Adj. Open", "Adj. High", "Adj. Low", "Adj. Close", "Adj. Volume"]
data.data_fields
=> ["date", "open", "high", "low", "close", "volume", "ex_dividend", "split_ratio", "adj_open", "adj_high", "adj_low", "adj_close", "adj_volume"]

List

Most list queries will return a paginated list of results. You can check whether the resulting list has more data by using the more_results? method. By default, each list query will return page 1 of the first 100 results (Please see the API Documentation for more detail). Depending on its results you can pass additional params to filter the data:

require 'quandl'
databases = Quandl::Database.all
=> ... results ...
databases.more_results?
=> true
Quandl::Database.all(params: { page: 2 })
=> ... more results ...

Lists also function as arrays and can be iterated through. Note however that using these features will only work on the current page of data you have locally. You will need to continue to fetch results and iterate again to loop through the full result set.

databases = Quandl::Database.all
databases.each { |d| puts d.database_code }
=> ... print database codes ...
databases.more_results?
=> true
Quandl::Database.all(params: { page: 2 }).each { |d| puts d.database_code }
=> ... print more database codes ...

Lists also return metadata associated with the request. This can include things like the current page, total results, etc. Each of these fields can be accessed through a hash or convenience method.

Quandl::Database.all.current_page
=> 1
Quandl::Database.all['current_page']
=> 1

As a convenience method lists can also return their data in CSV form. To do this simply issue the .to_csv method on a list:

databases = Quandl::Database.all.to_csv
=> "Id,Name,Database Code,Description,Datasets Count,Downloads,Premium,Image,Bundle Ids,Plan ...

Additional Links

License

MIT License