quandl_data 0.1.11 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,26 +1,26 @@
1
1
  # Quandl::Data
2
2
 
3
- ### Quandl::Data::Table
3
+ ### Quandl::Data
4
4
 
5
5
  ```ruby
6
6
 
7
7
  require 'quandl/data'
8
8
 
9
- decade_of_daily_data_as_csv = Quandl::Data::Random.table( rows: 3650, columns: 4, nils: false ).to_csv
9
+ decade_of_daily_data_as_csv = Quandl::Fabricate::Data.rand( rows: 3650, columns: 4, nils: false ).to_csv
10
10
 
11
- table = Quandl::Data::Table.new( decade_of_daily_data_as_csv )
11
+ table = Quandl::Data.new( decade_of_daily_data_as_csv )
12
12
  table.collapse(:monthly).transform(:rdiff).to_date
13
13
 
14
14
  ```
15
15
 
16
16
 
17
- ### Quandl::Fabricate::Data::Table
17
+ ### Quandl::Fabricate::Data
18
18
 
19
19
  ```ruby
20
20
 
21
21
  require 'quandl/fabricate/data'
22
22
 
23
- data = Quandl::Fabricate::Data::Table.rand( rows: 4, columns: 2, nils: false )
23
+ data = Quandl::Fabricate::Data.rand( rows: 4, columns: 2, nils: false )
24
24
  data.to_csv
25
25
 
26
26
  => "2456459,9.75573946698621,11.003327581245657\n2456460,9.815208080651333,11.06123640714187\n2456461,9.85055418685121,11.054083764705883\n2456462,9.915882352941177,10.96635294117647\n"
data/UPGRADE.md CHANGED
@@ -1,7 +1,14 @@
1
+ ## 1.0.0
2
+
3
+ * Quandl::Data::Table is removed. Replaced by: Quandl::Data
4
+ * Quandl::Fabricate::Data::Table is remove. Replaced by: Quandl::Fabricate::Data
5
+
6
+
1
7
  ## 0.1.11
2
8
 
3
9
  * add quandl/fabricate/data. deprecate Quandl::Data::Random.table
4
10
 
11
+
5
12
  ## 0.1
6
13
 
7
14
  * Initial
@@ -1,20 +1,15 @@
1
- require 'quandl/data/table/loggable'
2
- require 'quandl/data/table/operations'
3
-
4
- module Quandl
5
- module Data
1
+ module Quandl::Data::Enumerator
2
+ extend ActiveSupport::Concern
6
3
 
7
- class Table
8
-
9
- include Operations
10
- include Loggable
4
+ included do
5
+
6
+ delegate *Array.forwardable_methods, to: :data_array
11
7
 
12
- delegate *Array.forwardable_methods, to: :data_array
8
+ delegate :to_json, :as_json, to: :data_array
13
9
 
14
- delegate :to_json, :as_json, to: :data_array
10
+ attr_accessor :pristine_data
11
+ end
15
12
 
16
- attr_accessor :pristine_data
17
-
18
13
  def initialize(*args)
19
14
  self.pristine_data = args.first
20
15
  self.attributes = args.extract_options!
@@ -48,6 +43,4 @@ class Table
48
43
  end
49
44
  end
50
45
 
51
- end
52
- end
53
- end
46
+ end
@@ -0,0 +1,19 @@
1
+ module Quandl::Data::Logging
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ def to_h
6
+ t1 = Time.now
7
+ r = super
8
+ Quandl::Logger.debug("#{self.class.name}.to_h (#{t1.elapsed_ms})")
9
+ r
10
+ end
11
+
12
+ def to_csv
13
+ t1 = Time.now
14
+ r = super
15
+ Quandl::Logger.debug("#{self.class.name}.to_csv (#{t1.elapsed_ms})")
16
+ r
17
+ end
18
+
19
+ end
@@ -1,8 +1,4 @@
1
- module Quandl
2
- module Data
3
- class Table
4
-
5
- module Operations
1
+ module Quandl::Data::Operations
6
2
 
7
3
  extend ActiveSupport::Concern
8
4
  include Quandl::Operation
@@ -37,7 +33,7 @@ module Operations
37
33
  end
38
34
 
39
35
  def to_jd
40
- Table.new( Parse.date_to_julian( data_array ), frequency: frequency )
36
+ Quandl::Data.new( Parse.date_to_julian( data_array ), frequency: frequency )
41
37
  end
42
38
  def to_jd!
43
39
  @data_array = Parse.date_to_julian( data_array )
@@ -49,7 +45,7 @@ module Operations
49
45
  self
50
46
  end
51
47
  def to_date
52
- Table.new( Parse.julian_to_date( data_array ) )
48
+ Quandl::Data.new( Parse.julian_to_date( data_array ) )
53
49
  end
54
50
 
55
51
  def trim_start!(trim_date)
@@ -67,7 +63,7 @@ module Operations
67
63
  row_date = row[0]
68
64
  row_date < trim_date
69
65
  end
70
- Table.new(data)
66
+ Quandl::Data.new(data)
71
67
  end
72
68
 
73
69
  def trim_end!(trim_date)
@@ -85,7 +81,7 @@ module Operations
85
81
  row_date = row[0]
86
82
  row_date > trim_date
87
83
  end
88
- Table.new(data)
84
+ Quandl::Data.new(data)
89
85
  end
90
86
 
91
87
  def limit!(amount)
@@ -93,7 +89,7 @@ module Operations
93
89
  self
94
90
  end
95
91
  def limit(amount)
96
- Table.new( data_array[0..( amount.to_i - 1 )] )
92
+ Quandl::Data.new( data_array[0..( amount.to_i - 1 )] )
97
93
  end
98
94
 
99
95
  def sort_order(dir)
@@ -105,7 +101,7 @@ module Operations
105
101
  self
106
102
  end
107
103
  def sort_ascending
108
- Table.new( Parse.sort( data_array.dup, :asc ), frequency: frequency )
104
+ Quandl::Data.new( Parse.sort( data_array.dup, :asc ), frequency: frequency )
109
105
  end
110
106
 
111
107
  def sort_descending!
@@ -113,7 +109,7 @@ module Operations
113
109
  self
114
110
  end
115
111
  def sort_descending
116
- Table.new( Parse.sort( data_array.dup, :desc ), frequency: frequency )
112
+ Quandl::Data.new( Parse.sort( data_array.dup, :desc ), frequency: frequency )
117
113
  end
118
114
 
119
115
  def transform(*args)
@@ -149,8 +145,4 @@ module Operations
149
145
  data
150
146
  end
151
147
 
152
- end
153
-
154
- end
155
- end
156
148
  end
@@ -1,5 +1,5 @@
1
1
  module Quandl
2
- module Data
3
- VERSION = "0.1.11"
2
+ class Data
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
data/lib/quandl/data.rb CHANGED
@@ -7,10 +7,12 @@ require "active_support/core_ext/object"
7
7
 
8
8
  require "quandl/operation"
9
9
 
10
- require 'quandl/data/table'
11
- require 'quandl/data/random'
10
+ require 'quandl/data/enumerator'
11
+ require 'quandl/data/operations'
12
+ require 'quandl/data/logging'
12
13
 
13
- module Quandl
14
- module Data
15
- end
14
+ class Quandl::Data
15
+ include Enumerator
16
+ include Operations
17
+ include Logging if defined?(QUANDL_LOGGER) && QUANDL_LOGGER == true
16
18
  end
@@ -1,101 +1,89 @@
1
- module Quandl
2
- module Fabricate
3
- module Data
1
+ class Quandl::Fabricate::Data
4
2
 
5
- class Table
6
-
7
- class << self
3
+ def self.rand(*args)
4
+ new(*args).random
5
+ end
8
6
 
9
- def rand(*args)
10
- new(*args).random
11
- end
12
-
13
- end
7
+ attr_accessor :attributes, :offset, :rows, :columns, :frequency, :data, :nils
14
8
 
15
- attr_accessor :attributes, :offset, :rows, :columns, :frequency, :data, :nils
16
-
17
- def initialize(*args)
18
- self.attributes = default_options.merge args.extract_options!
19
- end
9
+ def initialize(*args)
10
+ self.attributes = default_options.merge args.extract_options!
11
+ end
20
12
 
21
- def to_csv(*args)
22
- data.collect(&:to_csv).join
23
- end
13
+ def to_csv(*args)
14
+ data.collect(&:to_csv).join
15
+ end
24
16
 
25
- def random_column
26
- self.columns = 1
27
- random
28
- end
17
+ def random_column
18
+ self.columns = 1
19
+ random
20
+ end
29
21
 
30
- def random
31
- data = []
32
- index = 0
33
- until data.count >= rows
34
- data << row(index) unless nils
35
- index += 1
36
- end
37
- Quandl::Data::Table.new( data ).sort_descending
22
+ def random
23
+ data = []
24
+ index = 0
25
+ until data.count >= rows
26
+ data << row(index) unless nils
27
+ index += 1
38
28
  end
29
+ Quandl::Data.new( data ).sort_descending
30
+ end
39
31
 
40
- def row(index)
41
- row = [ date(index) ]
42
- columns.times{|column_index| row << point(index, column_index) }
43
- row
44
- end
32
+ def row(index)
33
+ row = [ date(index) ]
34
+ columns.times{|column_index| row << point(index, column_index) }
35
+ row
36
+ end
45
37
 
46
- def date(index)
47
- (Date.today - ( index * ( frequencies[frequency] ) - offset )).jd
48
- end
38
+ def date(index)
39
+ (Date.today - ( index * ( frequencies[frequency] ) - offset )).jd
40
+ end
49
41
 
50
- def point(row_index, column_index)
51
- percent = ( (rand(10).to_f / 1000) ) - ( (rand(10).to_f / 850) ) + 1
52
- # increase the value
53
- trending_point[column_index] ||= column_index * column_index + 10
54
- trending_point[column_index] = trending_point[column_index] * percent
55
- # increase
56
- nils ? nil : trending_point[column_index]
57
- end
42
+ def point(row_index, column_index)
43
+ percent = ( (rand(10).to_f / 1000) ) - ( (rand(10).to_f / 850) ) + 1
44
+ # increase the value
45
+ trending_point[column_index] ||= column_index * column_index + 10
46
+ trending_point[column_index] = trending_point[column_index] * percent
47
+ # increase
48
+ nils ? nil : trending_point[column_index]
49
+ end
58
50
 
59
- def trending_point
60
- @trending_point ||= {}
61
- end
51
+ def trending_point
52
+ @trending_point ||= {}
53
+ end
62
54
 
63
- def nils
64
- @nils == false ? false : rand(6) == 1
65
- end
55
+ def nils
56
+ @nils == false ? false : rand(6) == 1
57
+ end
66
58
 
67
- def frequencies
68
- {daily: 1, weekly: 7, monthly: 30, quarterly: 90, annual: 365}
69
- end
59
+ def frequencies
60
+ {daily: 1, weekly: 7, monthly: 30, quarterly: 90, annual: 365}
61
+ end
70
62
 
71
- def default_options
72
- {
73
- offset: 1,
74
- rows: 20 + rand(100),
75
- columns: 2 + rand(4),
76
- frequency: :daily,
77
- monkey: 6
78
- }
79
- end
63
+ def default_options
64
+ {
65
+ offset: 1,
66
+ rows: 20 + rand(100),
67
+ columns: 2 + rand(4),
68
+ frequency: :daily,
69
+ monkey: 6
70
+ }
71
+ end
80
72
 
81
- def attributes
82
- @attributes ||= {}
83
- end
73
+ def attributes
74
+ @attributes ||= {}
75
+ end
84
76
 
85
- def attributes=(attrs)
86
- assign_attributes(attrs)
87
- attributes
88
- end
77
+ def attributes=(attrs)
78
+ assign_attributes(attrs)
79
+ attributes
80
+ end
89
81
 
90
- # mass assignment protection
91
- def assign_attributes(attrs)
92
- attrs.each do |name, value|
93
- self.send("#{name}=", value) if self.respond_to?("#{name}=")
94
- end
82
+ # mass assignment protection
83
+ def assign_attributes(attrs)
84
+ attrs.each do |name, value|
85
+ self.send("#{name}=", value) if self.respond_to?("#{name}=")
95
86
  end
96
-
97
87
  end
98
-
99
- end
100
- end
88
+
101
89
  end
@@ -1,6 +1,5 @@
1
- require 'quandl/fabricate/data'
2
-
3
1
  module Quandl
4
- module Fabricate
5
- end
2
+ module Fabricate
3
+ require 'quandl/fabricate/data'
4
+ end
6
5
  end
@@ -1,12 +1,12 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
- describe Quandl::Fabricate::Data::Table do
4
+ describe Quandl::Fabricate::Data do
5
5
 
6
- let(:data){ Quandl::Fabricate::Data::Table.rand( rows: 4, columns: 5, nils: false ) }
6
+ let(:data){ Quandl::Fabricate::Data.rand( rows: 4, columns: 5, nils: false ) }
7
7
  subject{ data }
8
8
 
9
- it{ should be_a Quandl::Data::Table }
9
+ it{ should be_a Quandl::Data }
10
10
  its(:count){ should eq 4 }
11
11
 
12
12
  describe "#first" do
@@ -1,14 +1,14 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
- describe Quandl::Data::Table do
5
- subject { Quandl::Data::Random.table( nils: false, rows: 4, columns: 4 ) }
4
+ describe Quandl::Data do
5
+ subject { Quandl::Fabricate::Data.rand( nils: false, rows: 4, columns: 4 ) }
6
6
 
7
7
  its(:to_h){ should be_a Hash }
8
8
  its(:count){ should eq 4 }
9
9
 
10
10
  it "should parse csv" do
11
- Quandl::Data::Table.new(subject.to_csv).count.should eq 4
11
+ Quandl::Data.new(subject.to_csv).count.should eq 4
12
12
  end
13
13
 
14
14
  it "should convert to julian dates" do
@@ -23,14 +23,19 @@ describe Quandl::Data::Table do
23
23
  date_data.first.first.should be_a Date
24
24
  end
25
25
 
26
- it "should transform the data" do
27
- value = subject.first[1]
28
- subject.transform(:rdiff).first[1].should_not eq value
29
- end
30
-
31
- it "should transform the data to rdiff from" do
32
- data = Quandl::Data::Table.new([[1,3,5],[4,5,4],[5,15,20]])
33
- data.transform(:rdiff_from).should eq [[1,4,3],[4,2,4],[5,0,0]]
26
+ describe "#transform" do
27
+ it "should rdiff" do
28
+ value = subject.first[1]
29
+ subject.transform(:rdiff).first[1].should_not eq value
30
+ end
31
+ it "should rdiff_from" do
32
+ data = Quandl::Data.new([[1,3,5],[4,5,4],[5,15,20]])
33
+ data.transform(:rdiff_from).should eq [[1,4,3],[4,2,4],[5,0,0]]
34
+ end
35
+ it "should cumul" do
36
+ data = Quandl::Data.new( [[1000, 10], [1001, 20], [1002, 30]] )
37
+ data.transform(:cumul).should eq [[1000, 10], [1001, 30], [1002, 60]]
38
+ end
34
39
  end
35
40
 
36
41
  it "should collapse the data" do
@@ -50,8 +55,8 @@ describe Quandl::Data::Table do
50
55
  data.should eq subject
51
56
  end
52
57
 
53
- it "should output csv when initialized with a Data::Table" do
54
- Quandl::Data::Table.new( subject ).to_csv.should eq subject.to_csv
58
+ it "should output csv when initialized with a Quandl::Data" do
59
+ Quandl::Data.new( subject ).to_csv.should eq subject.to_csv
55
60
  end
56
61
 
57
62
  end
@@ -79,7 +84,7 @@ describe Quandl::Data::Table do
79
84
  describe "trim_end" do
80
85
 
81
86
  it "should delete everything after trim_end" do
82
- data = Quandl::Data::Random.table( nils: false, rows: 10, columns: 1 ).sort_descending
87
+ data = Quandl::Fabricate::Data.rand( nils: false, rows: 10, columns: 1 ).sort_descending
83
88
  date = data[1][0]
84
89
  data.trim_end!(date)
85
90
  data.data_array.should be_a Array
@@ -105,7 +110,7 @@ describe Quandl::Data::Table do
105
110
  describe "trim_start" do
106
111
 
107
112
  it "should delete everything before trim_start" do
108
- data = Quandl::Data::Random.table( nils: false, rows: 10, columns: 1 ).sort_descending
113
+ data = Quandl::Fabricate::Data.rand( nils: false, rows: 10, columns: 1 ).sort_descending
109
114
  date = data[-2][0]
110
115
  data.trim_start(date).count.should eq 9
111
116
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quandl_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-23 00:00:00.000000000 Z
12
+ date: 2013-10-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -140,10 +140,9 @@ files:
140
140
  - Rakefile
141
141
  - UPGRADE.md
142
142
  - lib/quandl/data.rb
143
- - lib/quandl/data/random.rb
144
- - lib/quandl/data/table.rb
145
- - lib/quandl/data/table/loggable.rb
146
- - lib/quandl/data/table/operations.rb
143
+ - lib/quandl/data/enumerator.rb
144
+ - lib/quandl/data/logging.rb
145
+ - lib/quandl/data/operations.rb
147
146
  - lib/quandl/data/version.rb
148
147
  - lib/quandl/fabricate.rb
149
148
  - lib/quandl/fabricate/data.rb
@@ -1,16 +0,0 @@
1
- require 'quandl/data/table/operations'
2
-
3
- module Quandl
4
- module Data
5
-
6
- class Random
7
-
8
- def self.table(*args)
9
- warn "[DEPRECATION] `Quandl::Data::Random.table` is deprecated. Please use `Quandl::Fabricate::Data::Table` instead."
10
- Quandl::Fabricate::Data::Table.rand(*args)
11
- end
12
-
13
- end
14
-
15
- end
16
- end
@@ -1,21 +0,0 @@
1
- module Quandl
2
- module Data
3
- class Table
4
-
5
- module Loggable
6
-
7
- extend ActiveSupport::Concern
8
-
9
- def to_h
10
- Quandl::Logger.info_with_elapsed("Quandl::Data::Table.to_h"){ super }
11
- end
12
-
13
- def to_csv
14
- Quandl::Logger.info_with_elapsed("Quandl::Data::Table.to_csv"){ super }
15
- end
16
-
17
- end
18
-
19
- end
20
- end
21
- end