quata 0.1.2 → 0.1.3

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: 580e41302fffc675f215c6e0be760ee1c12331d1
4
- data.tar.gz: 2366adc0b92c05bb7b7fb3535c32cace572274fb
3
+ metadata.gz: 7974b74f540308b621c8a7fc0de260af55852223
4
+ data.tar.gz: e1277a24096c30ddc75b819cdc62be04740a4b68
5
5
  SHA512:
6
- metadata.gz: 013f37a822659606f555ccc6c3c3f454e3b57b18a24f84234947812509e2455f8102d7ee46768751ec903019cb40038956095b5a4c585558dcee10a0fcce9979
7
- data.tar.gz: 1d70b2d056137211277eb9e59070d3c8946a10eb14b4be974b39a22c5b39b863b9717d855383894df6d1bc41c410b04aa0ec014f9748d5000127867fae99b3db
6
+ metadata.gz: 8475fd5c7e82dcac96a77744f2e45b3c8ef6c18b9e576f48391553bdd9827c8a0bf0ba513c89ff679895bbdebf5aa7cb2b26184b87ab8ce2358c135f4882e3a3
7
+ data.tar.gz: 998facc8591d26543bcdc618d9bfa96654bcb1dee09d7f75102aad7ebf36bc5f4689aed56a173b3932eb2a94d919159b5e28be0bd74ddd53cb83be80297a58f9
data/README.md CHANGED
@@ -15,6 +15,19 @@ It provides direct access to all of the [Quandl API][1] endpoints.
15
15
 
16
16
  ---
17
17
 
18
+ Install
19
+ --------------------------------------------------
20
+
21
+ ```
22
+ $ gem install quata
23
+ ```
24
+
25
+ Or with bundler:
26
+
27
+ ```ruby
28
+ gem 'quata'
29
+ ```
30
+
18
31
  Features
19
32
  --------------------------------------------------
20
33
 
@@ -23,6 +36,7 @@ Features
23
36
  * Access any Quandl endpoint directly.
24
37
  * Display output in various formats.
25
38
  * Save output to a file, including bulk downloads.
39
+ * Includes a built in file cache (disabled by default).
26
40
 
27
41
  Usage
28
42
  --------------------------------------------------
@@ -60,14 +74,14 @@ result = quandl.get! "datasets/WIKI/AAPL.csv", rows: 3 # => CSV string
60
74
  To save the output directly to a file, use the `save` method:
61
75
 
62
76
  ```ruby
63
- result = quandl.save "aapl.csv", "datasets/WIKI/AAPL.csv", rows: 3
77
+ quandl.save "aapl.csv", "datasets/WIKI/AAPL.csv", rows: 3
64
78
  ```
65
79
 
66
80
  Debugging your request and adding "sticky" query parameters that stay with
67
81
  you for the following requests is also easy:
68
82
 
69
83
  ```ruby
70
- quandl.debug true
84
+ quandl.debug = true
71
85
  quandl.param rows: 10, order: 'asc'
72
86
  puts quandl.get 'WIKI/AAPL'
73
87
  # => https://www.quandl.com/api/v3/WIKI/AAPL.json?auth_token=key&rows=10&order=asc
@@ -125,7 +139,45 @@ $ quata url datasets/WIKI/AAPL rows:5
125
139
  # => https://www.quandl.com/api/v3/datasets/WIKI/AAPL.csv?auth_token=YOUR_KEY&rows=5
126
140
  ```
127
141
 
142
+ Caching
143
+ --------------------------------------------------
144
+
145
+ Quata uses the [WebCache][3] gem for automatic HTTP caching.
146
+ To take the path of least surprises, caching is disabled by default.
147
+
148
+ You can enable and customize it by either passing options on
149
+ initialization, or by accessing the `WebCache` object directly at
150
+ a later stage.
151
+
152
+ ```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
156
+
157
+ # or
158
+
159
+ quandl = Quandl.new 'Your API Key'
160
+ quandl.cache.enable
161
+ quandl.cache.dir = 'tmp/cache' # Change cache folder
162
+ quandl.cache.life = 120 # Change cache life to 2 minutes
163
+ ```
164
+
165
+ To enable caching for the command line, simply set one or both of
166
+ these environment variables:
167
+
168
+ ```
169
+ $ export QUANDL_CACHE_DIR=cache
170
+ $ export QUANDL_CACHE_LIFE=120
171
+ $ quata get datasets/WIKI/AAPL
172
+ # => This call will be cached
173
+ ```
174
+
175
+
176
+ Terminalcast
177
+ --------------------------------------------------
178
+
128
179
  ![Quata Demo](https://raw.githubusercontent.com/DannyBen/quata/master/demo.gif "Quata Demo")
129
180
 
130
181
  [1]: https://www.quandl.com/blog/getting-started-with-the-quandl-api
131
182
  [2]: https://github.com/DannyBen/quata/blob/master/lib/quata/docopt.txt
183
+ [3]: https://github.com/DannyBen/webcache
@@ -12,8 +12,8 @@ module Quata
12
12
  attr_reader :quandl
13
13
 
14
14
  def initialize
15
- @quandl = Quandl.new api_key
16
- @quandl.format :csv
15
+ @quandl = Quandl.new api_key, options
16
+ @quandl.format = :csv
17
17
  end
18
18
 
19
19
  # Gets an array of arguments (e.g. ARGV), executes the command if valid
@@ -54,20 +54,20 @@ module Quata
54
54
  end
55
55
 
56
56
  def pretty(path, params)
57
- quandl.format :json
57
+ quandl.format = :json
58
58
  response = quandl.get path, translate_params(params)
59
59
  puts JSON.pretty_generate response
60
60
  end
61
61
 
62
62
  def see(path, params)
63
- quandl.format :json
63
+ quandl.format = :json
64
64
  ap quandl.get path, translate_params(params)
65
65
  end
66
66
 
67
67
  def url(path, params)
68
- quandl.debug true
68
+ quandl.debug = true
69
69
  puts quandl.get path, translate_params(params)
70
- quandl.debug false
70
+ quandl.debug = false
71
71
  end
72
72
 
73
73
  # Convert a params array like [key:value, key:value] to a hash like
@@ -86,6 +86,22 @@ module Quata
86
86
  @api_key ||= ENV['QUANDL_KEY']
87
87
  end
88
88
 
89
- end
89
+ def options
90
+ return {} unless cache_dir || cache_life
91
+ result = {}
92
+ result[:use_cache] = true
93
+ result[:cache_dir] = cache_dir if cache_dir
94
+ result[:cache_life] = cache_life.to_i if cache_life
95
+ result
96
+ end
90
97
 
98
+ def cache_dir
99
+ ENV['QUANDL_CACHE_DIR']
100
+ end
101
+
102
+ def cache_life
103
+ ENV['QUANDL_CACHE_LIFE']
104
+ end
105
+
106
+ end
91
107
  end
@@ -4,13 +4,29 @@ require 'json'
4
4
  class Quandl < Quata::WebAPI
5
5
  attr_reader :api_key
6
6
 
7
- def initialize(api_key=nil, base_url=nil)
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
+
8
13
  @api_key = api_key
9
14
 
10
- base_url ||= 'https://www.quandl.com/api/v3'
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]
11
27
 
12
28
  param auth_token: api_key if api_key
13
- format :json
29
+ self.format = :json
14
30
 
15
31
  after_request do |response|
16
32
  begin
@@ -20,6 +36,6 @@ class Quandl < Quata::WebAPI
20
36
  end
21
37
  end
22
38
 
23
- super base_url
39
+ super opts[:base_url]
24
40
  end
25
41
  end
@@ -1,3 +1,3 @@
1
1
  module Quata
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -1,12 +1,14 @@
1
1
  require 'uri'
2
2
  require 'open-uri'
3
+ require 'webcache'
3
4
 
4
5
  module Quata
5
6
 
6
7
  # A general purpose HTTP wrapper. This is poor man's HTTParty with
7
8
  # dynamic methods.
8
9
  class WebAPI
9
- attr_reader :base_url, :after_request_block, :debug_mode
10
+ attr_reader :base_url, :after_request_block
11
+ attr_accessor :debug, :format
10
12
 
11
13
  def initialize(base_url)
12
14
  @base_url = base_url
@@ -15,11 +17,7 @@ module Quata
15
17
  # Allow using any method as the first segment of the path
16
18
  # object.user 'details' becomes object.get 'user/details'
17
19
  def method_missing(method_sym, *arguments, &block)
18
- begin
19
- get "/#{method_sym}", *arguments
20
- rescue
21
- super
22
- end
20
+ get "/#{method_sym}", *arguments
23
21
  end
24
22
 
25
23
  # Add one or more parameter to the default query string. Good for
@@ -31,17 +29,13 @@ module Quata
31
29
  end
32
30
  end
33
31
 
34
- # Set the default format that will be appended to the URL. value can be
35
- # a string or a symbol.
36
- def format(value=nil)
37
- @format = value if value
38
- @format
32
+ def cache
33
+ @cache ||= WebCache.new
39
34
  end
40
35
 
41
- # Set debug mode, which will return the requested, restructured URL
42
- # instead of returning the response.
43
- def debug(value)
44
- @debug_mode = value
36
+ # Return the last HTTP error, or false if all was good
37
+ def last_error
38
+ cache.last_error
45
39
  end
46
40
 
47
41
  # Set a block to be executed after the request. This is called only when
@@ -69,13 +63,13 @@ module Quata
69
63
  path = "#{path}/#{extra}" if extra
70
64
  url = construct_url path, params
71
65
 
72
- debug_mode ? url : http_get(url)
66
+ debug ? url : cache.get(url)
73
67
  end
74
68
 
75
69
  # Save the response from the API to a file.
76
70
  def save(filename, path, params={})
77
71
  response = get! path, nil, params
78
- return response if debug_mode
72
+ return response if debug
79
73
  File.write filename, response.to_s
80
74
  end
81
75
 
@@ -96,15 +90,5 @@ module Quata
96
90
  @default_params ||= {}
97
91
  end
98
92
 
99
- private
100
-
101
- def http_get(url)
102
- begin
103
- open(url).read
104
- rescue OpenURI::HTTPError => e
105
- e.message
106
- end
107
- end
108
93
  end
109
-
110
94
  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.2
4
+ version: 0.1.3
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-05-20 00:00:00.000000000 Z
11
+ date: 2016-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docopt
@@ -39,61 +39,61 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.6'
41
41
  - !ruby/object:Gem::Dependency
42
- name: runfile
42
+ name: webcache
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.7'
48
- type: :development
47
+ version: '0.1'
48
+ type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0.7'
54
+ version: '0.1'
55
55
  - !ruby/object:Gem::Dependency
56
- name: runfile-tasks
56
+ name: runfile
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.4'
61
+ version: '0.7'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.4'
68
+ version: '0.7'
69
69
  - !ruby/object:Gem::Dependency
70
- name: rspec
70
+ name: runfile-tasks
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '3.4'
75
+ version: '0.4'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '3.4'
82
+ version: '0.4'
83
83
  - !ruby/object:Gem::Dependency
84
- name: webmock
84
+ name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '2.0'
89
+ version: '3.4'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '2.0'
96
+ version: '3.4'
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: rdoc
99
99
  requirement: !ruby/object:Gem::Requirement