quandl_data 1.3.9 → 1.4.0

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: c3c2e3eed2e151a72e4c55bde8bf974c69f453b8
4
- data.tar.gz: ec9f4f6ee49b8c219d977c87cb594f406e837116
3
+ metadata.gz: 8ba35c6c066aacfe1f15e4007e95e2573594d454
4
+ data.tar.gz: 7d7418cf6699c656c2495b5621db5e700adfc794
5
5
  SHA512:
6
- metadata.gz: 13d6f8131b81c6f11bca78fd004be321db1e5e744ff829afecf0c535805e15ad27bd848b016400c96f39b3ac1d881f4977a32da5a05e63ba4d4f8ea1750e7cb5
7
- data.tar.gz: 0f49c42c92da222960accbbe615f2d4631bee98f44ed9a3c551e72e81fab0810fb09545cef844f4f757293d031d962098170f9af5db3649bcd57ae204b4b6243
6
+ metadata.gz: 3801aa3d4c9b908fe7ebd532341e1e797f0882242472e18cfdf45a1b05df30fa16927dd36621ceb01d9e1375c643d7f46eaf51647ef98aea8093781c1769807c
7
+ data.tar.gz: fa211ba94b2a348724a15bb9a0ab89fc04fcbb69e98b28c633a075eaabd48e6a7d53a94c099e04554ecc3575e5c1da7c80e860947174e56946d710a8baa29e86
data/Rakefile CHANGED
@@ -2,6 +2,10 @@ require "bundler"
2
2
  require "rake"
3
3
  require "bundler/gem_tasks"
4
4
  require "rspec/core/rake_task"
5
+ require 'quandl/data'
6
+ require 'pry'
7
+
8
+ require 'quandl/utility/rake_tasks'
5
9
 
6
10
  task :default => :spec
7
11
 
@@ -9,3 +13,14 @@ desc "Run all specs"
9
13
  RSpec::Core::RakeTask.new(:spec) do |task|
10
14
  task.pattern = "spec/**/*_spec.rb"
11
15
  end
16
+
17
+ task :console do |t,args|
18
+ binding.pry
19
+ end
20
+
21
+ Quandl::Utility::Tasks.configure do |c|
22
+ c.name = 'quandl_data'
23
+ c.version_path = 'VERSION'
24
+ c.changelog_path = 'UPGRADE.md'
25
+ c.changelog_matching = ['^QUGC','^WIKI']
26
+ end
data/UPGRADE.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 1.4.0
2
+
3
+ * QUGC-104 refactored Quandl::Data to use valid? and errors.
4
+
5
+
6
+
1
7
  ## 1.3.9
2
8
 
3
9
  * add malformed_csv spec and error catching
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.4.0
@@ -1,19 +1,13 @@
1
1
  require "quandl/data/version"
2
2
 
3
- require "active_support"
4
- require "active_support/inflector"
5
- require "active_support/core_ext/hash"
6
- require "active_support/core_ext/object"
3
+ require 'quandl/support'
7
4
 
8
5
  require "quandl/operation"
9
-
10
6
  require 'quandl/babelfish'
11
-
12
7
  require 'quandl/error/date_parse_error'
13
8
 
14
9
  require 'quandl/data/attributes'
15
10
  require 'quandl/data/cleaning'
16
- require 'quandl/data/enumerator'
17
11
  require 'quandl/data/operations'
18
12
  require 'quandl/data/format'
19
13
  require 'quandl/data/validations'
@@ -23,7 +17,6 @@ module Quandl
23
17
  class Data
24
18
  include Attributes
25
19
  include Cleaning
26
- include Enumerator
27
20
  include Operations
28
21
  include Validations
29
22
  include Logging if defined?(QUANDL_LOGGER) && QUANDL_LOGGER == true
@@ -4,33 +4,33 @@ module Attributes
4
4
 
5
5
  extend ActiveSupport::Concern
6
6
 
7
- def initialize(*args)
8
- self.pristine_data = args.first
9
- self.attributes = args.extract_options!
10
- end
11
-
12
- def headers=(value)
13
- @headers = value.flatten if value.kind_of?(Array)
14
- end
15
- def headers
16
- ensure_data_is_cleaned
17
- @headers
18
- end
19
- def headers?
20
- headers.present?
7
+ included do
8
+
9
+ include Quandl::Support::Attributes
10
+
11
+ define_attributes :headers, :pristine_data, :cleaned, :data_array
12
+
13
+ delegate *Array.forwardable_methods, to: :data_array
14
+ delegate :to_json, :as_json, to: :data_array
15
+
16
+ def headers=(value)
17
+ write_attribute(:headers, Array(value).flatten )
18
+ end
19
+
20
+ def cleaned?
21
+ read_attribute(:cleaned) == true
22
+ end
23
+
21
24
  end
22
25
 
23
- def attributes
24
- @attributes ||= {}
25
- end
26
- def attributes=(attrs)
27
- assign_attributes(attrs)
28
- attributes
29
- end
30
- def assign_attributes(attrs)
31
- attrs.each do |name, value|
32
- self.send("#{name}=", value) if self.respond_to?("#{name}=")
26
+ def initialize(*args)
27
+ # passed an instance of quandl data?
28
+ if args.first.kind_of?(Quandl::Data)
29
+ @attributes = args.first.attributes
30
+ elsif args.first.present?
31
+ self.pristine_data = args.first
33
32
  end
33
+ valid? unless cleaned?
34
34
  end
35
35
 
36
36
  end
@@ -4,23 +4,10 @@ module Cleaning
4
4
 
5
5
  extend ActiveSupport::Concern
6
6
 
7
- def cleaned
8
- @cleaned
9
- end
10
- def cleaned=(value)
11
- @cleaned = (value == true)
12
- end
13
- def cleaned?
14
- cleaned == true
15
- end
16
-
17
- def ensure_data_is_cleaned
18
- data_array unless cleaned?
19
- end
20
-
21
7
  protected
22
8
 
23
9
  def clean(data)
10
+ data = data.dup if data.is_a?(Array) &&data.respond_to?(:dup)
24
11
  # check if data is dirty
25
12
  requires_cleaning = ensure_data_requires_cleaning(data)
26
13
  # short ciruit unless data is dirty
@@ -34,12 +21,6 @@ module Cleaning
34
21
  def ensure_data_requires_cleaning(data)
35
22
  # skip cleaning if already clean
36
23
  return data if data.kind_of?(Array) && cleaned?
37
- # Quandl::Data is already clean, but to avoid errors extract internal array
38
- if data.kind_of?(Quandl::Data)
39
- # extract headers if present
40
- self.headers = data.headers
41
- return data.to_date.to_a
42
- end
43
24
  # Return empty array if given empty string, nil, etc.
44
25
  return [] if data.blank?
45
26
  # data requires cleaning
@@ -43,22 +43,22 @@ module Operations
43
43
  end
44
44
 
45
45
  def to_jd!
46
- @data_array = Quandl::Data::Format.to_jd( data_array ); self
46
+ self.data_array = Quandl::Data::Format.to_jd( data_array ); self
47
47
  end
48
48
 
49
49
  def to_date!
50
- @data_array = Quandl::Data::Format.to_date( data_array ); self
50
+ self.data_array = Quandl::Data::Format.to_date( data_array ); self
51
51
  end
52
52
 
53
53
  def to_date_str!
54
- @data_array = to_date!.collect{|r| r = r.dup; r[0] = r[0].to_s; r }; self
54
+ self.data_array = to_date!.collect{|r| r = r.dup; r[0] = r[0].to_s; r }; self
55
55
  end
56
56
 
57
57
  def trim_start!(date)
58
58
  # date format
59
59
  date = Quandl::Operation::QDate.parse(date)
60
60
  # reject rows with dates less than
61
- @data_array = to_date!.sort_descending!.delete_if do |row|
61
+ self.data_array = to_date!.sort_descending!.delete_if do |row|
62
62
  row_date = row[0]
63
63
  row_date < date
64
64
  end
@@ -69,7 +69,7 @@ module Operations
69
69
  # date format
70
70
  date = Quandl::Operation::QDate.parse(date)
71
71
  # reject rows with dates less than
72
- @data_array = to_date!.sort_descending!.delete_if do |row|
72
+ self.data_array = to_date!.sort_descending!.delete_if do |row|
73
73
  row_date = row[0]
74
74
  row_date > date
75
75
  end
@@ -77,7 +77,7 @@ module Operations
77
77
  end
78
78
 
79
79
  def limit!(amount)
80
- @data_array = data_array[0..( amount.to_i - 1 )]; self
80
+ self.data_array = data_array[0..( amount.to_i - 1 )]; self
81
81
  end
82
82
 
83
83
  def sort_order(dir)
@@ -85,21 +85,21 @@ module Operations
85
85
  end
86
86
 
87
87
  def sort_ascending!
88
- @data_array = Quandl::Operation::Sort.asc( data_array ); self
88
+ self.data_array = Quandl::Operation::Sort.asc( data_array ); self
89
89
  end
90
90
 
91
91
  def sort_descending!
92
- @data_array = Quandl::Operation::Sort.desc( data_array ); self
92
+ self.data_array = Quandl::Operation::Sort.desc( data_array ); self
93
93
  end
94
94
 
95
95
  def to_precision!(value)
96
- @data_array = Quandl::Operation::Value.precision(data_array, value); self
96
+ self.data_array = Quandl::Operation::Value.precision(data_array, value); self
97
97
  end
98
98
 
99
99
  def row(*args)
100
100
  return @row if args[0].nil?
101
101
  @row = args[0]
102
- @data_array = [data_array[ args[0] ]]
102
+ self.data_array = [data_array[ args[0] ]]
103
103
  self
104
104
  end
105
105
 
@@ -111,8 +111,8 @@ module Operations
111
111
  def transform=(value)
112
112
  return false unless Quandl::Operation::Transform.valid?(value)
113
113
  @transform = value
114
- @data_array = Quandl::Operation::Transform.perform( data_array, value )
115
- @data_array
114
+ self.data_array = Quandl::Operation::Transform.perform( data_array, value )
115
+ self.data_array
116
116
  end
117
117
 
118
118
  def collapse(*args)
@@ -124,7 +124,7 @@ module Operations
124
124
  return false unless Quandl::Operation::Collapse.valid?(collapse)
125
125
  @collapse = collapse
126
126
  @frequency = collapse
127
- @data_array = Quandl::Operation::Collapse.perform( data_array, collapse )
127
+ self.data_array = Quandl::Operation::Collapse.perform( data_array, collapse )
128
128
  end
129
129
 
130
130
  def frequency
@@ -135,7 +135,7 @@ module Operations
135
135
  end
136
136
 
137
137
  def clone
138
- self.class.new( data_array.dup, headers: headers, cleaned: cleaned )
138
+ self.class.new( _attributes: attributes.clone )
139
139
  end
140
140
 
141
141
  def to_precision(value)
@@ -2,40 +2,32 @@ module Quandl
2
2
  class Data
3
3
  module Validations
4
4
 
5
- extend ActiveSupport::Concern
6
-
7
- module ClassMethods
5
+ ERROR_TYPES = [
6
+ Error::GuessDateFormat, Error::InvalidDate,
7
+ Error::UnknownDateFormat, Error::DateParseError,
8
+ CSV::MalformedCSVError ]
8
9
 
9
- end
10
+ extend ActiveSupport::Concern
10
11
 
11
12
  included do
12
- ERROR_TYPES = [
13
- Error::GuessDateFormat, Error::InvalidDate,
14
- Error::UnknownDateFormat, Error::DateParseError,
15
- CSV::MalformedCSVError ]
16
- end
17
13
 
18
- def valid?
19
- raise_error_unless_valid!
20
- rescue *ERROR_TYPES => error
21
- self.errors << error
22
- self.data_array = []
23
- false
14
+ include ActiveModel::Validations
15
+
16
+ validate :data_should_be_clean!
24
17
  end
25
18
 
26
- def raise_error_unless_valid!
27
- self.errors = []
28
- self.data_array = pristine_data
19
+ def data_should_be_clean!
20
+ self.cleaned = false
21
+ self.data_array = clean(pristine_data)
29
22
  true
23
+
24
+ rescue *ERROR_TYPES => err
25
+ self.errors.add( :data, err.to_s )
26
+ self.data_array = []
27
+ false
28
+
30
29
  end
31
30
 
32
- def errors
33
- @errors ||= []
34
- end
35
- def errors=(value)
36
- @errors = value
37
- end
38
-
39
31
  end
40
32
  end
41
33
  end
@@ -1,5 +1,5 @@
1
1
  module Quandl
2
2
  class Data
3
- VERSION = "1.3.9"
3
+ VERSION = File.read(File.expand_path(File.join(File.dirname(__FILE__), '../../../VERSION'))).strip.rstrip
4
4
  end
5
5
  end
@@ -0,0 +1 @@
1
+ require 'quandl/support/attributes'
@@ -0,0 +1,82 @@
1
+ require 'active_model'
2
+ require "active_support/all"
3
+
4
+ module Quandl
5
+ module Support
6
+ module Attributes
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ extend ActiveModel::Callbacks
12
+ define_model_callbacks :initialize, only: [:after]
13
+ end
14
+
15
+ module ClassMethods
16
+
17
+ def inherited(subclass)
18
+ subclass.define_attributes(*attributes)
19
+ subclass.formatters = formatters.dup
20
+ end
21
+
22
+ def define_attributes(*attribute_names)
23
+ attribute_names.each do |key|
24
+ define_attribute(key)
25
+ end
26
+ end
27
+
28
+ def attributes
29
+ @attributes ||= []
30
+ end
31
+
32
+ protected
33
+
34
+ def define_attribute(key)
35
+ key = key.to_s
36
+ attributes << key unless attributes.include?(key)
37
+ define_method( key ){ read_attribute(key) }
38
+ define_method( "#{key}=" ){ |value| write_attribute(key, value) }
39
+ define_method( "#{key}?" ){ !read_attribute(key).nil? }
40
+ end
41
+
42
+ end
43
+
44
+ def initialize(*args)
45
+ run_callbacks(:initialize) do
46
+ attrs = args.extract_options!
47
+ # apply _attributes directly to @attributes
48
+ @attributes = attrs[:_attributes].is_a?(Hash) ? attrs.delete(:_attributes) : {}
49
+ # apply attrs through write_attribute
50
+ self.attributes = attrs if attrs.is_a?(Hash)
51
+ # onwards
52
+ super(*args) if defined?(super)
53
+ end
54
+ end
55
+
56
+ def attributes=(new_attrs)
57
+ new_attrs.stringify_keys.each do |attr_key, attr_value|
58
+ # skip those attributes that are not defined
59
+ next unless self.class.attributes.include?(attr_key) && self.respond_to?("#{attr_key}=")
60
+ # pass to the attribute writer
61
+ self.send( "#{attr_key}=", attr_value )
62
+ end
63
+ end
64
+
65
+ def attributes
66
+ @attributes ||= {}
67
+ end
68
+
69
+ protected
70
+
71
+ def read_attribute(key)
72
+ @attributes[key.to_s]
73
+ end
74
+
75
+ def write_attribute(key, value)
76
+ @attributes ||= {}
77
+ @attributes[key.to_s] = value
78
+ end
79
+
80
+ end
81
+ end
82
+ end
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
 
20
20
  s.add_runtime_dependency "quandl_operation", "~> 0.3"
21
21
  s.add_runtime_dependency "quandl_babelfish", "~> 0.0"
22
+ s.add_runtime_dependency "activemodel", ">= 3.0.0"
22
23
 
23
24
  s.add_development_dependency "rake", "~> 10.0"
24
25
  s.add_development_dependency "rspec", "~> 2.13"
@@ -27,4 +28,5 @@ Gem::Specification.new do |s|
27
28
  s.add_development_dependency "simplecov"
28
29
  s.add_development_dependency "guard"
29
30
  s.add_development_dependency "guard-rspec"
31
+ s.add_development_dependency "quandl_utility"
30
32
  end
@@ -10,7 +10,7 @@ describe Quandl::Data::Enumerator do
10
10
  its(:count){ should eq 4 }
11
11
 
12
12
  describe "#==" do
13
- it{ should eq data.data_array }
13
+ it{ should eq data.to_a }
14
14
  end
15
15
 
16
16
  [nil, :to_jd, :to_date].each do |date_format|
@@ -86,13 +86,13 @@ describe Quandl::Data::Operations do
86
86
 
87
87
  describe "#to_json" do
88
88
  it "should convert the data_array to json and back" do
89
- JSON.parse(subject.to_json).should eq JSON.parse(subject.data_array.to_json)
89
+ JSON.parse(subject.to_json).should eq JSON.parse(subject.send(:data_array).to_json)
90
90
  end
91
91
  end
92
92
 
93
93
  describe "#as_json" do
94
94
  it "should be a representation of data_array" do
95
- subject.as_json.should eq subject.data_array.as_json
95
+ subject.as_json.should eq subject.send(:data_array).as_json
96
96
  end
97
97
  end
98
98
 
@@ -102,7 +102,7 @@ describe Quandl::Data::Operations do
102
102
  data = Quandl::Fabricate::Data.rand( nils: false, rows: 10, columns: 1 ).sort_descending
103
103
  date = data[1][0]
104
104
  data.trim_end!(date)
105
- data.data_array.should be_a Array
105
+ data.send(:data_array).should be_a Array
106
106
  data.count.should eq 9
107
107
  end
108
108
 
@@ -50,7 +50,7 @@ describe Quandl::Data do
50
50
  subject{ Quandl::Data.new(malformed_csv) }
51
51
  before(:each){ subject.valid? }
52
52
  its(:valid?){ should be_false }
53
- its('errors.to_s'){ should eq "[#<CSV::MalformedCSVError: Illegal quoting in line 1.>]" }
53
+ its('errors.messages'){ should eq({:data=>["Illegal quoting in line 1."]}) }
54
54
  end
55
55
 
56
56
  [:to_jd, :sort_ascending, :sort_descending, :to_date_str].each do |o1|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quandl_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.9
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Hilscher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-05 00:00:00.000000000 Z
11
+ date: 2014-03-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: quandl_operation
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activemodel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +150,20 @@ dependencies:
136
150
  - - '>='
137
151
  - !ruby/object:Gem::Version
138
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: quandl_utility
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
139
167
  description: Data will be interfaced
140
168
  email:
141
169
  - blake@hilscher.ca
@@ -153,10 +181,10 @@ files:
153
181
  - README.md
154
182
  - Rakefile
155
183
  - UPGRADE.md
184
+ - VERSION
156
185
  - lib/quandl/data.rb
157
186
  - lib/quandl/data/attributes.rb
158
187
  - lib/quandl/data/cleaning.rb
159
- - lib/quandl/data/enumerator.rb
160
188
  - lib/quandl/data/format.rb
161
189
  - lib/quandl/data/logging.rb
162
190
  - lib/quandl/data/operations.rb
@@ -165,6 +193,8 @@ files:
165
193
  - lib/quandl/error/date_parse_error.rb
166
194
  - lib/quandl/fabricate.rb
167
195
  - lib/quandl/fabricate/data.rb
196
+ - lib/quandl/support.rb
197
+ - lib/quandl/support/attributes.rb
168
198
  - quandl_data.gemspec
169
199
  - spec/lib/fabricate/data_spec.rb
170
200
  - spec/lib/quandl/data/enumerator_spec.rb
@@ -1,24 +0,0 @@
1
- module Quandl::Data::Enumerator
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- delegate *Array.forwardable_methods, to: :data_array
6
-
7
- delegate :to_json, :as_json, to: :data_array
8
-
9
- attr_accessor :pristine_data
10
- end
11
-
12
- def data_array
13
- @data_array ||= clean( pristine_data )
14
- end
15
-
16
- def data_array=(data)
17
- @data_array = clean(data)
18
- end
19
-
20
- def data_array?
21
- @data_array.present?
22
- end
23
-
24
- end