GithubHelp home page GithubHelp logo

dannyben / nasdaq Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 902 KB

Nasdaq Data Link API Ruby Library and Command Line

License: MIT License

Ruby 93.67% AutoHotkey 6.33%
quandl-api quandl ruby api api-client command-line economic-data gem

nasdaq's Introduction

Nasdaq Data Link API and Command Line

Gem Version Build Status Maintainability


Lightweight ruby library and command line interface for accessing the Nasdaq Data Link API (formerly Quandl) with direct access to all of its endpoints.

This gem is not affiliated with Nasdaq or with Quandl.


Install

$ gem install nasdaq

Or with bundler:

gem 'nasdaq'

Features

  • Easy to use interface.
  • Use as a library or through the command line.
  • Access any of the API endpoints directly.
  • Display output in various formats.
  • Save output to a file.
  • Includes a built in file cache (disabled by default).

Usage

First, require and initialize with your-api-key

require 'nasdaq'
nasdaq = Nasdaq::API.new 'your-api-key'

Now, you can access any API endpoint with any optional parameter, like this:

result = nasdaq.get "datasets/WIKI/AAPL", rows: 3 # => Hash

In addition, for convenience, you can use the first part of the endpoint as a method name, like this:

result = nasdaq.datasets "WIKI/AAPL", rows: 3

In other words, these calls are the same:

nasdaq.get 'endpoint', param: value
nasdaq.endpoint, param: value

as well as these two:

nasdaq.get 'endpoint/sub', param: value
nasdaq.endpoint 'sub', param: value

By default, you will get a ruby hash in return. If you wish to have more control over the response, use the get! method instead:

result = nasdaq.get! "datasets/WIKI/AAPL", rows: 3

# Request Object
p result.request.class
# => HTTParty::Request

# Response Object
p result.response.class
# => Net::HTTPOK

p result.response.body
# => JSON string

p result.response.code
# => 200

p result.response.msg
# => OK

# Headers Object
p result.headers
# => Hash with headers

# Parsed Response Object
p result.parsed_response
# => Hash with HTTParty parsed response 
#    (this is the content returned with #get)

You can get the response as CSV by calling get_csv:

result = nasdaq.get_csv "datasets/WIKI/AAPL", rows: 3
# => CSV string

To save the output directly to a file, use the save method:

nasdaq.save "filename.json", "datasets/WIKI/AAPL", rows: 3

Or, to save CSV, use the save_csv method:

nasdaq.save_csv "filename.csv", "datasets/WIKI/AAPL", rows: 3

Command Line

The command line utility nasdaq acts in a similar way. To use your-api-key, simply set it in the environment variables NASDAQ_KEY:

$ export NASDAQ_KEY=your_key

These commands are available:

$ nasdaq get PATH [PARAMS...]        # print the output.  
$ nasdaq pretty PATH [PARAMS...]     # print a pretty JSON.  
$ nasdaq see PATH [PARAMS...]        # print a colored output.  
$ nasdaq url PATH [PARAMS...]        # show the constructed URL.  
$ nasdaq save FILE PATH [PARAMS...]  # save the output to a file.  

Run nasdaq --help for more information, or view the full usage help.

Examples:

# Shows the first two databases 
$ nasdaq see databases per_page:2

# Or more compactly, as CSV
$ nasdaq get databases per_page:2

# Prints CSV to screen (CSV is the default in the command line)
$ nasdaq get datasets/WIKI/AAPL

# Prints JSON instead
$ nasdaq get datasets/WIKI/AAPL.json

# Pass arguments using the same syntax - key:value
$ nasdaq get datasets/WIKI/AAPL rows:5

# Pass arguments that require spaces
$ nasdaq get datasets.json "query:qqq index"

# Prints a colored output
$ nasdaq see datasets/WIKI/AAPL rows:5

# Saves a file
$ nasdaq save output.csv datasets/WIKI/AAPL rows:5

# Shows the underlying URL for the request, good for debugging
$ nasdaq url datasets/WIKI/AAPL rows:5
# => https://data.nasdaq.com/api/v3/datasets/WIKI/AAPL.csv?api_key=YOUR_KEY&rows=5

Caching

The Nasdaq library uses the Lightly gem for automatic HTTP caching. To take the path of least surprises, caching is disabled by default.

You can enable and customize it by either passing options on initialization, or by accessing the Lightly object directly at a later stage.

nasdaq = Nasdaq::API.new 'your-api-key', use_cache: true
nasdaq = Nasdaq::API.new 'your-api-key', use_cache: true, cache_dir: 'tmp'
nasdaq = Nasdaq::API.new 'your-api-key', use_cache: true, cache_life: 120

# or 

nasdaq = Nasdaq::API.new 'your-api-key'
nasdaq.cache.enable
nasdaq.cache.dir = 'tmp/cache'   # Change cache folder
nasdaq.cache.life = 120          # Change cache life to 2 minutes

To enable caching for the command line, simply set one or both of these environment variables:

$ export NASDAQ_CACHE_DIR=cache   # default: 'cache'
$ export NASDAQ_CACHE_LIFE=120    # default: 3600 (1 hour)
$ nasdaq get datasets/WIKI/AAPL
# => This call will be cached

Command Line Demo

Demo

nasdaq's People

Contributors

dannyben avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nasdaq's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.