README.md in quandl-0.4.4 vs README.md in quandl-1.0.0.rc1

- old
+ new

@@ -1,369 +1,184 @@ -[![Code Climate](https://codeclimate.com/github/quandl/quandl_command.png)](https://codeclimate.com/github/quandl/quandl_command) +# Quandl Ruby Client -# Quandl Toolbelt +*Copyright Quandl 2015* -** The Quandl Toolbelt is currently in ALPHA TESTING. You are nevertheless welcome to try it. ** +The official gem for all your data needs! The Quandl client can be used to interact with the latest version of the [Quandl restful API](https://www.quandl.com/tools/api). Currently V3. -The Quandl toolbelt enables you to create and maintain time series data on Quandl.com. The Quandl toolbelt is extremly simple to understand and use. (We use it to maintain the 8 million datasets currently on the site.) +Note that v1 and v2 of the REST API are deprecated and we will be moving over all functionality to V3. During this transitionary period you can continue to use the old client here: https://rubygems.org/gems/quandl_client + ## Installation -### Mac +```ruby +gem 'quandl' +``` -**[quandl-toolbelt.pkg](http://s3.amazonaws.com/quandl-command/quandl-toolbelt.pkg)** +## Configuration +| Option | Explanation | Example | +|---|---|---| +| api_key | Your access key | `tEsTkEy123456789` | Used to identify who you are and provide more access. | +| api_version | The version you wish to access the api with | 2015-04-09 | Can be used to test your code against the latest version without committing to it. | -### Windows +```ruby +require 'quandl' +Quandl::ApiConfig.api_key = 'tEsTkEy123456789' +Quandl::ApiConfig.api_version = '2015-04-09' +``` -**[Quandl Setup.exe]( http://s3.amazonaws.com/quandl-command/Quandl+Setup.exe)** +## Retrieving Data +### Database -### Gemfile +To retrieve a database simply use its code with the get parameter: -In your Gemfile, add: - ```ruby -gem "quandl" +require 'quandl' +Quandl::Database.get('WIKI') +=> ... wiki database ... ``` +You can also retrieve a list of databases by using: +```ruby +require 'quandl' +Quandl::Database.all +=> ... results ... +``` +You can also search for specific databases by passing a query parameter such as: -## Login +```ruby +require 'quandl' +Quandl::Database.all(params: { query: 'oil' }) +=> ... oil results ... +``` -Once the toolbelt is installed, the next step is to login to Quandl: +### Dataset - $ quandl login - Obtain your token from the API tab on this page: http://www.quandl.com/users/info - Token: *************** - You have successfully authenticated! - username: tammer1 - email: tammer@quandl.com +Retrieving dataset data can be done in a similar way to Databases. For example to retrieve a dataset use its full code: -If you have a Quandl password, you can also use `quandl login --method password`. (You might not have a Quandl password if you log in using Github, Google, Linkedin or Twitter) +```ruby +require 'quandl' +Quandl::Dataset.get('WIKI/AAPL') +=> ... dataset ... +``` +You can also retrieve the dataset through the database by using the helper method. +```ruby +require 'quandl' +Quandl::Database.get('WIKI').datasets +=> ... datasets results ... +``` -## Create a Dataset +or to search for datasets with `AAPL` in them use: -Create [data.csv](https://raw2.github.com/quandl/toolbelt_help/master/data.csv) that looks something like this: +```ruby +require 'quandl' +Quandl::Database.get('WIKI').datasets(params: { query: 'apple' }) +=> ... datasets results for apple ... +``` - code: AAA - name: My first dataset - description: It only has two rows. - ----- - 1999-12-31, 1.23 - 2000-01-01, 4.56 +### Data -Now send it to Quandl: +Dataset data can be queried through a dataset. For example: - $ quandl upload data.csv +```ruby +require 'quandl' +Quandl::Dataset.get('WIKI/AAPL').data +=> ... data ... +``` -<!-- You just created a dataset on Quandl.com: [www.quandl.com/<your username>/AAA](#) --> +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: -You just created a dataset on Quandl.com: `www.quandl.com/<your-username>/AAA` +```ruby +require 'quandl' +Quandl::Dataset.get('WIKI/AAPL').data.first.date +=> ... date ... +``` +## 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. -## Update a Dataset +```ruby +require 'quandl' +database = Quandl::Database.get('WIKI') +database.data_fields +=> ["id", "name", "database_code", "description", "datasets_count", "downloads", "premium", "image"] +``` -The new dataset will now exist on Quandl forever. You can send new data and/or update metadata whenever you want. For example, create [data_update.csv](https://raw2.github.com/quandl/toolbelt_help/master/data_udpate.csv): +You can then uses these methods in your code. Additionally you can access the data by using the hash equalivalent lookup. - code: AAA - description: I am updating this description. - -- - 2000-01-02, 99.9 +```ruby +require 'quandl' +database = Quandl::Database.get('WIKI') +database.database_code +=> 'WIKI' +database['database_code'] +=> 'WIKI' +``` -Now send to Quandl: +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. - $ quandl upload data_update.csv +```ruby +require 'quandl' +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"] +``` -Notice that the dataset now has three rows and a new description: -<!-- [www.quandl.com/<your user name>/AAA](#) --> -`www.quandl.com/<your-username>/AAA` +### List -<!-- -## An Alternate Update Method -You might also want to edit it more at [www.quandl.com/edit/<your user name>/FOO](#) ---> +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. Depending on its results you can pass additional params to filter the data: +```ruby +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. +```ruby +require 'quandl' +databases = Quandl::Database.all.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 ... +``` -## Delete a Dataset +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. -You can delete the dataset: +```ruby +require 'quandl' +Quandl::Database.all.current_page +=> 1 +Quandl::Database.all['current_page'] +=> 1 +``` - $ quandl delete AAA - source_code … created_at: '2014-01-21T15:53:22Z' - Are you sure? (y/n) - y - OK 1241ms AAA +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: +```ruby +require 'quandl' +databases = Quandl::Database.all.to_csv +=> "Id,Name,Database Code,Description,Datasets Count,Downloads,Premium,Image,Bundle Ids,Plan ... +``` +## Additional Links - -## Scrapers and Other Data Producing Programs - -As long as your program outputs Quandl flavored CSV as above, it is ready for use with the Quandl toolbelt. Consider this scraper, written in both Ruby ([scraper1.rb](https://raw2.github.com/quandl/toolbelt_help/master/scraper1.rb)) and Python ([scraper1.py](https://raw2.github.com/quandl/toolbelt_help/master/scraper1.py)): - - # This is script that pulls the history of Facebook (FB) stock price from Google. - # It then prints the data to the screen in CSV format. - # It prepends the CSV with Quandl metadata (code, name, description) - # - … - - -If I were to run this script on Jan 31, 2014 I would get this: - - $ ruby scraper.rb - code: FB - name: Facebook Stock Price - ---- - Date,Open,High,Low,Close,Volume - 30-Jan-14,62.12,62.50,60.46,61.08,150438699 - 29-Jan-14,54.61,54.95,53.19,53.53,98089932 - ... - -I can turn the output of this script into a Quandl dataset like this: - - $ ruby scraper.rb | quandl upload - -or - - $ python scraper.py | quandl upload - -If you download the script and run the above command, you would see the result here: -<!-- [www.quandl.com/YOU/FB](#) --> -`www.quandl.com/<your-username>/FB` - -You can pipe the script to `quandl upload` each day to keep the dataset up to date on Quandl.com. Everytime you send data to an existing dataset the new data is merged with what is already there. (Hence the Quandl toolbelt is ideal for daily data harvesting or loading entire datasets or some combination of the two.) - -<!-- -Now I send my script to Quandl for regular execution: - - $ quandl schedule scraper.rb daily 17:30 - -The scraper will be run daily at 17:30 (in your time zone). Every day when it runs, if the script's output is not valid OR the script reports something to STDERR, then you receive an alert email. - -You now have a dataset on Quandl that refreshes daily! ---> - - - - -## Scheduling Your Scripts - -This feature is not ready for use yet. When it is ready you will be able to send any script to Quandl. Quandl will then run the script on a schedule and send the output to `quandl upload` for you. You can (optionally) receive emails when the script succeeds or fails. - - - - -## Many Datasets via One Input Stream - -You can send multiple datasets with a single call to `quandl upload`. [Scraper2.rb](https://raw2.github.com/quandl/toolbelt_help/master/scraper2.rb) ([Scraper2.py](https://raw2.github.com/quandl/toolbelt_help/master/scraper2.py)) produces the most recent closing data for two stocks: - - code: AAPL - -- - 2000-01-15,88.32,... - - code: MSFT - -- - 2000-01-15,44.20,... - -Then - - $ python scraper.py | quandl upload - -Creates or updates both `quandl.com/<your-username>/AAPL` and `quandl.com/<your-username>/MSFT` - -You can send an infinite number of datasets via one call to `quandl upload`. - - - - -## Quandl Flavored CSV - -Quandl "flavored" CSV is just just plain vanilla CSV prepended with metadata in [YAML](http://en.wikipedia.org/wiki/YAML) format. Metadata is seperated from data by a single line containing one or more dashes "-". - - -### Quick Reference - -Here is the entire specification by example for quick reference: - - # This is a comment. Also note blank lines are allowed; they are simply ignored - - code: A01 # only (uppercase) letters, numbers and "_" can be used - - name: My Dataset - - description: "This data is my dataset. Note the use of quotes so - that I can use two lines and also use the reserved character ':'" - - reference_url: www.wsj.com/somewhere # any valid url - - frequency: daily # frequency is inferred if you omit this field - - private: true # true => only you can see the dataset on Quandl - - ---- - - Date,Price,Volume # if omitted on new dataset, default headings are created - 2012-01-01,32.23 # the csv data. date can be almost any format you want - - -### Metadata Specifications - -|Field|Description|Required?| -|-----|------------|--------| -|code|a unique id for the dataset; uppercase letters, numbers and "_" are the only characters allowed.|Required| -|name|a name for the dataset|Strongly Recomended| -|description|a description for the dataset|Recomended| -|reference_url|An external URL where the data can be validated. Most datasets on Quandl cite an external source to maximize credability|Optional| -|frequency|daily, weekly, monthly,quarterly or annual|optional; inferred if omitted| -|private|true or false; default is false|private data is visible to only you| - - - - -## Example Scrapers - -### Shibor - -[www.shibor.org](http://www.shibor.org) publishes Shibor rates which Quandl republishes at [www.quandl.com/TAMMER1/SHIBOR](http://www.quandl.com/TAMMER1/SHIBOR) - -This dataset is maintained via [this ruby script](https://github.com/tammer/scrapers/blob/master/shibor.rb) that fetches the 10 most recent days of data from Shibor.org. - -You can run the script to print 10 days of Shibor rates to the screen: - - curl "https://raw.github.com/tammer/scrapers/master/shibor.rb" | ruby - -To maintain this dataset on Quandl, we simply run the following on a daily basis: - - curl "https://raw.github.com/tammer/scrapers/master/shibor.rb" | ruby | quandl upload - -Each day 10 rows are sent to Quandl. Usually 9 of those rows are redundant, but that is harmless since we replace existing data with exactly the same data. Notice how old data is not affected by the updates. - -The backfill for this dataset was manually downloaded and converted into a simple CSV file which we then pushed to the site: - - quandl upload shibor_backfill.csv - - -### Hsieh Trend Following Factors - -Professor David Hsieh maintains hedge fund trend following risk factors at [faculty.fuqua.duke.edu/~dah7/HFRFData.htm](https://faculty.fuqua.duke.edu/~dah7/HFRFData.htm). They are available on Quandl at [quandl.com/TAMMER1/TFRF](http://www.quandl.com/TAMMER1/TFRF). - -The data is maintained by running [hsieh.rb](https://github.com/tammer/scrapers/blob/master/hsieh.rb) every day. To see the output of the script: - - curl "https://raw.github.com/tammer/scrapers/master/hsieh.rb" | ruby - -To keep the data up to date, we scheduled a daily run of: - - curl "https://raw.github.com/tammer/scrapers/master/hsieh.rb" | ruby | quandl upload - - -### Copyright Data - -Some data publishers provide data on the condition that you not republish it. When scraping such sites, be sure to set the private flag to be true so that only you can see the data, at which point you should be in compliance, since you are simply storing a single copy on a private cloud based repository; (no different from storing a copy on Google Docs or Dropbox). - -For example, if you happen to need the MSCI Far East Index on Quandl, you can scrape it with a program like [this](https://github.com/tammer/scrapers/blob/master/msci.rb). You then pipe to Quandl as usual, ensuring the private flag is true: - - curl "https://raw.github.com/tammer/scrapers/master/msci.rb" | ruby | quandl upload - -Now you have the data you need on Quandl while remaining compliant with MSCI's terms of use. - - -### Additional Examples - -|Dataset|Scraper| -|-------|----| -| [Litecoin vs USD](http://quandl.com/TAMMER1/LTCUSD)| [litecoin.rb](https://github.com/tammer/scrapers/blob/master/litecoin.rb)| - - - - -## Full Reference - -Other features of the Toolbelt including `quandl download`, `quandl info`, `quandl list` and other minor features are documented in the [Quandl Toolbelt Reference](#) page. - - - - -## FAQ - -### How can I use ":" in the name or description field? - -You should put the text in double quotes: - - code: FOO - name: My Dataset - description: "I love colons : : :" - -From Ruby: - - puts "description: \"I love colons : : :\" " - -or - - puts ' description: "I love colons : : :" ' - -From Python: - - print "description: \"I love colons : : :\"" - - -### Are the Datasets Publicly Accessible? - -You decide. By default it is public. use: - - private: true - -To make the dataset visible only to you. - - -### Can you handle high frequency (intra-day) data? - -No. - - -### How do I including Blank or Nils - -This is how you include nil datums: - - Code: NIL - Name: Example Data with Missing Points - Description: This dataset is for example only. - -- - Date, High, Low, Mid - 2005, 1, 2, 3 - 2004, 5, nil, 4 - 2003, ,,9 - 2002, 1, 2, N.a. - -This dataset can be seen on Quandl right [here](http://www.quandl.com/TAMMER1/NIL) - - -### Your SHIBOR script seems to download the past 10 days' worth of data... - -...Assuming that happens daily, then you'll have overlapping data (e.g., the most recent day's data is new, but the prior nine days worth of data should be in the database already). How does Quandl deal with that? What if the underlying data changes - will Quandl update the previous nine days of data? Will it record what the data used to be based on the 'original' dataset? - -Answer: If you upload data for dates where data already exists, the new data over-writes the old data. Thus if you send redundant data, it is harmless. Shibor.rb is written this way for two reason: 1) helpful in case the publisher changes something a few days later. 2) helpful if we miss run for a couple of days for some reason. - - -### A given municipal bond doesn't trade every day... - -So, if I set up a separate 'id' for each bond, then each day there will be some bonds that get pricing updates and others that don't. Are there any issues with this, or can Quandl handle this kind of 'sparse' data? - -Answer: Sparse data is not a problem. - - -### Why can't I find my dataset using search on Quandl. - -If it is private, it will not appear in search ever. If it is public, it can take up to 1 hour before our index is updated with your new dataset. - - -### My Question is not answered! - -You best <a href='mailto:connect@quandl.com'>email</a> me then. Put "Toolbelt" in the subject and you go right to the top of my inbox. +* [Quandl](https://www.quandl.com) +* [Quandl Tools](https://www.quandl.com/tools/api)