quandl_ruby 0.1.2 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47bb3190333744823d2bd2af9451ca002fa62642
4
- data.tar.gz: 459eca5a698c9f403d82a39ead2e4ff4eb805d83
3
+ metadata.gz: c0952595cac0796e5898395d4c40c759e0340f68
4
+ data.tar.gz: beb7d47ca034889c91d39c4f442c63550d7d0911
5
5
  SHA512:
6
- metadata.gz: 614f1a25a0731764eaca03b6d53fb49ee882993389dfd5b8650bf350105bf10e7ad80ca8a17ad0895409545a837da2d3ad06b1ae3ac90f523704aa360c5d6fe6
7
- data.tar.gz: 8a2d905dc84aba3b06b3eaac2ba066a5d190ca834d80f50cf7de07154a382e4ec53cb0c1f8f8b935f6de8dfb1548ff3c7984afaf1b7e308ebf5628ac26ef53a4
6
+ metadata.gz: 16174ec2d59b403d495e1859e12f5836f7d273a69effb7a42e3c460d8f044c350dafa22520151a6e36e7016f5998d64ed2db38709ca3c6273715acc10642994c
7
+ data.tar.gz: 910439ff04926038746f133d463dc4e3ae9a8049cafc3e5573ebe0350d00ff11a1bbbfd917bc8fa72737b83bd528fa4f2bda27691b9d1b2a53ff81461377f67c
data/README.md CHANGED
@@ -57,20 +57,67 @@ All the above classes have the same API:
57
57
  # `:csv` => parsed CSV into hashes
58
58
  # `:xml` => a string of unparsed xml
59
59
  # Set the format by providing a `:format` key in the options hash
60
+ # Quandl::Class.get is an alias for Quandl::Class.new
60
61
  query = Quandl::Class.new(query, options)
61
62
 
62
- # Use instance#get to retrieve data from Quandl
63
- data = query.get
63
+ # Use instance#data to retrieve data from Quandl
64
+ data = query.data
64
65
 
65
- # Instantiating and then calling the `get` method memoizes the response
66
- # Pass in `true` to instance#get to clear the cache and reload the data
67
- data = query.get(true)
66
+ # Instantiating and then calling the `data` method memoizes the response
67
+ # Pass in `true` to instance#data to clear the cache
68
+ data = query.data(true)
69
+ ```
70
+
71
+
72
+ #### An Alternative to Passing in an Options Hash
73
+
74
+ You can also pass in options by calling methods on an instance of a `Quandl::Class`:
75
+
76
+ ```ruby
77
+ # This will make more sense if you read through:
78
+ # http://www.quandl.com/help/api#Data-Manipulation
79
+ gdp = Quandl::Dataset.get('FRED/GDP')
80
+
81
+ # Corresponds to the `rows` parameter. Aliased as #rows.
82
+ gdp.limit(20)
83
+
84
+ # Corresponds to the `sort_order` parameter. Aliased as #sort.
85
+ gdp.order(:asc) # Only accepts :asc or :desc
68
86
 
69
- # The following is a shortcut to Class.new(query, options).get,
70
- # albeit with no build-in memoization:
71
- data = Quandl::Class.get(query, options)
87
+ # Corresponds to the `column` parameter.
88
+ gdp.column(4)
89
+
90
+ # Corresponds to the `collapse` parameter. Aliased as #frequency
91
+ # Accepts: :none, :daily, :weekly, :monthly, :quarterly, :annual
92
+ gdp.collapse(:annual)
93
+
94
+ # Corresponds to the `exclude_headers` parameter.
95
+ gdp.headers(false) # (sets `exclude_headers` to `true`)
96
+
97
+ # Corresponds to the `trim_start` parameter. Also accepts an instance of Date.
98
+ gdp.start('2010-01-01')
99
+
100
+ # Corresponds to the `trim_end` parameter. Also accepts an instance of Date.
101
+ gdp.end('2014-01-01')
102
+
103
+ # Corresponds to the `transformation` parameter.
104
+ # Accepts: :diff, :rdiff, :cumul, :normalize
105
+ gdp.transform(:normalize)
106
+
107
+ # After all the above, call #metadata or #data to retrieve the respective data
108
+ gdp.metadata # => returns metadata for 'FRED/GDP'
109
+ gdp.data # => returns metadata for 'FRED/GDP'
110
+
111
+ # If you were to change an option, like below:
112
+ gdp.transform(:rdiff)
113
+
114
+ # You'll need to call #reload! or pass `true` as an argument to #data or #metadata
115
+ gdp.reload!
116
+
117
+ gdp.data
72
118
  ```
73
119
 
120
+
74
121
  ### A Simple Example
75
122
 
76
123
  ```ruby
@@ -1,35 +1,38 @@
1
1
  module Quandl
2
2
  class Dataset
3
- attr_accessor :set, :options, :data
3
+ attr_accessor :query, :options
4
4
 
5
- def self.get(params, options = {})
6
- instance = new(params, options)
7
- instance.get
8
- if block_given?
9
- yield(instance.data)
10
- else
11
- instance.data
12
- end
5
+ include Quandl::Filter
6
+
7
+ def self.get(*params)
8
+ new(*params)
13
9
  end
14
10
 
15
- def initialize(params, options = {})
16
- @set = params
11
+ def initialize(query, options = {})
12
+ @query = query
17
13
  @options = options
18
14
  end
19
15
 
20
- def get(reload = false)
21
- if !data || reload
22
- raw_data = Quandl::Request.new('datasets', {
23
- dataset: set,
24
- options: options
25
- }).get
26
- self.data = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
27
- end
28
- if block_given?
29
- yield(data)
30
- else
31
- data
16
+ [:data, :metadata].each do |data|
17
+ define_method(data) do |bust_cache = false|
18
+ cache = instance_variable_get "@#{data}"
19
+ if cache && !bust_cache
20
+ cache
21
+ else
22
+ reload!
23
+ instance_variable_get "@#{data}"
24
+ end
32
25
  end
33
26
  end
27
+
28
+ def reload!
29
+ raw_data = Quandl::Request.new('datasets', {
30
+ dataset: query,
31
+ options: options
32
+ }).get
33
+ response = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
34
+ @data = response.delete(:data)
35
+ @metadata = response
36
+ end
34
37
  end
35
38
  end
@@ -1,35 +1,18 @@
1
1
  module Quandl
2
- class Favorites
3
- attr_reader :auth_token, :options, :data
4
-
5
- def self.get(options = {})
6
- instance = new(options)
7
- instance.get
8
- if block_given?
9
- yield(instance.data)
10
- else
11
- instance.data
12
- end
13
- end
14
-
2
+ class Favorites < Quandl::Dataset
15
3
  def initialize(options = {})
16
- @auth_token = options.delete(:auth_token) || Quandl.configuration.auth_token
4
+ @query = options.delete(:auth_token) || Quandl.configuration.auth_token
17
5
  @options = options
18
6
  end
19
7
 
20
- def get(reload = false)
21
- if !data || reload
22
- raw_data = Quandl::Request.new('current_user/collections/datasets/favourites', {
23
- options: options,
24
- auth_token: auth_token
25
- }).get
26
- self.data = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
27
- end
28
- if block_given?
29
- yield(data)
30
- else
31
- data
32
- end
8
+ def reload!
9
+ raw_data = Quandl::Request.new('current_user/collections/datasets/favourites', {
10
+ auth_token: query,
11
+ options: options
12
+ }).get
13
+ response = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
14
+ @data = response.delete(:data)
15
+ @metadata = response
33
16
  end
34
17
  end
35
18
 
@@ -0,0 +1,52 @@
1
+ module Quandl
2
+ module Filter
3
+ def limit(n)
4
+ @options[:rows] = n
5
+ self
6
+ end
7
+
8
+ alias_method :rows, :limit
9
+
10
+ def order(direction)
11
+ @options[:sort_order] = direction.to_sym if [:asc, :desc].include? direction.to_sym
12
+ self
13
+ end
14
+
15
+ alias_method :sort, :order
16
+
17
+ def column(n)
18
+ @options[:column] = n
19
+ self
20
+ end
21
+
22
+ def collapse(frequency)
23
+ if [:none, :daily, :weekly, :monthly, :quarterly, :annual].include? frequency.to_sym
24
+ @options[:collapse] = frequency.to_sym
25
+ end
26
+ self
27
+ end
28
+
29
+ def transform(transformation)
30
+ if [:diff, :rdiff, :cumul, :normalize].include? transformation.to_sym
31
+ @options[:transformation] = transformation.to_sym
32
+ end
33
+ self
34
+ end
35
+
36
+ def headers(request)
37
+ @options[:exclude_headers] = !request
38
+ self
39
+ end
40
+
41
+ [:start, :end].each do |filter|
42
+ define_method(filter) do |datetime|
43
+ if datetime.is_a?(String) && match_data = datetime.match(/(\d{4}).(\d{2}).(\d{2})/)
44
+ @options["trim_#{filter}".to_sym] = match_data[1..3].join('-')
45
+ elsif datetime.is_a?(Date)
46
+ @options["trim_#{filter}".to_sym] = datetime.to_s
47
+ end
48
+ self
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,6 +1,6 @@
1
1
  module Quandl
2
2
  class Metadata < Quandl::Dataset
3
- def initialize(params = {}, options = {})
3
+ def initialize(query, options = {})
4
4
  options[:exclude_data] = true
5
5
  super
6
6
  end
@@ -1,35 +1,13 @@
1
1
  module Quandl
2
- class Multiset
3
- attr_accessor :sets, :options, :data
4
-
5
- def self.get(datasets, options = {})
6
- instance = new(datasets, options)
7
- instance.get
8
- if block_given?
9
- yield(instance.data)
10
- else
11
- instance.data
12
- end
13
- end
14
-
15
- def initialize(datasets, options = {})
16
- @sets = datasets
17
- @options = options
18
- end
19
-
20
- def get
21
- if !data || reload
22
- raw_data = Quandl::Request.new('multisets', {
23
- datasets: sets,
24
- options: options
25
- }).get
26
- self.data = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
27
- end
28
- if block_given?
29
- yield(data)
30
- else
31
- data
32
- end
2
+ class Multiset < Quandl::Dataset
3
+ def reload!
4
+ raw_data = Quandl::Request.new('multisets', {
5
+ datasets: query,
6
+ options: options
7
+ }).get
8
+ response = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
9
+ @data = response.delete(:data)
10
+ @metadata = response
33
11
  end
34
12
  end
35
13
  end
data/lib/quandl/parse.rb CHANGED
@@ -2,11 +2,9 @@ require 'json'
2
2
  require 'csv'
3
3
 
4
4
  module Quandl
5
-
6
5
  CSV::Converters[:blank_to_nil] = Proc.new do |field|
7
6
  field && field.empty? ? nil : field
8
7
  end
9
-
10
8
  def self.parse(data, format)
11
9
  case format
12
10
  when :json
data/lib/quandl/search.rb CHANGED
@@ -1,35 +1,13 @@
1
1
  module Quandl
2
- class Search
3
- attr_accessor :query, :options, :data
4
-
5
- def self.get(query, options = {})
6
- instance = new(query, options)
7
- instance.get
8
- if block_given?
9
- yield(instance.data)
10
- else
11
- instance.data
12
- end
13
- end
14
-
15
- def initialize(query, options = {})
16
- @query = query
17
- @options = options
18
- end
19
-
20
- def get
21
- if !data || reload
22
- raw_data = Quandl::Request.new('datasets', {
23
- query: query,
24
- options: options
25
- }).get
26
- self.data = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
27
- end
28
- if block_given?
29
- yield(data)
30
- else
31
- data
32
- end
2
+ class Search < Quandl::Dataset
3
+ def reload!
4
+ raw_data = Quandl::Request.new('datasets', {
5
+ query: query,
6
+ options: options
7
+ }).get
8
+ response = Quandl.parse(raw_data, (options[:format] || :json).to_sym)
9
+ @data = response.delete(:data)
10
+ @metadata = response
33
11
  end
34
12
  end
35
13
  end
@@ -1,3 +1,3 @@
1
1
  module Quandl
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
data/lib/quandl.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require_relative 'quandl/version'
2
2
  require_relative 'quandl/configuration'
3
3
  require_relative 'quandl/parse'
4
+ require_relative 'quandl/filter'
4
5
  require_relative 'quandl/dataset'
5
6
  require_relative 'quandl/metadata'
6
7
  require_relative 'quandl/multiset'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quandl_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kash Nouroozi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-20 00:00:00.000000000 Z
11
+ date: 2014-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -54,6 +54,7 @@ files:
54
54
  - lib/quandl/configuration.rb
55
55
  - lib/quandl/dataset.rb
56
56
  - lib/quandl/favorites.rb
57
+ - lib/quandl/filter.rb
57
58
  - lib/quandl/metadata.rb
58
59
  - lib/quandl/multiset.rb
59
60
  - lib/quandl/parse.rb