quata 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 580ff598467d94e05a1ab31b7c6a07907fcb618a
4
+ data.tar.gz: 2acff7751ca88d39a4f57ea0b7aa1b31c09234be
5
+ SHA512:
6
+ metadata.gz: 8edcf7a6387ccb52603351c03685f68b5c2d4c35d806a1a7693cd019f2b8af2df7285832642a16a8decae6c2e726a875db0a3cd877d286fb63827c3d4b28ff54
7
+ data.tar.gz: 45aa82466c670de3b0935c510f00c4ba6ef4113a94fb14608fbc7a0e7ee30a4fe56b2c42ae752a21e9f959c69f56ab7dc58e8bb1e3ec9e839af3bb364b370184
@@ -0,0 +1,98 @@
1
+ Quata - Quandl API Library and Command Line
2
+ ==================================================
3
+
4
+ ---
5
+
6
+ Quata is a lightweight ruby library for accessing Quandl with a command
7
+ line interface.
8
+
9
+ ---
10
+
11
+ Features
12
+ --------------------------------------------------
13
+
14
+ * Access any Quandl endpoint directly.
15
+ * Display output in various formats.
16
+ * Save output to a file, including bulk downloads.
17
+
18
+ Example
19
+ --------------------------------------------------
20
+
21
+ First, require and initialize Quata with your API key
22
+
23
+ ```ruby
24
+ require 'quata'
25
+ quandl = Quandl.new 'Your API Key'
26
+ ```
27
+
28
+ Now, you can access any Quandl endpoint with any optional parameter, like
29
+ this:
30
+
31
+ ```ruby
32
+ result = quandl.get "datasets/WIKI/AAPL", rows: 3 # => Hash
33
+ ```
34
+
35
+ In addition, for convenience, you can use the first part of the endpoint as
36
+ a method name, like this:
37
+
38
+ ```ruby
39
+ result = quandl.datasets "WIKI/AAPL", rows: 3
40
+ ```
41
+
42
+ By default, you will get a ruby hash in return. If you wish to get the raw
43
+ output, you can use the `get!` method:
44
+
45
+ ```ruby
46
+ result = quandl.get! "datasets/WIKI/AAPL", rows: 3 # => JSON string
47
+ result = quandl.get! "datasets/WIKI/AAPL.json", rows: 3 # => JSON string
48
+ result = quandl.get! "datasets/WIKI/AAPL.csv", rows: 3 # => CSV string
49
+ ```
50
+
51
+ To save the output directly to a file, use the `save` method:
52
+
53
+ ```ruby
54
+ result = quandl.save "aapl.csv", "datasets/WIKI/AAPL.csv", rows: 3
55
+ ```
56
+
57
+ Command Line
58
+ --------------------------------------------------
59
+
60
+ The command line utility `quata` acts similarly. It provides these commands:
61
+
62
+ `$ quata get PATH [PARAMS...]` - print the output.
63
+ `$ quata pretty PATH [PARAMS...]` - print a pretty JSON.
64
+ `$ quata see PATH [PARAMS...]` - print a colored output.
65
+ `$ quata url PATH [PARAMS...]` - show the constructed URL.
66
+ `$ quata save FILE PATH [PARAMS...]` - save the output to a file.
67
+
68
+ Run `quata --help` for more information.
69
+
70
+ Examples:
71
+ ```bash
72
+ # Shows the first two databases
73
+ quata see databases per_page:2
74
+
75
+ # Or more compactly, as CSV
76
+ quata get databases per_page:2
77
+
78
+ # Prints CSV to screen (CSV is the default in the command line)
79
+ $ quata get datasets/WIKI/AAPL
80
+
81
+ # Prints JSON instead
82
+ $ quata get datasets/WIKI/AAPL.json
83
+
84
+ # Pass arguments using the same syntax - key:value
85
+ $ quata get datasets/WIKI/AAPL rows:5
86
+
87
+ # Pass arguments that require spaces
88
+ $ quata get datasets.json "query:qqq index"
89
+
90
+ # Prints a colored output
91
+ $ quata see datasets/WIKI/AAPL rows:5
92
+
93
+ # Saves a file
94
+ $ quata save output.csv datasets/WIKI/AAPL rows:5
95
+
96
+ # Shows the URL that Quata has constructed, good for debugging
97
+ $ quata url datasets/WIKI/AAPL rows:5
98
+ ```
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'quata'
4
+ Quata::CommandLine.instance.execute ARGV
@@ -0,0 +1,4 @@
1
+ require 'quata/version'
2
+ require 'quata/web_api'
3
+ require 'quata/quandl'
4
+ require 'quata/command_line'
@@ -0,0 +1,84 @@
1
+ require 'singleton'
2
+ require 'docopt'
3
+ require 'json'
4
+ require 'awesome_print'
5
+
6
+ module Quata
7
+
8
+ class CommandLine
9
+ include Singleton
10
+
11
+ attr_reader :quandl
12
+
13
+ def initialize
14
+ @quandl = Quandl.new api_key
15
+ @quandl.format :csv
16
+ end
17
+
18
+ def execute(argv=[])
19
+ doc = File.read File.dirname(__FILE__) + '/docopt.txt'
20
+ begin
21
+ args = Docopt::docopt(doc, argv: argv, version: VERSION)
22
+ handle args
23
+ rescue Docopt::Exit => e
24
+ puts e.message
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def handle(args)
31
+ path = args['PATH']
32
+ params = args['PARAMS']
33
+ file = args['FILE']
34
+
35
+ return get(path, params) if args['get']
36
+ return pretty(path, params) if args['pretty']
37
+ return see(path, params) if args['see']
38
+ return url(path, params) if args['url']
39
+ return save(file, path, params) if args['save']
40
+ end
41
+
42
+ def get(path, params)
43
+ puts quandl.get! path, translate_params(params)
44
+ end
45
+
46
+ def save(file, path, params)
47
+ success = quandl.save file, path, translate_params(params)
48
+ puts success ? "Saved #{file}" : "Saving failed"
49
+ end
50
+
51
+ def pretty(path, params)
52
+ quandl.format :json
53
+ response = quandl.get path, translate_params(params)
54
+ puts JSON.pretty_generate response
55
+ end
56
+
57
+ def see(path, params)
58
+ quandl.format :json
59
+ ap quandl.get path, translate_params(params)
60
+ end
61
+
62
+ def url(path, params)
63
+ quandl.debug true
64
+ puts quandl.get path, translate_params(params)
65
+ quandl.debug false
66
+ end
67
+
68
+ def translate_params(params)
69
+ return nil if params.empty?
70
+ result = {}
71
+ params.each do |param|
72
+ key, value = param.split ':'
73
+ result[key] = value
74
+ end
75
+ result
76
+ end
77
+
78
+ def api_key
79
+ @api_key ||= ENV['QUANDL_KEY']
80
+ end
81
+
82
+ end
83
+
84
+ end
@@ -0,0 +1,25 @@
1
+ # Quandl
2
+ require 'json'
3
+
4
+ class Quandl < Quata::WebAPI
5
+ attr_reader :api_key
6
+
7
+ def initialize(api_key=nil, base_url=nil)
8
+ @api_key = api_key
9
+
10
+ base_url ||= 'https://www.quandl.com/api/v3'
11
+
12
+ param :auth_token, api_key if api_key
13
+ format :json
14
+
15
+ after_request do |response|
16
+ begin
17
+ JSON.parse response, symbolize_names: true
18
+ rescue JSON::ParserError => e
19
+ response
20
+ end
21
+ end
22
+
23
+ super base_url
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Quata
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,101 @@
1
+ require 'uri'
2
+ require 'open-uri'
3
+
4
+ module Quata
5
+
6
+ class WebAPI
7
+ attr_reader :base_url, :after_request_block, :debug_mode
8
+
9
+ def initialize(base_url)
10
+ @base_url = base_url
11
+ end
12
+
13
+ # Allow using any method as the first segment of the path
14
+ # object.user 'details' becomes object.get 'user/details'
15
+ def method_missing(method_sym, *arguments, &block)
16
+ begin
17
+ get "/#{method_sym}", *arguments
18
+ rescue
19
+ super
20
+ end
21
+ end
22
+
23
+ # Add a parameter to the default query string. Good for adding keys that
24
+ # are always needed, like API keys.
25
+ def param(key, value)
26
+ default_params[key] = value
27
+ end
28
+
29
+ # Set the default format that will be appended to the URL. value can be
30
+ # a string or a symbol.
31
+ def format(value=nil)
32
+ @format = value if value
33
+ @format
34
+ end
35
+
36
+ # Set debug mode, which will return the requested, restructured URL
37
+ # instead of returning the response.
38
+ def debug(value)
39
+ @debug_mode = value
40
+ end
41
+
42
+ # Set a block to be executed after the request. This is called only when
43
+ # using `get` and not when using `get!`. Good for JSON decoding, for
44
+ # example.
45
+ def after_request(&block)
46
+ @after_request_block = block
47
+ end
48
+
49
+ # Return the response from teh API.
50
+ def get(path, extra=nil, params={})
51
+ response = get! path, extra, params
52
+ response = after_request_block.call(response) if after_request_block
53
+ response
54
+ end
55
+
56
+ # Return the response from the API, without executing the after_request
57
+ # block.
58
+ def get!(path, extra=nil, params={})
59
+ if extra.is_a?(Hash) and params.empty?
60
+ params = extra
61
+ extra = nil
62
+ end
63
+
64
+ path = "#{path}/#{extra}" if extra
65
+ url = construct_url path, params
66
+
67
+ debug_mode ? url : http_get(url)
68
+ end
69
+
70
+ # Save the response from the API to a file.
71
+ def save(filename, path, params={})
72
+ response = get! path, nil, params
73
+ return response if debug_mode
74
+ File.write filename, response.to_s
75
+ end
76
+
77
+ # Build a URL from all its explicit and implicit pieces.
78
+ def construct_url(path, params={})
79
+ path = "/#{path}" unless path[0] == '/'
80
+ all_params = default_params.merge params
81
+ result = "#{base_url}#{path}"
82
+ result = "#{result}.#{format}" if format && File.extname(result) == ''
83
+ unless all_params.empty?
84
+ all_params = URI.encode_www_form all_params
85
+ result = "#{result}?#{all_params}"
86
+ end
87
+ result
88
+ end
89
+
90
+ def default_params
91
+ @default_params ||= {}
92
+ end
93
+
94
+ private
95
+
96
+ def http_get(url)
97
+ open(url).read
98
+ end
99
+ end
100
+
101
+ end
metadata ADDED
@@ -0,0 +1,177 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: docopt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: runfile
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: runfile-tasks
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rdoc
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '4.2'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '4.2'
111
+ - !ruby/object:Gem::Dependency
112
+ name: byebug
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '9.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '9.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.11'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.11'
139
+ description: Easy to use API for Quandl data service with a command line interface
140
+ email: db@dannyben.com
141
+ executables:
142
+ - quata
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - README.md
147
+ - bin/quata
148
+ - lib/quata.rb
149
+ - lib/quata/command_line.rb
150
+ - lib/quata/quandl.rb
151
+ - lib/quata/version.rb
152
+ - lib/quata/web_api.rb
153
+ homepage: https://github.com/DannyBen/quata
154
+ licenses:
155
+ - MIT
156
+ metadata: {}
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: 2.0.0
166
+ required_rubygems_version: !ruby/object:Gem::Requirement
167
+ requirements:
168
+ - - ">="
169
+ - !ruby/object:Gem::Version
170
+ version: '0'
171
+ requirements: []
172
+ rubyforge_project:
173
+ rubygems_version: 2.4.6
174
+ signing_key:
175
+ specification_version: 4
176
+ summary: Quandl API Library and Command Line
177
+ test_files: []