quata 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7784275cd18e6a826b6a74a9099cfa0270557779
4
- data.tar.gz: c4f4655ae4b3436aa4f0fd289673d9c748682201
3
+ metadata.gz: ee013d7102f41a7111beeb91d3e281e979e5a6e5
4
+ data.tar.gz: bebc7f30bdf578fea50410f71478e33ae2399bbd
5
5
  SHA512:
6
- metadata.gz: ff5064da6fd39ba64ef4b57df65f694d537129febcfbfb458065c7bf6c7e86e66260b7c8b91d6b9c375d95b89b3e1c15a3db563ac161cade625fc3fe28c47798
7
- data.tar.gz: fe0b245fcfdfb8204b99f76223008460229d2bb0446fc1a9810b5b7ad7884c3a3069ee2f578062c0e57f0884e695ac7749ecbc814fd7e30275d4c46afc23f07b
6
+ metadata.gz: 248bc0587c9c8ad2f51ecfe11add8893a65216e8c5711ba8029e1d34cd55ddf444bc20c394593ea08607d685e945106c821989fdf358590d1ae86f83b1af307b
7
+ data.tar.gz: a76b18055f12c4cb24cf4941911bc3c07ea7730869c5c11e0369f22cacdd4440c092283724a223e1102f4ea213f33afb604773da7bdf4e2f76c3a95ee1598cd6
data/README.md CHANGED
@@ -28,6 +28,7 @@ Or with bundler:
28
28
  gem 'quata'
29
29
  ```
30
30
 
31
+
31
32
  Features
32
33
  --------------------------------------------------
33
34
 
@@ -38,6 +39,7 @@ Features
38
39
  * Save output to a file, including bulk downloads.
39
40
  * Includes a built in file cache (disabled by default).
40
41
 
42
+
41
43
  Usage
42
44
  --------------------------------------------------
43
45
 
@@ -45,7 +47,7 @@ First, require and initialize with your API key
45
47
 
46
48
  ```ruby
47
49
  require 'quata'
48
- quandl = Quandl.new 'Your API Key'
50
+ quandl = Quata::API.new 'Your API Key'
49
51
  ```
50
52
 
51
53
  Now, you can access any Quandl endpoint with any optional parameter, like
@@ -62,33 +64,73 @@ a method name, like this:
62
64
  result = quandl.datasets "WIKI/AAPL", rows: 3
63
65
  ```
64
66
 
65
- By default, you will get a ruby hash in return. If you wish to get the raw
66
- output, you can use the `get!` method:
67
+ In other words, these calls are the same:
67
68
 
68
69
  ```ruby
69
- result = quandl.get! "datasets/WIKI/AAPL", rows: 3 # => JSON string
70
- result = quandl.get! "datasets/WIKI/AAPL.json", rows: 3 # => JSON string
71
- result = quandl.get! "datasets/WIKI/AAPL.csv", rows: 3 # => CSV string
70
+ quandl.get 'endpoint', param: value
71
+ quandl.endpoint, param: value
72
72
  ```
73
73
 
74
- To save the output directly to a file, use the `save` method:
74
+ as well as these two:
75
75
 
76
76
  ```ruby
77
- quandl.save "aapl.csv", "datasets/WIKI/AAPL.csv", rows: 3
77
+ quandl.get 'endpoint/sub', param: value
78
+ quandl.endpoint 'sub', param: value
78
79
  ```
79
80
 
80
- Debugging your request and adding "sticky" query parameters that stay with
81
- you for the following requests is also easy:
81
+ By default, you will get a ruby hash in return. If you wish to have more
82
+ control over the response, use the `get!` method instead:
82
83
 
83
84
  ```ruby
84
- quandl.debug = true
85
- quandl.param rows: 10, order: 'asc'
86
- puts quandl.datasets 'WIKI/AAPL'
87
- # => https://www.quandl.com/api/v3/datasets/WIKI/AAPL.json?auth_token=key&rows=10&order=asc
85
+ result = quandl.get! "datasets/WIKI/AAPL", rows: 3
86
+
87
+ # Request Object
88
+ p payload.request.class
89
+ # => HTTParty::Request
90
+
91
+ # Response Object
92
+ p payload.response.class
93
+ # => Net::HTTPOK
94
+
95
+ p payload.response.body
96
+ # => JSON string
88
97
 
89
- quandl.param order: nil # remove param
98
+ p payload.response.code
99
+ # => 200
100
+
101
+ p payload.response.msg
102
+ # => OK
103
+
104
+ # Headers Object
105
+ p payload.headers
106
+ # => Hash with headers
107
+
108
+ # Parsed Response Object
109
+ p payload.parsed_response
110
+ # => Hash with HTTParty parsed response
111
+ # (this is the content returned with #get)
90
112
  ```
91
113
 
114
+ You can get the response as CSV by calling `get_csv`:
115
+
116
+ ```ruby
117
+ result = quandl.get_csv "datasets/WIKI/AAPL", rows: 3
118
+ # => CSV string
119
+ ```
120
+
121
+ To save the output directly to a file, use the `save` method:
122
+
123
+ ```ruby
124
+ quandl.save "filename.json", "datasets/WIKI/AAPL", rows: 3
125
+ ```
126
+
127
+ Or, to save CSV, use the `save_csv` method:
128
+
129
+ ```ruby
130
+ quandl.save_csv "filename.csv", "datasets/WIKI/AAPL", rows: 3
131
+ ```
132
+
133
+
92
134
  Command Line
93
135
  --------------------------------------------------
94
136
 
@@ -142,7 +184,7 @@ $ quata url datasets/WIKI/AAPL rows:5
142
184
  Caching
143
185
  --------------------------------------------------
144
186
 
145
- Quata uses the [WebCache][3] gem for automatic HTTP caching.
187
+ Quata uses the [Lightly][3] gem for automatic HTTP caching.
146
188
  To take the path of least surprises, caching is disabled by default.
147
189
 
148
190
  You can enable and customize it by either passing options on
@@ -150,13 +192,13 @@ initialization, or by accessing the `WebCache` object directly at
150
192
  a later stage.
151
193
 
152
194
  ```ruby
153
- quandl = Quandl.new 'Your API Key', use_cache: true
154
- quandl = Quandl.new 'Your API Key', use_cache: true, cache_dir: 'tmp'
155
- quandl = Quandl.new 'Your API Key', use_cache: true, cache_life: 120
195
+ quandl = Quata::API.new 'Your API Key', use_cache: true
196
+ quandl = Quata::API.new 'Your API Key', use_cache: true, cache_dir: 'tmp'
197
+ quandl = Quata::API.new 'Your API Key', use_cache: true, cache_life: 120
156
198
 
157
199
  # or
158
200
 
159
- quandl = Quandl.new 'Your API Key'
201
+ quandl = Quata::API.new 'Your API Key'
160
202
  quandl.cache.enable
161
203
  quandl.cache.dir = 'tmp/cache' # Change cache folder
162
204
  quandl.cache.life = 120 # Change cache life to 2 minutes
@@ -180,4 +222,4 @@ Terminalcast
180
222
 
181
223
  [1]: https://www.quandl.com/blog/getting-started-with-the-quandl-api
182
224
  [2]: https://github.com/DannyBen/quata/blob/master/lib/quata/docopt.txt
183
- [3]: https://github.com/DannyBen/webcache
225
+ [3]: https://github.com/DannyBen/lightly
data/bin/quata CHANGED
@@ -1,4 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require 'quata'
4
- Quata::CommandLine.instance.execute ARGV
4
+
5
+ begin
6
+ Quata::CommandLine.instance.execute ARGV
7
+ rescue Quata::BadResponse => e
8
+ STDERR.puts "#{e.class} - #{e.message}"
9
+ end
@@ -1,4 +1,4 @@
1
1
  require 'quata/version'
2
- require 'quata/web_api'
3
- require 'quata/quandl'
2
+ require 'quata/api'
4
3
  require 'quata/command_line'
4
+ require 'quata/exceptions'
@@ -0,0 +1,42 @@
1
+ require 'apicake'
2
+
3
+ module Quata
4
+ # Provides access to all the Quandl API endpoints
5
+ class API < APICake::Base
6
+ base_uri 'https://www.quandl.com/api/v3'
7
+
8
+ attr_reader :api_key
9
+
10
+ # TODO:
11
+ # - handle format
12
+
13
+ # Initializes the API with an optional API Key, and cache settings.
14
+ def initialize(api_key=nil, opts={})
15
+ opts, api_key = api_key, nil if api_key.is_a?(Hash) && opts.empty?
16
+ @api_key = api_key
17
+ cache.disable unless opts[:use_cache]
18
+ cache.dir = opts[:cache_dir] if opts[:cache_dir]
19
+ cache.life = opts[:cache_life] if opts[:cache_life]
20
+ end
21
+
22
+ # Returns a hash that will be merged into all query strings before
23
+ # sending the request to Quandl. This method is used by API Cake.
24
+ def default_query
25
+ { api_key: api_key }
26
+ end
27
+
28
+ # Forwards all arguments to #get! and converts the JSON response to CSV
29
+ # If the response contains one or more arrays, the first array will be
30
+ # the CSV output. Otherwise, the response itself will be used.
31
+ def get_csv(path, params={})
32
+ path = "#{path}.csv"
33
+ payload = get! path, params
34
+
35
+ if payload.response.code != '200'
36
+ raise Quata::BadResponse, "#{payload.response.code} #{payload.response.msg}"
37
+ end
38
+
39
+ payload.response.body
40
+ end
41
+ end
42
+ end
@@ -9,13 +9,6 @@ module Quata
9
9
  class CommandLine
10
10
  include Singleton
11
11
 
12
- attr_reader :quandl
13
-
14
- def initialize
15
- @quandl = Quandl.new api_key, options
16
- @quandl.format = :csv
17
- end
18
-
19
12
  # Gets an array of arguments (e.g. ARGV), executes the command if valid
20
13
  # and shows usage patterns / help otherwise.
21
14
  def execute(argv=[])
@@ -28,73 +21,90 @@ module Quata
28
21
  end
29
22
  end
30
23
 
24
+ def quandl
25
+ @quandl ||= quandl!
26
+ end
27
+
31
28
  private
32
29
 
30
+ attr_reader :path, :params, :file, :csv
31
+
32
+ def quandl!
33
+ API.new api_key, options
34
+ end
35
+
33
36
  # Called when the arguments match one of the usage patterns. Will
34
37
  # delegate action to other, more specialized methods.
35
38
  def handle(args)
36
- path = args['PATH']
37
- params = args['PARAMS']
38
- file = args['FILE']
39
-
40
- return get(path, params) if args['get']
41
- return pretty(path, params) if args['pretty']
42
- return see(path, params) if args['see']
43
- return url(path, params) if args['url']
44
- return save(file, path, params) if args['save']
39
+ @path = args['PATH']
40
+ @params = translate_params args['PARAMS']
41
+ @file = args['FILE']
42
+ @csv = args['--csv']
43
+
44
+ return get if args['get']
45
+ return pretty if args['pretty']
46
+ return see if args['see']
47
+ return url if args['url']
48
+ return save if args['save']
45
49
  end
46
50
 
47
- def get(path, params)
48
- puts quandl.get! path, translate_params(params)
51
+ def get
52
+ if csv
53
+ puts quandl.get_csv path, params
54
+ else
55
+ payload = quandl.get! path, params
56
+ puts payload.response.body
57
+ end
49
58
  end
50
59
 
51
- def save(file, path, params)
52
- success = quandl.save file, path, translate_params(params)
60
+ def save
61
+ if csv
62
+ success = quandl.save_csv file, path, params
63
+ else
64
+ success = quandl.save file, path, params
65
+ end
53
66
  puts success ? "Saved #{file}" : "Saving failed"
54
67
  end
55
68
 
56
- def pretty(path, params)
57
- quandl.format = :json
58
- response = quandl.get path, translate_params(params)
59
- puts JSON.pretty_generate response
69
+ def pretty
70
+ payload = quandl.get path, params
71
+ puts JSON.pretty_generate payload
60
72
  end
61
73
 
62
- def see(path, params)
63
- quandl.format = :json
64
- ap quandl.get path, translate_params(params)
74
+ def see
75
+ ap quandl.get path, params
65
76
  end
66
77
 
67
- def url(path, params)
68
- quandl.debug = true
69
- puts quandl.get path, translate_params(params)
70
- quandl.debug = false
78
+ def url
79
+ puts quandl.url path, params
71
80
  end
72
81
 
73
82
  # Convert a params array like [key:value, key:value] to a hash like
74
83
  # {key: value, key: value}
75
- def translate_params(params)
76
- return nil if params.empty?
84
+ def translate_params(pairs)
77
85
  result = {}
78
- params.each do |param|
79
- key, value = param.split ':'
80
- result[key] = value
86
+ return result if pairs.empty?
87
+ pairs.each do |pair|
88
+ key, value = pair.split ':'
89
+ result[key.to_sym] = value
81
90
  end
82
91
  result
83
92
  end
84
93
 
85
- def api_key
86
- @api_key ||= ENV['QUANDL_KEY']
87
- end
88
-
89
94
  def options
90
- return {} unless cache_dir || cache_life
91
95
  result = {}
96
+ return result unless cache_dir || cache_life
97
+
92
98
  result[:use_cache] = true
93
99
  result[:cache_dir] = cache_dir if cache_dir
94
100
  result[:cache_life] = cache_life.to_i if cache_life
95
101
  result
96
102
  end
97
103
 
104
+ def api_key
105
+ ENV['QUANDL_KEY']
106
+ end
107
+
98
108
  def cache_dir
99
109
  ENV['QUANDL_CACHE_DIR']
100
110
  end
@@ -1,11 +1,11 @@
1
1
  Quata
2
2
 
3
3
  Usage:
4
- quata get PATH [PARAMS...]
4
+ quata get [--csv] PATH [PARAMS...]
5
5
  quata pretty PATH [PARAMS...]
6
6
  quata see PATH [PARAMS...]
7
7
  quata url PATH [PARAMS...]
8
- quata save FILE PATH [PARAMS...]
8
+ quata save [--csv] FILE PATH [PARAMS...]
9
9
  quata (-h|--help|--version)
10
10
 
11
11
  Commands:
@@ -26,10 +26,8 @@ Commands:
26
26
 
27
27
  Parameters:
28
28
  PATH:
29
- This is the Quandl API path, for example: datasets/WIKI/AAPL.
30
- Format is defaulted to CSV in most cases, but you can override it like
31
- you normally would by appending it at the end, like
32
- datasets/WIKI/AAPL.json
29
+ This is the Quandl API path without the query string.
30
+ For example: datasets/WIKI/AAPL.
33
31
 
34
32
  PARAMS:
35
33
  An optional list of query string parameters, separated by a space, to
@@ -39,14 +37,32 @@ Parameters:
39
37
  FILE:
40
38
  Path to the output file.
41
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
+ QUANDL_KEY=y0urAP1k3y
48
+ Set Your Quandl api key. This variable is optional.
49
+
50
+ QUANDL_CACHE_LIFE=360
51
+ Set the number of seconds to consider the cache fresh. This variable
52
+ it optional.
53
+
54
+ QUANDL_CACHE_DIR=./cache
55
+ Set the cache directory. This variable is optional.
56
+ If both QUANDL_CACHE_DIR and QUANDL_CACHE_LIFE are not set, requests
57
+ will not be cached.
58
+
42
59
  Examples:
43
60
  quata get databases per_page:2
44
61
  quata get datasets/WIKI/AAPL
45
- quata get datasets/WIKI/AAPL rows:5
62
+ quata get --csv datasets/WIKI/AAPL rows:5
46
63
  quata get datasets source_code:WIKI query:*
47
64
  quata get datasets query:oil
48
- quata get datasets.json "query:qqq index"
49
- quata see datasets/WIKI/AAPL rows:5
50
65
  quata url datasets/WIKI/AAPL rows:5
51
- quata save output.csv datasets/WIKI/AAPL rows:5
52
- quata pretty datasets.json query:qqq
66
+ quata save --csv output.csv datasets/WIKI/AAPL rows:5
67
+ quata save output.json datasets/WIKI/AAPL rows:5
68
+ quata pretty datasets "query:qqq index" per_page:2
@@ -0,0 +1,3 @@
1
+ module Quata
2
+ class BadResponse < StandardError; end
3
+ end
@@ -1,3 +1,3 @@
1
1
  module Quata
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-22 00:00:00.000000000 Z
11
+ date: 2017-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docopt
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.6'
41
41
  - !ruby/object:Gem::Dependency
42
- name: webcache
42
+ name: apicake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
@@ -160,11 +160,11 @@ files:
160
160
  - README.md
161
161
  - bin/quata
162
162
  - lib/quata.rb
163
+ - lib/quata/api.rb
163
164
  - lib/quata/command_line.rb
164
165
  - lib/quata/docopt.txt
165
- - lib/quata/quandl.rb
166
+ - lib/quata/exceptions.rb
166
167
  - lib/quata/version.rb
167
- - lib/quata/web_api.rb
168
168
  homepage: https://github.com/DannyBen/quata
169
169
  licenses:
170
170
  - MIT
@@ -1,41 +0,0 @@
1
- require 'json'
2
-
3
- # Provides access to all the Quandl API endpoints
4
- class Quandl < Quata::WebAPI
5
- attr_reader :api_key
6
-
7
- def initialize(api_key=nil, opts={})
8
- if api_key.is_a? Hash
9
- opts = api_key
10
- api_key = nil
11
- end
12
-
13
- @api_key = api_key
14
-
15
- defaults = {
16
- use_cache: false,
17
- cache_dir: nil,
18
- cache_life: nil,
19
- base_url: 'https://www.quandl.com/api/v3'
20
- }
21
-
22
- opts = defaults.merge! opts
23
-
24
- self.cache.disable unless opts[:use_cache]
25
- self.cache.dir = opts[:cache_dir] if opts[:cache_dir]
26
- self.cache.life = opts[:cache_life] if opts[:cache_life]
27
-
28
- param auth_token: api_key if api_key
29
- self.format = :json
30
-
31
- after_request do |response|
32
- begin
33
- JSON.parse response, symbolize_names: true
34
- rescue JSON::ParserError
35
- response
36
- end
37
- end
38
-
39
- super opts[:base_url]
40
- end
41
- end
@@ -1,93 +0,0 @@
1
- require 'uri'
2
- require 'open-uri'
3
- require 'webcache'
4
-
5
- module Quata
6
-
7
- # A general purpose HTTP wrapper. This is poor man's HTTParty with
8
- # dynamic methods.
9
- class WebAPI
10
- attr_reader :base_url, :after_request_block, :last_error
11
- attr_accessor :debug, :format
12
-
13
- def initialize(base_url)
14
- @base_url = base_url
15
- end
16
-
17
- # Allow using any method as the first segment of the path
18
- # object.user 'details' becomes object.get 'user/details'
19
- def method_missing(method_sym, *arguments, &block)
20
- get "/#{method_sym}", *arguments
21
- end
22
-
23
- # Add one or more parameter to the default query string. Good for
24
- # adding keys that are always needed, like API keys.
25
- def param(params)
26
- params.each do |key, value|
27
- default_params[key] = value
28
- default_params.delete key if value.nil?
29
- end
30
- end
31
-
32
- def cache
33
- @cache ||= WebCache.new
34
- end
35
-
36
- # Set a block to be executed after the request. This is called only when
37
- # using `get` and not when using `get!`. Good for JSON decoding, for
38
- # example.
39
- def after_request(&block)
40
- @after_request_block = block
41
- end
42
-
43
- # Return the response from the API.
44
- def get(path, extra=nil, params={})
45
- response = get! path, extra, params
46
- response = after_request_block.call(response) if after_request_block
47
- response
48
- end
49
-
50
- # Return the response from the API, without executing the after_request
51
- # block.
52
- def get!(path, extra=nil, params={})
53
- if extra.is_a?(Hash) and params.empty?
54
- params = extra
55
- extra = nil
56
- end
57
-
58
- path = "#{path}/#{extra}" if extra
59
- url = construct_url path, params
60
-
61
- return url if debug
62
-
63
- response = cache.get(url)
64
- @last_error = response.error
65
- response.content
66
- end
67
-
68
- # Save the response from the API to a file.
69
- def save(filename, path, params={})
70
- response = get! path, nil, params
71
- return response if debug
72
- File.write filename, response.to_s
73
- end
74
-
75
- # Build a URL from all its explicit and implicit pieces.
76
- def construct_url(path, params={})
77
- path = "/#{path}" unless path[0] == '/'
78
- all_params = default_params.merge params
79
- result = "#{base_url}#{path}"
80
- result = "#{result}.#{format}" if format && File.extname(result) == ''
81
- unless all_params.empty?
82
- all_params = URI.encode_www_form all_params
83
- result = "#{result}?#{all_params}"
84
- end
85
- result
86
- end
87
-
88
- def default_params
89
- @default_params ||= {}
90
- end
91
-
92
- end
93
- end