nasdaq 0.3.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aed342089c070709ba3e3ae42d467da76f67428d3323d70c04c5a2c26131be7d
4
+ data.tar.gz: c33341051dc44b7351afbe394361c94a8b8d7fa193c1353e77ffff7e5a076de2
5
+ SHA512:
6
+ metadata.gz: 4f281cf8f6c899edff90e6ed4f78ff0dda02f0c6b00c41204ffd7db1c2e1388dfc600a30628b69f2c8cabe08e0b885e38d5cebc61cbaa48a649cdbea8de0dac9
7
+ data.tar.gz: d4d1d7912ef5f5eeca413b26f37eaac3f1b579b772b3053c294c60c4ca20c602ba1376f22de9e2af88fdd3e324c9a3c7cae25836fbeecada41c0e675978e5780
data/README.md ADDED
@@ -0,0 +1,222 @@
1
+ # Nasdaq Data Link API and Command Line
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/nasdaq.svg)](https://badge.fury.io/rb/nasdaq)
4
+ [![Build Status](https://github.com/DannyBen/nasdaq/workflows/Test/badge.svg)](https://github.com/DannyBen/nasdaq/actions?query=workflow%3ATest)
5
+
6
+ ---
7
+
8
+ Lightweight ruby library and command line interface for accessing the
9
+ [Nasdaq Data Link API][1] (formerly Quandl) with direct access to all of its
10
+ endpoints.
11
+
12
+ **This gem is not affiliated with Nasdaq or with Quandl.**
13
+
14
+ ---
15
+
16
+ ## Install
17
+
18
+ ```
19
+ $ gem install nasdaq
20
+ ```
21
+
22
+ Or with bundler:
23
+
24
+ ```ruby
25
+ gem 'nasdaq'
26
+ ```
27
+
28
+
29
+ ## Features
30
+
31
+ * Easy to use interface.
32
+ * Use as a library or through the command line.
33
+ * Access any of the API endpoints directly.
34
+ * Display output in various formats.
35
+ * Save output to a file.
36
+ * Includes a built in file cache (disabled by default).
37
+
38
+
39
+ ## Usage
40
+
41
+ First, require and initialize with your-api-key
42
+
43
+ ```ruby
44
+ require 'nasdaq'
45
+ nasdaq = Nasdaq::API.new 'your-api-key'
46
+ ```
47
+
48
+ Now, you can access any API endpoint with any optional parameter, like
49
+ this:
50
+
51
+ ```ruby
52
+ result = nasdaq.get "datasets/WIKI/AAPL", rows: 3 # => Hash
53
+ ```
54
+
55
+ In addition, for convenience, you can use the first part of the endpoint as
56
+ a method name, like this:
57
+
58
+ ```ruby
59
+ result = nasdaq.datasets "WIKI/AAPL", rows: 3
60
+ ```
61
+
62
+ In other words, these calls are the same:
63
+
64
+ ```ruby
65
+ nasdaq.get 'endpoint', param: value
66
+ nasdaq.endpoint, param: value
67
+ ```
68
+
69
+ as well as these two:
70
+
71
+ ```ruby
72
+ nasdaq.get 'endpoint/sub', param: value
73
+ nasdaq.endpoint 'sub', param: value
74
+ ```
75
+
76
+ By default, you will get a ruby hash in return. If you wish to have more
77
+ control over the response, use the `get!` method instead:
78
+
79
+ ```ruby
80
+ result = nasdaq.get! "datasets/WIKI/AAPL", rows: 3
81
+
82
+ # Request Object
83
+ p payload.request.class
84
+ # => HTTParty::Request
85
+
86
+ # Response Object
87
+ p payload.response.class
88
+ # => Net::HTTPOK
89
+
90
+ p payload.response.body
91
+ # => JSON string
92
+
93
+ p payload.response.code
94
+ # => 200
95
+
96
+ p payload.response.msg
97
+ # => OK
98
+
99
+ # Headers Object
100
+ p payload.headers
101
+ # => Hash with headers
102
+
103
+ # Parsed Response Object
104
+ p payload.parsed_response
105
+ # => Hash with HTTParty parsed response
106
+ # (this is the content returned with #get)
107
+ ```
108
+
109
+ You can get the response as CSV by calling `get_csv`:
110
+
111
+ ```ruby
112
+ result = nasdaq.get_csv "datasets/WIKI/AAPL", rows: 3
113
+ # => CSV string
114
+ ```
115
+
116
+ To save the output directly to a file, use the `save` method:
117
+
118
+ ```ruby
119
+ nasdaq.save "filename.json", "datasets/WIKI/AAPL", rows: 3
120
+ ```
121
+
122
+ Or, to save CSV, use the `save_csv` method:
123
+
124
+ ```ruby
125
+ nasdaq.save_csv "filename.csv", "datasets/WIKI/AAPL", rows: 3
126
+ ```
127
+
128
+
129
+ ## Command Line
130
+
131
+ The command line utility `nasdaq` acts in a similar way. To use your-api-key,
132
+ simply set it in the environment variables `NASDAQ_KEY`:
133
+
134
+ ```
135
+ $ export NASDAQ_KEY=your_key
136
+ ```
137
+
138
+ These commands are available:
139
+
140
+ ```bash
141
+ $ nasdaq get PATH [PARAMS...] # print the output.
142
+ $ nasdaq pretty PATH [PARAMS...] # print a pretty JSON.
143
+ $ nasdaq see PATH [PARAMS...] # print a colored output.
144
+ $ nasdaq url PATH [PARAMS...] # show the constructed URL.
145
+ $ nasdaq save FILE PATH [PARAMS...] # save the output to a file.
146
+ ```
147
+
148
+ Run `nasdaq --help` for more information, or view the [full usage help][2].
149
+
150
+ Examples:
151
+
152
+ ```bash
153
+ # Shows the first two databases
154
+ $ nasdaq see databases per_page:2
155
+
156
+ # Or more compactly, as CSV
157
+ $ nasdaq get databases per_page:2
158
+
159
+ # Prints CSV to screen (CSV is the default in the command line)
160
+ $ nasdaq get datasets/WIKI/AAPL
161
+
162
+ # Prints JSON instead
163
+ $ nasdaq get datasets/WIKI/AAPL.json
164
+
165
+ # Pass arguments using the same syntax - key:value
166
+ $ nasdaq get datasets/WIKI/AAPL rows:5
167
+
168
+ # Pass arguments that require spaces
169
+ $ nasdaq get datasets.json "query:qqq index"
170
+
171
+ # Prints a colored output
172
+ $ nasdaq see datasets/WIKI/AAPL rows:5
173
+
174
+ # Saves a file
175
+ $ nasdaq save output.csv datasets/WIKI/AAPL rows:5
176
+
177
+ # Shows the underlying URL for the request, good for debugging
178
+ $ nasdaq url datasets/WIKI/AAPL rows:5
179
+ # => https://data.nasdaq.com/api/v3/datasets/WIKI/AAPL.csv?api_key=YOUR_KEY&rows=5
180
+ ```
181
+
182
+ ## Caching
183
+
184
+ The Nasdaq library uses the [Lightly][3] gem for automatic HTTP caching.
185
+ To take the path of least surprises, caching is disabled by default.
186
+
187
+ You can enable and customize it by either passing options on
188
+ initialization, or by accessing the `Lightly` object directly at
189
+ a later stage.
190
+
191
+ ```ruby
192
+ nasdaq = Nasdaq::API.new 'your-api-key', use_cache: true
193
+ nasdaq = Nasdaq::API.new 'your-api-key', use_cache: true, cache_dir: 'tmp'
194
+ nasdaq = Nasdaq::API.new 'your-api-key', use_cache: true, cache_life: 120
195
+
196
+ # or
197
+
198
+ nasdaq = Nasdaq::API.new 'your-api-key'
199
+ nasdaq.cache.enable
200
+ nasdaq.cache.dir = 'tmp/cache' # Change cache folder
201
+ nasdaq.cache.life = 120 # Change cache life to 2 minutes
202
+ ```
203
+
204
+ To enable caching for the command line, simply set one or both of
205
+ these environment variables:
206
+
207
+ ```bash
208
+ $ export NASDAQ_CACHE_DIR=cache # default: 'cache'
209
+ $ export NASDAQ_CACHE_LIFE=120 # default: 3600 (1 hour)
210
+ $ nasdaq get datasets/WIKI/AAPL
211
+ # => This call will be cached
212
+ ```
213
+
214
+
215
+ ## Command Line Demo
216
+
217
+ ![Demo](https://raw.githubusercontent.com/DannyBen/nasdaq/master/demo.gif "Demo")
218
+
219
+ [1]: https://docs.data.nasdaq.com/docs/getting-started
220
+ [2]: https://github.com/DannyBen/nasdaq/blob/master/lib/nasdaq/docopt.txt
221
+ [3]: https://github.com/DannyBen/lightly
222
+
data/bin/nasdaq ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'nasdaq'
3
+
4
+ begin
5
+ Nasdaq::CommandLine.execute ARGV
6
+ rescue Nasdaq::BadResponse => e
7
+ warn "#{e.class} - #{e.message}"
8
+ end
data/lib/nasdaq/api.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'apicake'
2
+
3
+ module Nasdaq
4
+ # Provides access to all the Quandl API endpoints
5
+ class API < APICake::Base
6
+ base_uri 'https://data.nasdaq.com/api/v3'
7
+
8
+ attr_reader :api_key
9
+
10
+ # TODO: handle format
11
+
12
+ # Initializes the API with an optional API Key, and cache settings.
13
+ def initialize(api_key = nil, opts = {})
14
+ if api_key.is_a?(Hash) && opts.empty?
15
+ opts = api_key
16
+ api_key = nil
17
+ end
18
+ @api_key = api_key
19
+ cache.disable unless opts[:use_cache]
20
+ cache.dir = opts[:cache_dir] if opts[:cache_dir]
21
+ cache.life = opts[:cache_life] if opts[:cache_life]
22
+ end
23
+
24
+ # Returns a hash that will be merged into all query strings before
25
+ # sending the request to Quandl. This method is used by API Cake.
26
+ def default_query
27
+ { api_key: api_key }
28
+ end
29
+
30
+ # Forwards all arguments to #get! and converts the JSON response to CSV
31
+ # If the response contains one or more arrays, the first array will be
32
+ # the CSV output. Otherwise, the response itself will be used.
33
+ def get_csv(path, params = {})
34
+ path = "#{path}.csv"
35
+ payload = get! path, params
36
+
37
+ if payload.response.code != '200'
38
+ raise Nasdaq::BadResponse, "#{payload.response.code} #{payload.response.msg}"
39
+ end
40
+
41
+ payload.response.body
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,97 @@
1
+ require 'super_docopt'
2
+ require 'json'
3
+ require 'lp'
4
+
5
+ module Nasdaq
6
+ # Handles the command line interface
7
+ class CommandLine < SuperDocopt::Base
8
+ version VERSION
9
+ docopt File.expand_path 'docopt.txt', __dir__
10
+ subcommands %w[get pretty see url save]
11
+
12
+ attr_reader :path, :params, :file, :csv
13
+
14
+ def before_execute
15
+ @path = args['PATH']
16
+ @params = translate_params args['PARAMS']
17
+ @file = args['FILE']
18
+ @csv = args['--csv']
19
+ end
20
+
21
+ def get
22
+ if csv
23
+ puts nasdaq.get_csv path, params
24
+ else
25
+ payload = nasdaq.get! path, params
26
+ puts payload.response.body
27
+ end
28
+ end
29
+
30
+ def save
31
+ success = if csv
32
+ nasdaq.save_csv file, path, params
33
+ else
34
+ nasdaq.save file, path, params
35
+ end
36
+ puts success ? "Saved #{file}" : 'Saving failed'
37
+ end
38
+
39
+ def pretty
40
+ payload = nasdaq.get path, params
41
+ puts JSON.pretty_generate payload
42
+ end
43
+
44
+ def see
45
+ lp nasdaq.get path, params
46
+ end
47
+
48
+ def url
49
+ puts nasdaq.url path, params
50
+ end
51
+
52
+ def nasdaq
53
+ @nasdaq ||= nasdaq!
54
+ end
55
+
56
+ private
57
+
58
+ def nasdaq!
59
+ API.new api_key, options
60
+ end
61
+
62
+ # Convert a params array like [key:value, key:value] to a hash like
63
+ # {key: value, key: value}
64
+ def translate_params(pairs)
65
+ result = {}
66
+ return result if pairs.empty?
67
+
68
+ pairs.each do |pair|
69
+ key, value = pair.split ':'
70
+ result[key.to_sym] = value
71
+ end
72
+ result
73
+ end
74
+
75
+ def options
76
+ result = {}
77
+ return result unless cache_dir || cache_life
78
+
79
+ result[:use_cache] = true
80
+ result[:cache_dir] = cache_dir if cache_dir
81
+ result[:cache_life] = cache_life.to_i if cache_life
82
+ result
83
+ end
84
+
85
+ def api_key
86
+ ENV['NASDAQ_KEY']
87
+ end
88
+
89
+ def cache_dir
90
+ ENV['NASDAQ_CACHE_DIR']
91
+ end
92
+
93
+ def cache_life
94
+ ENV['NASDAQ_CACHE_LIFE']
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,68 @@
1
+ Nasdaq Data Link
2
+
3
+ Usage:
4
+ nasdaq get [--csv] PATH [PARAMS...]
5
+ nasdaq pretty PATH [PARAMS...]
6
+ nasdaq see PATH [PARAMS...]
7
+ nasdaq url PATH [PARAMS...]
8
+ nasdaq save [--csv] FILE PATH [PARAMS...]
9
+ nasdaq (-h|--help|--version)
10
+
11
+ Commands:
12
+ get
13
+ Downloads data and prints it to screen as it.
14
+
15
+ pretty
16
+ Downloads data and prints it as a prettified JSON output.
17
+
18
+ see
19
+ Downloads data and prints it with a colored output.
20
+
21
+ url
22
+ Shows the URL constructed from the request.
23
+
24
+ save
25
+ Downloads data and saves it to a file.
26
+
27
+ Parameters:
28
+ PATH:
29
+ This is the Nasdaq Data Link API path without the query string.
30
+ For example: datasets/WIKI/AAPL.
31
+
32
+ PARAMS:
33
+ An optional list of query string parameters, separated by a space, to
34
+ send with the request. Each parameter should be in the format of
35
+ key:value, for example: page:2 per_page:10
36
+
37
+ FILE:
38
+ Path to the output file.
39
+
40
+ Flags:
41
+ --csv
42
+ When this flag is provided, the data will be converted to CSV before
43
+ it is displayed or saved. Note that this works only with endpoints that
44
+ have a 'data' attribute.
45
+
46
+ Environment Variables:
47
+ NASDAQ_KEY=y0urAP1k3y
48
+ Set Your Nasdaq Data Link API key. This variable is optional.
49
+
50
+ NASDAQ_CACHE_LIFE=360
51
+ Set the number of seconds to consider the cache fresh. This variable
52
+ it optional.
53
+
54
+ NASDAQ_CACHE_DIR=./cache
55
+ Set the cache directory. This variable is optional.
56
+ If both NASDAQ_CACHE_DIR and NASDAQ_CACHE_LIFE are not set, requests
57
+ will not be cached.
58
+
59
+ Examples:
60
+ nasdaq get databases per_page:2
61
+ nasdaq get datasets/WIKI/AAPL
62
+ nasdaq get --csv datasets/WIKI/AAPL rows:5
63
+ nasdaq get datasets source_code:WIKI query:*
64
+ nasdaq get datasets query:oil
65
+ nasdaq url datasets/WIKI/AAPL rows:5
66
+ nasdaq save --csv output.csv datasets/WIKI/AAPL rows:5
67
+ nasdaq save output.json datasets/WIKI/AAPL rows:5
68
+ nasdaq pretty datasets "query:qqq index" per_page:2
@@ -0,0 +1,3 @@
1
+ module Nasdaq
2
+ class BadResponse < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Nasdaq
2
+ VERSION = '0.3.0.rc1'
3
+ end
data/lib/nasdaq.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'nasdaq/version'
2
+ require 'nasdaq/api'
3
+ require 'nasdaq/command_line'
4
+ require 'nasdaq/exceptions'
5
+ require 'byebug' if ENV['BYEBUG']
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nasdaq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: apicake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lp
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: super_docopt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.2'
55
+ description: Easy to use API for the Nasdaq Data Link API with a command line interface
56
+ email: db@dannyben.com
57
+ executables:
58
+ - nasdaq
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - bin/nasdaq
64
+ - lib/nasdaq.rb
65
+ - lib/nasdaq/api.rb
66
+ - lib/nasdaq/command_line.rb
67
+ - lib/nasdaq/docopt.txt
68
+ - lib/nasdaq/exceptions.rb
69
+ - lib/nasdaq/version.rb
70
+ homepage: https://github.com/DannyBen/nasdaq
71
+ licenses:
72
+ - MIT
73
+ metadata:
74
+ bug_tracker_uri: https://github.com/DannyBen/nasdaq/issues
75
+ changelog_uri: https://github.com/DannyBen/nasdaq/blob/master/CHANGELOG.md
76
+ source_code_uri: https://github.com/DannyBen/nasdaq
77
+ rubygems_mfa_required: 'true'
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '2.7'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">"
90
+ - !ruby/object:Gem::Version
91
+ version: 1.3.1
92
+ requirements: []
93
+ rubygems_version: 3.4.9
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Nasdaq Data Link API Library and Command Line
97
+ test_files: []