quandl_format 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 +4 -4
- data/Gemfile +11 -0
- data/examples/load.rb +9 -0
- data/lib/quandl/format/dataset/attributes.rb +0 -11
- data/lib/quandl/format/dataset/load.rb +30 -12
- data/lib/quandl/format/version.rb +1 -1
- data/spec/fixtures/data/invalid_yaml.qdf +20 -0
- data/spec/fixtures/format.rb +8 -8
- data/spec/lib/quandl/format/dataset/errors_spec.rb +14 -19
- data/spec/lib/quandl/format/dataset/load_spec.rb +1 -1
- data/spec/lib/quandl/format/dataset_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6a154e15469886cd35f5722374918456c299200
|
4
|
+
data.tar.gz: f5b673bb2619677b9610bd0820feac55c1969bba
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dd7857d5d4d3b62ef46e2fbd5951f9e4106c7e47b0b24ff20c19caf66e04b2cecae1a4c39ecbb6fa485badb23262e9b761616596047d38781b4efb0fdd0220d
|
7
|
+
data.tar.gz: 2b3b289b1db9c084e4edf2b6d00e320c65c5ce4193e1cd7b286aa1fb6adeb6d5d6fb7a50c3006776783a4f7dd0215665a2463f2b8eb886f0f07d8f241b592a6e
|
data/Gemfile
CHANGED
@@ -1,2 +1,13 @@
|
|
1
1
|
source "https://rubygems.org"
|
2
2
|
gemspec
|
3
|
+
|
4
|
+
use_local_gems = ENV['BUNDLE_LOCAL_GEMS'] == "true" && ENV['BUNDLE_LOCAL_DIR']
|
5
|
+
local_gem_dir = ENV['BUNDLE_LOCAL_DIR']
|
6
|
+
|
7
|
+
if use_local_gems
|
8
|
+
# gem 'her', path: "#{local_gem_dir}/her"
|
9
|
+
gem 'quandl_client', path: "#{local_gem_dir}/quandl/client"
|
10
|
+
gem 'quandl_data', path: "#{local_gem_dir}/quandl/data"
|
11
|
+
gem 'quandl_operation', path: "#{local_gem_dir}/quandl/operation"
|
12
|
+
gem 'quandl_babelfish', path: "#{local_gem_dir}/quandl/babelfish"
|
13
|
+
end
|
data/examples/load.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'quandl/format'
|
3
|
+
|
4
|
+
Quandl::Logger.use Quandl::Logger::Outputs
|
5
|
+
|
6
|
+
Quandl::Format::Dataset.load_from_file('spec/fixtures/data/invalid_data.qdf')
|
7
|
+
Quandl::Format::Dataset.load_from_file('spec/fixtures/data/invalid_yaml.qdf')
|
8
|
+
Quandl::Format::Dataset.load_from_file('spec/fixtures/data/mismatched_columns.qdf')
|
9
|
+
Quandl::Format::Dataset.load_from_file('spec/fixtures/data/mismatched_rows.qdf')
|
@@ -82,17 +82,6 @@ module Attributes
|
|
82
82
|
self.class.attribute_names.inject({}){|m,k| m[k] = self.send(k); m }
|
83
83
|
end
|
84
84
|
|
85
|
-
def inspect
|
86
|
-
attrs = attributes.collect do |key, value|
|
87
|
-
if value.is_a?(String)
|
88
|
-
value = "#{value[0..20]}..." if value.length > 20
|
89
|
-
value = "'#{value}'"
|
90
|
-
end
|
91
|
-
"#{key}: #{value}"
|
92
|
-
end
|
93
|
-
%Q{<##{self.class.name} #{attrs.join(', ')}>}
|
94
|
-
end
|
95
|
-
|
96
85
|
protected
|
97
86
|
|
98
87
|
def data_rows_should_have_equal_columns!
|
@@ -20,7 +20,10 @@ class Quandl::Format::Dataset::Load
|
|
20
20
|
def parse_string(input)
|
21
21
|
nodes = []
|
22
22
|
section_type = :data
|
23
|
+
line_index = 0
|
23
24
|
input.each_line do |rline|
|
25
|
+
# track current line index
|
26
|
+
line_index += 1
|
24
27
|
# strip whitespace
|
25
28
|
line = rline.strip.rstrip
|
26
29
|
# ignore comments and blank lines
|
@@ -30,7 +33,7 @@ class Quandl::Format::Dataset::Load
|
|
30
33
|
if line =~ attribute_format
|
31
34
|
# if we are leaving the data section
|
32
35
|
# then this is the start of a new node
|
33
|
-
nodes << { attributes: '', data: '' } if section_type == :data
|
36
|
+
nodes << { attributes: '', data: '', line: line_index } if section_type == :data
|
34
37
|
# update the section to attributes
|
35
38
|
section_type = :attributes
|
36
39
|
|
@@ -49,35 +52,50 @@ class Quandl::Format::Dataset::Load
|
|
49
52
|
end
|
50
53
|
|
51
54
|
def parse_yaml_and_csv(nodes)
|
52
|
-
|
55
|
+
output = []
|
56
|
+
nodes.each do |node|
|
53
57
|
# parse attrs as yaml
|
54
|
-
node[:attributes] =
|
58
|
+
node[:attributes] = parse_yaml_attributes(node)
|
59
|
+
# we cant continue unless attributes are present
|
60
|
+
next if node[:attributes].blank?
|
55
61
|
# parse data as csv
|
56
62
|
node[:attributes][:data] = CSV.parse(node[:data])
|
57
|
-
|
63
|
+
# onwards
|
64
|
+
output << node
|
58
65
|
end
|
66
|
+
output
|
59
67
|
end
|
60
68
|
|
61
69
|
def nodes_to_datasets(nodes)
|
62
70
|
datasets = []
|
63
|
-
nodes.
|
64
|
-
dataset = node_to_dataset(node
|
71
|
+
nodes.each do |node|
|
72
|
+
dataset = node_to_dataset(node)
|
65
73
|
datasets << dataset if dataset
|
66
74
|
end
|
67
75
|
datasets
|
68
76
|
end
|
69
77
|
|
70
|
-
def
|
78
|
+
def parse_yaml_attributes(node)
|
79
|
+
YAML.load( node[:attributes] ).symbolize_keys!
|
80
|
+
rescue => e
|
81
|
+
message = "Error: Dataset starting at line #{node[:line]}\n"
|
82
|
+
message += "#{$!}\n"
|
83
|
+
message += "--"
|
84
|
+
Quandl::Logger.error(message)
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
|
88
|
+
def node_to_dataset(node)
|
71
89
|
Quandl::Format::Dataset.new( node[:attributes] )
|
72
|
-
rescue => e
|
73
|
-
message =
|
90
|
+
rescue => e
|
91
|
+
message = ''
|
74
92
|
message += node[:attributes][:source_code] + '/' if node[:attributes][:source_code].present?
|
75
|
-
message += node[:attributes][:code] +
|
93
|
+
message += node[:attributes][:code] + ' '
|
94
|
+
message += "error around line #{node[:line]} \n"
|
76
95
|
message += "#{$!}\n"
|
77
96
|
message += "--"
|
78
|
-
Quandl::Logger.error(message
|
97
|
+
Quandl::Logger.error(message)
|
79
98
|
nil
|
80
|
-
# raise $!, message, $!.backtrace
|
81
99
|
end
|
82
100
|
|
83
101
|
def attribute_format
|
@@ -0,0 +1,20 @@
|
|
1
|
+
name: "A new title"
|
2
|
+
code "test"
|
3
|
+
description: "The description Date, Open, High"
|
4
|
+
private: false
|
5
|
+
-
|
6
|
+
2013-11-22,1252.0,454.95,448.2,450.0,450.0,1354405.0,6099.41
|
7
|
+
|
8
|
+
name: "A new title"
|
9
|
+
code: "test"
|
10
|
+
description: "The description Date, Open, High"
|
11
|
+
private: false
|
12
|
+
-
|
13
|
+
2013-11-22,1252.0,454.95,448.2,450.0,450.0,1354405.0,6099.41
|
14
|
+
|
15
|
+
name: "A new title"
|
16
|
+
description: "The description Date, Open, High"
|
17
|
+
private: false
|
18
|
+
asomjkl
|
19
|
+
-
|
20
|
+
2013-11-22,1252.0,454.95,448.2,450.0,450.0,1354405.0,6099.41
|
data/spec/fixtures/format.rb
CHANGED
@@ -19,8 +19,8 @@ description: |-
|
|
19
19
|
This is the second line.
|
20
20
|
-
|
21
21
|
Date, Value, High, Low
|
22
|
-
2013-11-20,9.
|
23
|
-
2013-11-19,10.
|
22
|
+
2013-11-20,9.99,11,14
|
23
|
+
2013-11-19,10.03,,14.09
|
24
24
|
|
25
25
|
# Second dataset
|
26
26
|
code: DATASET_CODE_2
|
@@ -29,9 +29,9 @@ name: Test Dataset Name 2
|
|
29
29
|
description: Here is a description with multiple lines.
|
30
30
|
-
|
31
31
|
Date, Value, High, Low
|
32
|
-
2013-11-20,9.
|
33
|
-
2013-11-19,10.
|
34
|
-
2013-11-18,11.
|
32
|
+
2013-11-20,9.99,11.0,14.0
|
33
|
+
2013-11-19,10.03,,14.09
|
34
|
+
2013-11-18,11.03,,15.09
|
35
35
|
}
|
36
36
|
end
|
37
37
|
|
@@ -44,7 +44,7 @@ def qdf_attributes
|
|
44
44
|
column_names: ['Date', 'Value', 'High', 'Low'],
|
45
45
|
private: false,
|
46
46
|
reference_url: 'http://test.com/',
|
47
|
-
data: Quandl::Data.new([["2013-11-20", "9.
|
47
|
+
data: Quandl::Data.new([["2013-11-20", "9.99", "11.0", "14.0"],["2013-11-19", "10.03", nil, "14.09"]]),
|
48
48
|
}
|
49
49
|
end
|
50
50
|
|
@@ -59,7 +59,7 @@ private: false
|
|
59
59
|
reference_url: http://test.com/
|
60
60
|
-
|
61
61
|
Date,Value,High,Low
|
62
|
-
2013-11-20,9.
|
63
|
-
2013-11-19,10.
|
62
|
+
2013-11-20,9.99,11.0,14.0
|
63
|
+
2013-11-19,10.03,,14.09
|
64
64
|
}
|
65
65
|
end
|
@@ -18,25 +18,20 @@ describe Quandl::Format::Dataset do
|
|
18
18
|
its(:data){ should eq Quandl::Data.new([['2013-11-22','1252.0','454.95','448.2','450.0','450.0','1354405.0','6099.41'],['2013-11-21','452.25','457.75','449.1','451.2','451.0','218881.0','992.94']]) }
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
context "mismatched_rows.qdf" do
|
38
|
-
let(:data){ Quandl::Format::Dataset.load( fixtures_data['mismatched_rows'] ) }
|
39
|
-
it{ expect{data}.to raise_error Quandl::Format::Errors::ColumnCountMismatch, /had 3 columns/ }
|
21
|
+
|
22
|
+
expected_errors = [
|
23
|
+
{ file: 'invalid_data', error: /Date/ },
|
24
|
+
{ file: 'unknown_attribute', error: /this_attribute_does_not_exist/ },
|
25
|
+
{ file: 'mismatched_columns', error: /column_names had 4 columns/ },
|
26
|
+
{ file: 'mismatched_rows', error: /had 3 columns/ },
|
27
|
+
{ file: 'invalid_yaml', error: /could not find expected ':'/ },
|
28
|
+
]
|
29
|
+
# run each expectation
|
30
|
+
expected_errors.each do |pair|
|
31
|
+
it "#{pair[:file]}.qdf should error with #{pair[:error]}" do
|
32
|
+
Quandl::Logger.should_receive(:error).at_least(:once).with(pair[:error])
|
33
|
+
Quandl::Format::Dataset.load( fixtures_data[pair[:file]] )
|
34
|
+
end
|
40
35
|
end
|
41
36
|
|
42
37
|
end
|
@@ -26,7 +26,7 @@ describe Quandl::Format::Dataset::Load do
|
|
26
26
|
its(:name){ should eq 'Oil India Limited' }
|
27
27
|
its(:description){ should eq "Here is a description with multiple lines.\nThis is the second line." }
|
28
28
|
its(:column_names){ should eq ['Date', 'Value', 'High', 'Low'] }
|
29
|
-
its(:data){ should eq Quandl::Data.new([["2013-11-20", "9.
|
29
|
+
its(:data){ should eq Quandl::Data.new([["2013-11-20", "9.99", "11.0", "14.0"],["2013-11-19", "10.03", nil, "14.09"]]) }
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -12,7 +12,7 @@ describe Quandl::Format::Dataset do
|
|
12
12
|
its(:name){ should eq 'Test Dataset Name 2' }
|
13
13
|
its(:description){ should eq "Here is a description with multiple lines.\n This is the second line." }
|
14
14
|
its(:column_names){ should eq ['Date', 'Value', 'High', 'Low'] }
|
15
|
-
its(:data){ should eq Quandl::Data.new([["2013-11-20", "9.
|
15
|
+
its(:data){ should eq Quandl::Data.new([["2013-11-20", "9.99", "11.0", "14.0"], ["2013-11-19", "10.03", nil, "14.09"]]) }
|
16
16
|
|
17
17
|
its(:attributes){ should eq attributes }
|
18
18
|
|
data/spec/spec_helper.rb
CHANGED
@@ -14,4 +14,4 @@ require 'pry'
|
|
14
14
|
|
15
15
|
# Replace Quandl::Logger with Spec::Logger that will raise errors sent to #error
|
16
16
|
# This allows us to easily test error assertions in spec/lib/quandl/format/errors_spec.rb
|
17
|
-
Quandl::Logger.use(
|
17
|
+
Quandl::Logger.use(Quandl::Logger::Outputs)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quandl_format
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Blake Hilscher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -136,6 +136,7 @@ files:
|
|
136
136
|
- README.md
|
137
137
|
- Rakefile
|
138
138
|
- UPGRADE.md
|
139
|
+
- examples/load.rb
|
139
140
|
- lib/quandl/client/dataset/to_qdf.rb
|
140
141
|
- lib/quandl/format.rb
|
141
142
|
- lib/quandl/format/dataset.rb
|
@@ -149,6 +150,7 @@ files:
|
|
149
150
|
- spec/config/client.rb
|
150
151
|
- spec/config/logger.rb
|
151
152
|
- spec/fixtures/data/invalid_data.qdf
|
153
|
+
- spec/fixtures/data/invalid_yaml.qdf
|
152
154
|
- spec/fixtures/data/mismatched_columns.qdf
|
153
155
|
- spec/fixtures/data/mismatched_rows.qdf
|
154
156
|
- spec/fixtures/data/unknown_attribute.qdf
|
@@ -189,6 +191,7 @@ test_files:
|
|
189
191
|
- spec/config/client.rb
|
190
192
|
- spec/config/logger.rb
|
191
193
|
- spec/fixtures/data/invalid_data.qdf
|
194
|
+
- spec/fixtures/data/invalid_yaml.qdf
|
192
195
|
- spec/fixtures/data/mismatched_columns.qdf
|
193
196
|
- spec/fixtures/data/mismatched_rows.qdf
|
194
197
|
- spec/fixtures/data/unknown_attribute.qdf
|