quandl_format 0.0.2 → 0.1.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: 88de117fd0aaa74d5aed80b7d34a77200409fa6e
4
- data.tar.gz: 30fbd84655aedd481be97a672725aa173116bff9
3
+ metadata.gz: 321907cc4f59730ec0c37fd9bf0c5e67cd80fdc8
4
+ data.tar.gz: 8b272a1c2699e966ad6f96394725ea902cdcfce8
5
5
  SHA512:
6
- metadata.gz: 9809fccf8d818791f6b000d44918a525836d32c3227f4156bd5f7fe098240e42e7179745ee78ffc411604f8164429760670e9da8e0f02e778d42c9a5e7f23b08
7
- data.tar.gz: 0fc2a754ce3f08d0fd4b54066ab059623e53e3af8b112255188a42f60a54e3af0cfd00b613851a4d9902a91287129dde92dfe1320012d334bca9efa5210d0545
6
+ metadata.gz: 156bd0b3ed9d48abcb2639c67cb1492359dd88119e45575b95d97c64d6c55936ce73a67402258254a67aa2d646c6b97e1eb3b8c9a3db2ae44f1121167123b860
7
+ data.tar.gz: 6e046e36bf08aa9e8ee08089ad72b6cdde1c4673c87d9f6ef28fb3ee9c0a0709a5b63e360cf0b942dfb2dfd54b67249ccbc51644d98f14ccc3ed915e72efc86c
data/.gitignore CHANGED
@@ -2,4 +2,5 @@
2
2
  /pkg
3
3
  /tmp
4
4
  *.gem
5
- coverage/*
5
+ coverage/*
6
+ .rvmrc
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ group :specs do
5
+ guard :rspec, cmd: 'bundle exec rspec --fail-fast -f doc --color' do
6
+ watch(%r{^spec/.+_spec\.rb$})
7
+ watch(%r{^(lib/.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
8
+ watch('spec/spec_helper.rb') { 'spec' }
9
+ end
10
+ end
data/UPGRADE.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 0.1.0
2
+
3
+ * only update client attrs before upload
4
+ * dataset/client.rb client.assign_attributes(attributes) before upload human_errors revised
5
+ * load rescues and raises contextual errors
6
+ * add Dataset#human_errors, #full_url
7
+ * add fixtures/data/*.qdf that gets loaded into fixtures_data
8
+ * refactor spec format and qdf_attributes to fixtures
9
+ * add quandl/client/dataset/to_qdf extension Dataset#to_qdf Dataset#qdf_attributes
10
+ * refactor node/dump/load to Dataset Dataset::Load, Dataset::Dump
11
+
12
+
1
13
  ## 0.0.2
2
14
 
3
15
  * add support for meta attributes: private, display_url
@@ -0,0 +1,20 @@
1
+ module Quandl
2
+ module Client
3
+
4
+ class Dataset
5
+
6
+ def to_qdf
7
+ Quandl::Format::Dataset.new( qdf_attributes ).to_qdf
8
+ end
9
+
10
+ def qdf_attributes
11
+ Quandl::Format::Dataset.attribute_names.inject({}) do |memo, name|
12
+ memo[name] = self.send(name) if self.respond_to?(name)
13
+ memo
14
+ end
15
+ end
16
+
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,105 @@
1
+ module Quandl
2
+ module Format
3
+ class Dataset
4
+
5
+ module Attributes
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+
11
+ META_ATTRIBUTES = :source_code, :code, :name, :description, :private, :display_url
12
+ DATA_ATTRIBUTES = :column_names, :data
13
+
14
+ def attribute_names
15
+ @attribute_names ||= meta_attribute_names + data_attribute_names
16
+ end
17
+
18
+ def meta_attribute_names
19
+ META_ATTRIBUTES
20
+ end
21
+
22
+ def data_attribute_names
23
+ DATA_ATTRIBUTES
24
+ end
25
+
26
+ end
27
+
28
+ included do
29
+ attribute_names.each do |name|
30
+ attr_reader name unless method_defined?(name)
31
+ attr_writer name unless method_defined?("#{name}=")
32
+ end
33
+ end
34
+
35
+ def initialize(*args)
36
+ attrs = args.extract_options!
37
+ assign_attributes(attrs) if attrs.is_a?(Hash)
38
+ end
39
+
40
+ def assign_attributes(attrs)
41
+ attrs.each do |key, value|
42
+ raise_unknown_attribute_error!(key) unless respond_to?(key)
43
+ self.send("#{key}=", value)
44
+ end
45
+ end
46
+
47
+ def full_code=(value)
48
+ value = value.split('/')
49
+ self.source_code = value[0]
50
+ self.code = value[1]
51
+ end
52
+
53
+ def full_code
54
+ [source_code, code].join('/')
55
+ end
56
+
57
+ def description=(value)
58
+ @description = value.to_s.gsub('\n', "\n")
59
+ end
60
+
61
+ def data=(rows)
62
+ self.column_names = rows.shift unless rows.first.collect{|r| r.to_s.numeric? }.include?(true)
63
+ @data = Quandl::Data.new(rows).to_date
64
+ end
65
+
66
+ def column_names=(names)
67
+ @column_names = Array(names).flatten.collect{|n| n.strip.rstrip }
68
+ end
69
+
70
+ def to_qdf
71
+ Dump.record(self)
72
+ end
73
+
74
+ def meta_attributes
75
+ self.class.meta_attribute_names.inject({}){|m,k| m[k] = self.send(k); m }
76
+ end
77
+
78
+ def attributes
79
+ self.class.attribute_names.inject({}){|m,k| m[k] = self.send(k); m }
80
+ end
81
+
82
+ def inspect
83
+ attrs = attributes.collect do |key, value|
84
+ if value.is_a?(String)
85
+ value = "#{value[0..20]}..." if value.length > 20
86
+ value = "'#{value}'"
87
+ end
88
+ "#{key}: #{value}"
89
+ end
90
+ %Q{<##{self.class.name} #{attrs.join(', ')}>}
91
+ end
92
+
93
+ private
94
+
95
+ def raise_unknown_attribute_error!(key)
96
+ m = "UnknownAttribute #{key} recognized attributes are: #{self.class.attribute_names}"
97
+ raise Quandl::Format::Errors::UnknownAttribute, m
98
+ end
99
+
100
+
101
+ end
102
+
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,66 @@
1
+ module Quandl
2
+ module Format
3
+ class Dataset
4
+
5
+ module Client
6
+ extend ActiveSupport::Concern
7
+
8
+ def human_errors
9
+ return if errors.blank?
10
+ puts errors
11
+ m = "#{client.full_url} #{client.status}\n"
12
+ m += " errors: \n"
13
+ m += errors.collect do |error_type, messages|
14
+ next human_error(error_type, messages) unless messages.is_a?(Hash)
15
+ messages.collect{|n,m| human_error(n, m) }
16
+ end.flatten.compact.join
17
+ end
18
+
19
+ def human_error(name, message)
20
+ message = message.join(', ') if message.respond_to?(:join)
21
+ " #{name}: #{message}\n"
22
+ end
23
+
24
+ def full_url
25
+ client.full_url
26
+ end
27
+
28
+ def upload
29
+ client.save if valid?
30
+ end
31
+
32
+ def errors
33
+ client.error_messages
34
+ end
35
+
36
+ def valid?
37
+ assign_client_attributes
38
+ client.valid_with_server?
39
+ end
40
+
41
+ def client
42
+ @client ||= find_or_build_client
43
+ end
44
+ def client=(value)
45
+ raise ArgumentError, "Expected Quandl::Client::Dataset received #{value.class}" unless value.is_a?(Quandl::Client::Dataset)
46
+ @client = value
47
+ end
48
+
49
+
50
+ protected
51
+
52
+ def assign_client_attributes
53
+ client.assign_attributes(attributes)
54
+ end
55
+
56
+ def find_or_build_client
57
+ @client = Quandl::Client::Dataset.find(full_code)
58
+ @client = Quandl::Client::Dataset.new unless @client.exists?
59
+ @client
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -1,24 +1,21 @@
1
- module Quandl
2
- module Format
3
-
4
- class Dump
5
-
1
+ class Quandl::Format::Dataset::Dump
2
+
6
3
  class << self
7
4
 
8
- def nodes(*args)
9
- Array(args).flatten.collect{|r| node(r) }.join("\n")
5
+ def collection(*args)
6
+ Array(args).flatten.collect{|r| record(r) }.join("\n")
10
7
  end
11
8
 
12
- def node(node)
13
- self.new(node).to_qdf
9
+ def record(record)
10
+ self.new(record).to_qdf
14
11
  end
15
12
 
16
13
  end
17
14
 
18
- attr_accessor :node
15
+ attr_accessor :record
19
16
 
20
17
  def initialize(r)
21
- self.node = r
18
+ self.record = r
22
19
  end
23
20
 
24
21
  def to_qdf
@@ -30,21 +27,18 @@ class Dump
30
27
  end
31
28
 
32
29
  def meta_attributes
33
- node.meta_attributes.stringify_keys.to_yaml[4..-1] + "-\n"
30
+ record.meta_attributes.stringify_keys.to_yaml[4..-1] + "-\n"
34
31
  end
35
32
 
36
33
  def data
37
- data = node.data
34
+ data = record.data
38
35
  data = data.collect(&:to_csv).join if data.is_a?(Array) && data.first.respond_to?(:to_csv)
39
36
  data = data.to_csv if data.respond_to?(:to_csv)
40
37
  data
41
38
  end
42
39
 
43
40
  def column_names
44
- node.column_names.to_csv if node.column_names.present?
41
+ record.column_names.to_csv if record.column_names.present?
45
42
  end
46
43
 
47
- end
48
-
49
- end
50
44
  end
@@ -1,14 +1,23 @@
1
- class Quandl::Format::Load
1
+ class Quandl::Format::Dataset::Load
2
2
 
3
3
  SECTION_DELIMITER = '-'
4
4
 
5
5
  class << self
6
-
7
- def from_file(path)
8
- from_string(File.read(path).strip)
6
+
7
+ def file(path)
8
+ string(File.read(path).strip)
9
9
  end
10
10
 
11
- def from_string(input)
11
+ def string(input)
12
+ nodes = parse_string(input)
13
+ nodes = parse_yaml_and_csv(nodes)
14
+ nodes = nodes_to_datasets(nodes)
15
+ nodes
16
+ end
17
+
18
+ protected
19
+
20
+ def parse_string(input)
12
21
  nodes = []
13
22
  section_type = :data
14
23
  input.each_line do |rline|
@@ -25,7 +34,7 @@ class Quandl::Format::Load
25
34
  # update the section to attributes
26
35
  section_type = :attributes
27
36
 
28
- # have we reached the end of the attributes?
37
+ # have we reached the end of the attributes?
29
38
  elsif line[0] == '-'
30
39
  # update the section to data
31
40
  section_type = :data
@@ -36,29 +45,39 @@ class Quandl::Format::Load
36
45
  # YAML must include whitespace
37
46
  nodes[-1][section_type] += (section_type == :data) ? "#{line}\n" : rline
38
47
  end
39
- # append the current node
40
- nodes = parse_nodes(nodes)
41
- nodes = initialize_nodes(nodes)
42
48
  nodes
43
49
  end
44
50
 
45
-
46
- protected
47
-
48
- def parse_nodes(nodes)
51
+ def parse_yaml_and_csv(nodes)
49
52
  nodes.collect do |node|
50
53
  # parse attrs as yaml
51
- node[:attributes] = YAML.load( node[:attributes] )
54
+ node[:attributes] = YAML.load( node[:attributes] ).symbolize_keys!
52
55
  # parse data as csv
53
56
  node[:attributes][:data] = CSV.parse(node[:data])
54
57
  node
55
58
  end
56
59
  end
57
60
 
58
- def initialize_nodes(nodes)
59
- nodes.collect do |node|
60
- Quandl::Format::Node.new(node[:attributes])
61
+ def nodes_to_datasets(nodes)
62
+ datasets = []
63
+ nodes.each_with_index do |node, index|
64
+ dataset = node_to_dataset(node, index)
65
+ datasets << dataset if dataset
61
66
  end
67
+ datasets
68
+ end
69
+
70
+ def node_to_dataset(node, index)
71
+ Quandl::Format::Dataset.new( node[:attributes] )
72
+ rescue => e# Quandl::Format::Errors::UnknownAttribute => e
73
+ message = "Error: Dataset #{index + 1}\n"
74
+ message += node[:attributes][:source_code] + '/' if node[:attributes][:source_code].present?
75
+ message += node[:attributes][:code] + "\n"
76
+ message += "#{$!}\n"
77
+ message += "--"
78
+ Quandl::Logger.error(message, e)
79
+ nil
80
+ # raise $!, message, $!.backtrace
62
81
  end
63
82
 
64
83
  def attribute_format
@@ -0,0 +1,36 @@
1
+ module Quandl
2
+ module Format
3
+
4
+ class Dataset
5
+
6
+ # classes
7
+ require_relative 'dataset/load'
8
+ require_relative 'dataset/dump'
9
+
10
+ # concerns
11
+ require_relative 'dataset/attributes'
12
+ require_relative 'dataset/client'
13
+
14
+ include Quandl::Format::Dataset::Attributes
15
+ include Quandl::Format::Dataset::Client
16
+
17
+ class << self
18
+
19
+ def load(input)
20
+ Load.string(input)
21
+ end
22
+
23
+ def load_from_file(path)
24
+ Load.file(path)
25
+ end
26
+
27
+ def dump(datasets)
28
+ Dump.collection(datasets)
29
+ end
30
+
31
+ end
32
+
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ module Quandl
2
+ module Format
3
+ module Errors
4
+ class UnknownAttribute < StandardError; end
5
+ end
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  module Quandl
2
2
  module Format
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
data/lib/quandl/format.rb CHANGED
@@ -4,35 +4,18 @@ require "quandl/format/version"
4
4
 
5
5
  require 'csv'
6
6
 
7
- require 'quandl/operation'
7
+ require 'quandl/client'
8
8
 
9
9
  require "active_support"
10
10
  require "active_support/inflector"
11
11
  require "active_support/core_ext/hash"
12
12
  require "active_support/core_ext/object"
13
13
 
14
- require 'quandl/format/dump'
15
- require 'quandl/format/load'
16
- require 'quandl/format/node'
14
+ require 'quandl/format/errors'
15
+ require 'quandl/format/dataset'
16
+ require 'quandl/client/dataset/to_qdf'
17
17
 
18
18
  module Quandl
19
19
  module Format
20
-
21
- class << self
22
-
23
- def load(input)
24
- Quandl::Format::Load.from_string(input)
25
- end
26
-
27
- def load_file(file_path)
28
- Quandl::Format::Load.from_file(file_path)
29
- end
30
-
31
- def dump(nodes)
32
- Quandl::Format::Dump.nodes(nodes)
33
- end
34
-
35
- end
36
-
37
20
  end
38
21
  end
@@ -22,7 +22,9 @@ Gem::Specification.new do |s|
22
22
  s.add_development_dependency "fivemat", "~> 1.2"
23
23
  s.add_development_dependency "pry"
24
24
  s.add_development_dependency "simplecov"
25
+ s.add_development_dependency "guard"
26
+ s.add_development_dependency "guard-rspec"
25
27
 
26
- s.add_runtime_dependency "quandl_operation", "~> 0.1"
28
+ s.add_runtime_dependency "quandl_client", "~> 2.2"
27
29
 
28
30
  end
@@ -0,0 +1,2 @@
1
+ Quandl::Client.token = ENV['QUANDL_AUTH_TOKEN']
2
+ Quandl::Client.use ENV['QUANDL_API_HOST']
@@ -0,0 +1,25 @@
1
+ module Spec
2
+ class Logger
3
+
4
+ class << self
5
+
6
+ def info(*args)
7
+ log(*args)
8
+ end
9
+
10
+ def debug(*args)
11
+ log(*args)
12
+ end
13
+
14
+ def error(*args)
15
+ raise args[1] if args[1].kind_of?(StandardError)
16
+ end
17
+
18
+ def log(*args)
19
+ STDOUT << args.collect(&:to_s).join(', ') + "\n"
20
+ end
21
+
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ code: "BLAKE_TEST_1"
2
+ name: "A new title"
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
+ Date,Open,High,Low,Last,Close,Total Trade Quantity,Turnover (Lacs)
8
+ 2013-11-21,452.25,457.75,449.1,451.2,451.0,218881.0,992.94
9
+
10
+ code: "BLAKE_TEST_2"
11
+ name: "A new title"
12
+ description: "The description Date, Open, High"
13
+ -
14
+ 2013-11-22,1252.0,454.95,448.2,450.0,450.0,1354405.0,6099.41
15
+ 2013-11-21,452.25,457.75,449.1,451.2,451.0,218881.0,992.94
16
+ Date,Open,High,Low,Last,Close,Total Trade Quantity,Turnover (Lacs)
17
+
18
+ code: "BLAKE_TEST_3"
19
+ name: "A new title"
20
+ description: "The description Date, Open, High"
21
+ -
22
+ Date,Open,High,Low,Last,Close,Total Trade Quantity,Turnover (Lacs)
23
+ 2013-11-22,1252.0,454.95,448.2,450.0,Low,1354405.0,6099.41
24
+ 2013-11-21,452.25,457.75,449.1,451.2,451.0,218881.0,992.94
@@ -0,0 +1,11 @@
1
+ code: "BLAKE_TEST_1"
2
+ name: "A new title"
3
+ this_attribute_does_not_exist: "Why is this here?"
4
+ description: "The description Date, Open, High"
5
+ private: false
6
+ -
7
+ Date, Column 1, Column 2, Column 3, Column 4
8
+ 2013-11-30,,11.003235294117646,13.948117647058822,19.038
9
+ 2013-11-29,9.953796020761246,10.886730449826988,13.957142899653979,19.10407305882353
10
+ 2013-11-28,10.031669836688378,10.89889797209444,13.954679874436392,19.20858357614533
11
+ 2013-11-25,10.07474700716357,10.876459064504834,13.923487060599417,19.139658658607395
@@ -0,0 +1,24 @@
1
+ code: "BLAKE_TEST_1"
2
+ name: "A new title"
3
+ description: "The description Date, Open, High"
4
+ private: false
5
+ -
6
+ Date,Open,High,Low,Last,Close,Total Trade Quantity,Turnover (Lacs)
7
+ 2013-11-22,1252.0,454.95,448.2,450.0,450.0,1354405.0,6099.41
8
+ 2013-11-21,452.25,457.75,449.1,451.2,451.0,218881.0,992.94
9
+
10
+ code: "BLAKE_TEST_2"
11
+ name: "A new title"
12
+ description: "The description Date, Open, High"
13
+ -
14
+ Date,Open,High,Low,Last,Close,Total Trade Quantity,Turnover (Lacs)
15
+ 2013-11-22,1252.0,454.95,448.2,450.0,450.0,1354405.0,6099.41
16
+ 2013-11-21,452.25,457.75,449.1,451.2,451.0,218881.0,992.94
17
+
18
+ code: "BLAKE_TEST_3"
19
+ name: "A new title"
20
+ description: "The description Date, Open, High"
21
+ -
22
+ Date,Open,High,Low,Last,Close,Total Trade Quantity,Turnover (Lacs)
23
+ 2013-11-22,1252.0,454.95,448.2,450.0,450.0,1354405.0,6099.41
24
+ 2013-11-21,452.25,457.75,449.1,451.2,451.0,218881.0,992.94
@@ -0,0 +1,65 @@
1
+ def fixtures_data
2
+ return @fixtures_data if @fixtures_data
3
+ @fixtures_data = {}
4
+ Dir.glob( File.join( File.dirname(__FILE__), 'data/**/*.qdf' ) ).each{|f|
5
+ name = File.basename(f, '.qdf').to_s
6
+ @fixtures_data[name] = File.read(f)
7
+ }
8
+ @fixtures_data
9
+ end
10
+
11
+ def qdf_format
12
+ %Q{
13
+ # first dataset
14
+ source_code: NSE
15
+ code: OIL
16
+ name: Oil India Limited
17
+ description: |-
18
+ Here is a description with multiple lines.
19
+ This is the second line.
20
+ -
21
+ Date, Value, High, Low
22
+ 2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353
23
+ 2013-11-19,10.039388096885814,,14.09718770934256
24
+
25
+ # Second dataset
26
+ code: DATASET_CODE_2
27
+ source_code: SOURCE_CODE_2
28
+ name: Test Dataset Name 2
29
+ description: Here is a description with multiple lines.
30
+ -
31
+ Date, Value, High, Low
32
+ 2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353
33
+ 2013-11-19,10.039388096885814,,14.09718770934256
34
+ 2013-11-18,11.039388096885814,,15.09718770934256
35
+ }
36
+ end
37
+
38
+ def qdf_attributes
39
+ {
40
+ code: 'DATASET_CODE_2',
41
+ source_code: 'SOURCE_CODE',
42
+ name: 'Test Dataset Name 2',
43
+ description: "Here is a description with multiple lines.\n This is the second line.",
44
+ column_names: ['Date', 'Value', 'High', 'Low'],
45
+ private: false,
46
+ display_url: 'http://test.com/',
47
+ data: Quandl::Data.new([["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]]),
48
+ }
49
+ end
50
+
51
+ def qdf_attributes_to_format
52
+ %Q{source_code: SOURCE_CODE
53
+ code: DATASET_CODE_2
54
+ name: Test Dataset Name 2
55
+ description: |-
56
+ Here is a description with multiple lines.
57
+ This is the second line.
58
+ private: false
59
+ display_url: http://test.com/
60
+ -
61
+ Date,Value,High,Low
62
+ 2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353
63
+ 2013-11-19,10.039388096885814,,14.09718770934256
64
+ }
65
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Client::Dataset do
5
+
6
+ let(:attributes) { {
7
+ code: 'DATASET_CODE_2',
8
+ source_code: 'SOURCE_CODE',
9
+ name: 'Test Dataset Name 2',
10
+ description: "Here is a description with multiple lines.\n This is the second line.",
11
+ column_names: ['Date', 'Value', 'High', 'Low'],
12
+ private: false,
13
+ display_url: 'http://test.com/',
14
+ data: [["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],
15
+ ["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]],
16
+ }}
17
+
18
+ subject{ Quandl::Client::Dataset.new( attributes ) }
19
+
20
+ it{ should respond_to :to_qdf }
21
+
22
+ its(:to_qdf){ should eq Quandl::Format::Dataset.new(attributes).to_qdf }
23
+
24
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Format::Dataset::Attributes do
5
+
6
+ let(:attributes) { qdf_attributes }
7
+
8
+ subject{ Quandl::Format::Dataset.new( attributes ) }
9
+
10
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Format::Dataset::Client do
5
+
6
+ context "without attributes" do
7
+ subject{ Quandl::Format::Dataset.new }
8
+ its(:valid?){ should be_false }
9
+ its(:upload){ should be_false }
10
+ end
11
+
12
+ let(:attributes) { qdf_attributes }
13
+ let(:dataset){ Quandl::Format::Dataset.new( attributes ) }
14
+ subject{ dataset }
15
+
16
+ it{ should respond_to :valid? }
17
+ it{ should respond_to :upload }
18
+
19
+ its(:valid?){ should be_true }
20
+ its(:client){ should be_a Quandl::Client::Dataset }
21
+
22
+ ["string", 10, Date.today, [1,2,3], {hash: 'test'} ].each do |value|
23
+ it "#client= #{value.class} should raise_error ArgumentError" do
24
+ expect{ subject.client = value }.to raise_error ArgumentError
25
+ end
26
+ end
27
+
28
+ it "#client= Quandl::Client::Dataset should return client" do
29
+ d = Quandl::Client::Dataset.new
30
+ subject.client = d
31
+ subject.client.should eq d
32
+ end
33
+
34
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Format::Dataset do
5
+ subject{ data }
6
+ context "valid.qdf" do
7
+ let(:data){ Quandl::Format::Dataset.load( fixtures_data['valid'] ) }
8
+
9
+ it{ should be_a Array }
10
+ its(:count){ should eq 3 }
11
+
12
+ describe "#first" do
13
+ subject{ data.first }
14
+ its(:code){ should eq 'BLAKE_TEST_1' }
15
+ its(:name){ should eq 'A new title' }
16
+ its(:description){ should eq 'The description Date, Open, High'}
17
+ its(:column_names){ should eq ['Date','Open','High','Low','Last','Close','Total Trade Quantity','Turnover (Lacs)']}
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
+ end
20
+ end
21
+
22
+ context "invalid_data.qdf" do
23
+ let(:data){ Quandl::Format::Dataset.load( fixtures_data['invalid_data'] ) }
24
+ it{ expect{data}.to raise_error Quandl::Operation::Errors::UnknownDateFormat, /Date/ }
25
+ end
26
+
27
+ context "unknown_attribute.qdf" do
28
+ let(:data){ Quandl::Format::Dataset.load( fixtures_data['unknown_attribute'] ) }
29
+ it{ expect{data}.to raise_error Quandl::Format::Errors::UnknownAttribute, /this_attribute_does_not_exist/ }
30
+ end
31
+
32
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Format::Dataset::Load do
5
+
6
+ let(:format){ qdf_format }
7
+
8
+ describe ".file" do
9
+ subject{ Quandl::Format::Dataset::Load.file("spec/fixtures/data/valid.qdf") }
10
+ its(:count){ should eq 3 }
11
+ end
12
+
13
+ describe ".string" do
14
+
15
+ let(:collection){ Quandl::Format::Dataset::Load.string(format) }
16
+ subject{ collection }
17
+
18
+ its(:count){ should eq 2 }
19
+
20
+ describe "#first" do
21
+ subject{ collection.first }
22
+
23
+ it{ should be_a Quandl::Format::Dataset }
24
+ its(:source_code){ should eq 'NSE' }
25
+ its(:code){ should eq 'OIL' }
26
+ its(:name){ should eq 'Oil India Limited' }
27
+ its(:description){ should eq "Here is a description with multiple lines.\nThis is the second line." }
28
+ its(:column_names){ should eq ['Date', 'Value', 'High', 'Low'] }
29
+ its(:data){ should eq Quandl::Data.new([["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]]) }
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe Quandl::Format::Dataset do
5
+
6
+ let(:attributes) { qdf_attributes }
7
+
8
+ subject{ Quandl::Format::Dataset.new(attributes) }
9
+
10
+ its(:code){ should eq 'DATASET_CODE_2' }
11
+ its(:source_code){ should eq 'SOURCE_CODE' }
12
+ its(:name){ should eq 'Test Dataset Name 2' }
13
+ its(:description){ should eq "Here is a description with multiple lines.\n This is the second line." }
14
+ its(:column_names){ should eq ['Date', 'Value', 'High', 'Low'] }
15
+ its(:data){ should eq Quandl::Data.new([["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"], ["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]]) }
16
+
17
+ its(:attributes){ should eq attributes }
18
+
19
+ its(:to_qdf){ should eq qdf_attributes_to_format }
20
+
21
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,8 +3,15 @@ if ENV['COVERAGE']
3
3
  SimpleCov.start
4
4
  end
5
5
 
6
+ Dir.glob( File.join( File.dirname(__FILE__), 'fixtures/**/*.rb' ) ).each{|f| require(f) }
6
7
  $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
7
8
 
8
9
  require "rspec"
9
10
  require "quandl/format"
10
- require 'pry'
11
+ require "config/client"
12
+ require "config/logger"
13
+ require 'pry'
14
+
15
+ # Replace Quandl::Logger with Spec::Logger that will raise errors sent to #error
16
+ # This allows us to easily test error assertions in spec/lib/quandl/format/errors_spec.rb
17
+ Quandl::Logger.use(Spec::Logger)
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.0.2
4
+ version: 0.1.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: 2013-11-26 00:00:00.000000000 Z
11
+ date: 2013-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -81,19 +81,47 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: quandl_operation
84
+ name: guard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: quandl_client
85
113
  requirement: !ruby/object:Gem::Requirement
86
114
  requirements:
87
115
  - - ~>
88
116
  - !ruby/object:Gem::Version
89
- version: '0.1'
117
+ version: '2.2'
90
118
  type: :runtime
91
119
  prerelease: false
92
120
  version_requirements: !ruby/object:Gem::Requirement
93
121
  requirements:
94
122
  - - ~>
95
123
  - !ruby/object:Gem::Version
96
- version: '0.1'
124
+ version: '2.2'
97
125
  description: Data will be loaded and dumped.
98
126
  email:
99
127
  - blake@hilscher.ca
@@ -103,18 +131,33 @@ extra_rdoc_files: []
103
131
  files:
104
132
  - .gitignore
105
133
  - Gemfile
134
+ - Guardfile
106
135
  - LICENSE
107
136
  - README.md
108
137
  - Rakefile
109
138
  - UPGRADE.md
139
+ - lib/quandl/client/dataset/to_qdf.rb
110
140
  - lib/quandl/format.rb
111
- - lib/quandl/format/dump.rb
112
- - lib/quandl/format/load.rb
113
- - lib/quandl/format/node.rb
141
+ - lib/quandl/format/dataset.rb
142
+ - lib/quandl/format/dataset/attributes.rb
143
+ - lib/quandl/format/dataset/client.rb
144
+ - lib/quandl/format/dataset/dump.rb
145
+ - lib/quandl/format/dataset/load.rb
146
+ - lib/quandl/format/errors.rb
114
147
  - lib/quandl/format/version.rb
115
148
  - quandl_format.gemspec
116
- - spec/lib/quandl/format/load_spec.rb
117
- - spec/lib/quandl/format/node_spec.rb
149
+ - spec/config/client.rb
150
+ - spec/config/logger.rb
151
+ - spec/fixtures/data/invalid_data.qdf
152
+ - spec/fixtures/data/unknown_attribute.qdf
153
+ - spec/fixtures/data/valid.qdf
154
+ - spec/fixtures/format.rb
155
+ - spec/lib/quandl/client/dataset_spec.rb
156
+ - spec/lib/quandl/format/dataset/attributes_spec.rb
157
+ - spec/lib/quandl/format/dataset/client_spec.rb
158
+ - spec/lib/quandl/format/dataset/errors_spec.rb
159
+ - spec/lib/quandl/format/dataset/load_spec.rb
160
+ - spec/lib/quandl/format/dataset_spec.rb
118
161
  - spec/spec_helper.rb
119
162
  homepage: http://blake.hilscher.ca/
120
163
  licenses:
@@ -141,6 +184,16 @@ signing_key:
141
184
  specification_version: 4
142
185
  summary: Quandl Data Format
143
186
  test_files:
144
- - spec/lib/quandl/format/load_spec.rb
145
- - spec/lib/quandl/format/node_spec.rb
187
+ - spec/config/client.rb
188
+ - spec/config/logger.rb
189
+ - spec/fixtures/data/invalid_data.qdf
190
+ - spec/fixtures/data/unknown_attribute.qdf
191
+ - spec/fixtures/data/valid.qdf
192
+ - spec/fixtures/format.rb
193
+ - spec/lib/quandl/client/dataset_spec.rb
194
+ - spec/lib/quandl/format/dataset/attributes_spec.rb
195
+ - spec/lib/quandl/format/dataset/client_spec.rb
196
+ - spec/lib/quandl/format/dataset/errors_spec.rb
197
+ - spec/lib/quandl/format/dataset/load_spec.rb
198
+ - spec/lib/quandl/format/dataset_spec.rb
146
199
  - spec/spec_helper.rb
@@ -1,73 +0,0 @@
1
- class Quandl::Format::Node
2
-
3
- META_ATTRIBUTES = :source_code, :code, :name, :description, :private, :display_url
4
- DATA_ATTRIBUTES = :column_names, :data
5
-
6
- attr_accessor *( META_ATTRIBUTES + DATA_ATTRIBUTES )
7
-
8
- class << self
9
-
10
- def attribute_names
11
- @attribute_names ||= meta_attribute_names + data_attribute_names
12
- end
13
-
14
- def meta_attribute_names
15
- META_ATTRIBUTES
16
- end
17
-
18
- def data_attribute_names
19
- DATA_ATTRIBUTES
20
- end
21
-
22
- end
23
-
24
- def initialize(attrs)
25
- assign_attributes(attrs)
26
- end
27
-
28
- def assign_attributes(attrs)
29
- attrs.each do |key, value|
30
- self.send("#{key}=", value) if self.respond_to?(key)
31
- end
32
- end
33
-
34
- def full_code=(value)
35
- value = value.split('/')
36
- self.source_code = value[0]
37
- self.code = value[1]
38
- end
39
-
40
- def full_code
41
- [source_code, code].join('/')
42
- end
43
-
44
- def description=(value)
45
- @description = value.to_s.gsub('\n', "\n")
46
- end
47
-
48
- def data=(rows)
49
- self.column_names = rows.shift unless rows.first.collect{|r| r.to_s.numeric? }.include?(true)
50
- @data = rows
51
- end
52
-
53
- def column_names=(names)
54
- @column_names = Array(names).flatten.collect{|n| n.strip.rstrip }
55
- end
56
-
57
- def to_qdf
58
- Quandl::Format::Dump.node(self)
59
- end
60
-
61
- def meta_attributes
62
- self.class.meta_attribute_names.inject({}){|m,k| m[k] = self.send(k); m }
63
- end
64
-
65
- def attributes
66
- self.class.attribute_names.inject({}){|m,k| m[k] = self.send(k); m }
67
- end
68
-
69
- def inspect
70
- "<##{self.class.name} #{meta_attributes.to_s} >"
71
- end
72
-
73
- end
@@ -1,54 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe Quandl::Format::Load do
5
-
6
- let(:qdf_dataset){
7
- %Q{
8
- # first dataset
9
- source_code: NSE
10
- code: OIL
11
- name: Oil India Limited
12
- description: |-
13
- Here is a description with multiple lines.
14
- This is the second line.
15
- -
16
- Date, Value, High, Low
17
- 2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353
18
- 2013-11-19,10.039388096885814,,14.09718770934256
19
-
20
- # Second dataset
21
- code: DATASET_CODE_2
22
- source_code: SOURCE_CODE_2
23
- name: Test Dataset Name 2
24
- description: Here is a description with multiple lines.
25
- -
26
- Date, Value, High, Low
27
- 2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353
28
- 2013-11-19,10.039388096885814,,14.09718770934256
29
- 2013-11-18,11.039388096885814,,15.09718770934256
30
- }
31
- }
32
-
33
- describe ".from_string" do
34
-
35
- let(:collection){ Quandl::Format::Load.from_string(qdf_dataset) }
36
- subject{ collection }
37
-
38
- its(:count){ should eq 2 }
39
-
40
- describe "#first" do
41
- subject{ collection.first }
42
-
43
- it{ should be_a Quandl::Format::Node }
44
- its(:source_code){ should eq 'NSE' }
45
- its(:code){ should eq 'OIL' }
46
- its(:name){ should eq 'Oil India Limited' }
47
- its(:description){ should eq "Here is a description with multiple lines.\nThis is the second line." }
48
- its(:column_names){ should eq ['Date', 'Value', 'High', 'Low'] }
49
- its(:data){ should eq [["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],
50
- ["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]] }
51
- end
52
- end
53
-
54
- end
@@ -1,44 +0,0 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
3
-
4
- describe Quandl::Format::Node do
5
-
6
- let(:attributes) { {
7
- code: 'DATASET_CODE_2',
8
- source_code: 'SOURCE_CODE',
9
- name: 'Test Dataset Name 2',
10
- description: "Here is a description with multiple lines.\n This is the second line.",
11
- column_names: ['Date', 'Value', 'High', 'Low'],
12
- private: false,
13
- display_url: 'http://test.com/',
14
- data: [["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],
15
- ["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]],
16
- }}
17
-
18
- subject{ Quandl::Format::Node.new(attributes) }
19
-
20
- its(:code){ should eq 'DATASET_CODE_2' }
21
- its(:source_code){ should eq 'SOURCE_CODE' }
22
- its(:name){ should eq 'Test Dataset Name 2' }
23
- its(:description){ should eq "Here is a description with multiple lines.\n This is the second line." }
24
- its(:column_names){ should eq ['Date', 'Value', 'High', 'Low'] }
25
- its(:data){ should eq [["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],
26
- ["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]] }
27
-
28
- its(:attributes){ should eq attributes }
29
-
30
- its(:to_qdf){ should eq %Q{source_code: SOURCE_CODE
31
- code: DATASET_CODE_2
32
- name: Test Dataset Name 2
33
- description: |-
34
- Here is a description with multiple lines.
35
- This is the second line.
36
- private: false
37
- display_url: http://test.com/
38
- -
39
- Date,Value,High,Low
40
- 2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353
41
- 2013-11-19,10.039388096885814,,14.09718770934256
42
- }}
43
-
44
- end