quandl_format 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a70e0e436b6880e25c8bfc3643d70b2440bd4d8
4
+ data.tar.gz: e1fe743d521464cac0e2202e8c78ad1f2b09858f
5
+ SHA512:
6
+ metadata.gz: 4429a0b2cbd4c1a99c452182bbb621edb06029ceabdaa7188a3838efa0d4ba288a6ef3ffef3a11dcd20975a1365680285375ab4688b896728acef386924ee915
7
+ data.tar.gz: 6c38e3a0d254ee664b5ea5d1c041d5c49a2642ad89ac3f223b727100113884f735233daa88abb66927e38289f7d3a1b7b2a14e1cbb8b82463374eab74ef7903a
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ /Gemfile.lock
2
+ /pkg
3
+ /tmp
4
+ *.gem
5
+ coverage/*
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012-2013 Blake Hilscher
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # Quandl::Format
2
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler"
2
+ require "rake"
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ task :default => :spec
7
+
8
+ desc "Run all specs"
9
+ RSpec::Core::RakeTask.new(:spec) do |task|
10
+ task.pattern = "spec/**/*_spec.rb"
11
+ end
data/UPGRADE.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.0.1
2
+
3
+ * init
@@ -0,0 +1,56 @@
1
+ module Quandl
2
+ module Format
3
+
4
+ class Dump
5
+
6
+ ATTRIBUTES = [ :source_code, :code, :name, :description, :private, :display_url ]
7
+
8
+ class << self
9
+
10
+ def nodes(*args)
11
+ Array(args).flatten.collect{|r| node(r) }.join("\n")
12
+ end
13
+
14
+ def node(node)
15
+ self.new(node).to_qdf
16
+ end
17
+
18
+ end
19
+
20
+ attr_accessor :node
21
+
22
+ def initialize(r)
23
+ self.node = r
24
+ end
25
+
26
+ def to_qdf
27
+ [ attributes,
28
+ "-\n",
29
+ column_names,
30
+ data
31
+ ].compact.join
32
+ end
33
+
34
+ def attributes
35
+ attrs = ATTRIBUTES.inject({}) do |memo, name|
36
+ name = name.to_s
37
+ memo[name] = node.send(name) if node.respond_to?(name) && node.send(name).present?
38
+ memo
39
+ end
40
+ attrs.to_yaml[4..-1]
41
+ end
42
+
43
+ def data
44
+ data = node.data.is_a?(Array) ? node.data.collect(&:to_csv).join : node.data
45
+ data = data.to_csv if data.respond_to?(:to_csv)
46
+ data
47
+ end
48
+
49
+ def column_names
50
+ node.column_names.to_csv if node.column_names.present?
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,70 @@
1
+ class Quandl::Format::Load
2
+
3
+ SECTION_DELIMITER = '-'
4
+
5
+ class << self
6
+
7
+ def from_file(path)
8
+ from_string(File.read(path).strip)
9
+ end
10
+
11
+ def from_string(input)
12
+ nodes = []
13
+ section_type = :data
14
+ input.each_line do |rline|
15
+ # strip whitespace
16
+ line = rline.strip.rstrip
17
+ # ignore comments and blank lines
18
+ next if line[0] == '#' || line.blank?
19
+
20
+ # are we looking at an attribute?
21
+ if line =~ attribute_format
22
+ # if we are leaving the data section
23
+ # then this is the start of a new node
24
+ nodes << { attributes: '', data: '' } if section_type == :data
25
+ # update the section to attributes
26
+ section_type = :attributes
27
+
28
+ # have we reached the end of the attributes?
29
+ elsif line[0] == '-'
30
+ # update the section to data
31
+ section_type = :data
32
+ # skip to the next line
33
+ next
34
+ end
35
+ # add the line to it's section in the current node.
36
+ # YAML must include whitespace
37
+ nodes[-1][section_type] += (section_type == :data) ? "#{line}\n" : rline
38
+ end
39
+ # append the current node
40
+ nodes = parse_nodes(nodes)
41
+ nodes = initialize_nodes(nodes)
42
+ nodes
43
+ end
44
+
45
+
46
+ protected
47
+
48
+ def parse_nodes(nodes)
49
+ nodes.collect do |node|
50
+ # parse attrs as yaml
51
+ node[:attributes] = YAML.load( node[:attributes] )
52
+ # parse data as csv
53
+ node[:attributes][:data] = CSV.parse(node[:data])
54
+ node
55
+ end
56
+ end
57
+
58
+ def initialize_nodes(nodes)
59
+ nodes.collect do |node|
60
+ Quandl::Format::Node.new(node[:attributes])
61
+ end
62
+ end
63
+
64
+ def attribute_format
65
+ /^([a-z0-9_]+): (.+)/
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,51 @@
1
+ class Quandl::Format::Node
2
+
3
+ ATTRIBUTES = :source_code, :code, :name, :description, :column_names, :data
4
+ attr_accessor *ATTRIBUTES
5
+
6
+ def initialize(attrs)
7
+ assign_attributes(attrs)
8
+ end
9
+
10
+ def assign_attributes(attrs)
11
+ attrs.each do |key, value|
12
+ self.send("#{key}=", value) if self.respond_to?(key)
13
+ end
14
+ end
15
+
16
+ def attributes
17
+ ATTRIBUTES.inject({}){|m,k| m[k] = self.send(k); m }
18
+ end
19
+
20
+ def inspect
21
+ "<##{self.class.name} #{attributes.to_s} >"
22
+ end
23
+
24
+ def full_code=(value)
25
+ value = value.split('/')
26
+ self.source_code = value[0]
27
+ self.code = value[1]
28
+ end
29
+
30
+ def full_code
31
+ [source_code, code].join('/')
32
+ end
33
+
34
+ def description=(value)
35
+ @description = value.to_s.gsub('\n', "\n")
36
+ end
37
+
38
+ def data=(rows)
39
+ self.column_names = rows.shift unless rows.first.collect{|r| r.to_s.numeric? }.include?(true)
40
+ @data = rows
41
+ end
42
+
43
+ def column_names=(names)
44
+ @column_names = Array(names).flatten.collect{|n| n.strip.rstrip }
45
+ end
46
+
47
+ def to_qdf
48
+ Quandl::Format::Dump.node(self)
49
+ end
50
+
51
+ end
@@ -0,0 +1,5 @@
1
+ module Quandl
2
+ module Format
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,38 @@
1
+ require 'quandl/logger'
2
+
3
+ require "quandl/format/version"
4
+
5
+ require 'csv'
6
+
7
+ require 'quandl/operation'
8
+
9
+ require "active_support"
10
+ require "active_support/inflector"
11
+ require "active_support/core_ext/hash"
12
+ require "active_support/core_ext/object"
13
+
14
+ require 'quandl/format/dump'
15
+ require 'quandl/format/load'
16
+ require 'quandl/format/node'
17
+
18
+ module Quandl
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
+ end
38
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "quandl/format/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "quandl_format"
7
+ s.version = Quandl::Format::VERSION
8
+ s.authors = ["Blake Hilscher"]
9
+ s.email = ["blake@hilscher.ca"]
10
+ s.homepage = "http://blake.hilscher.ca/"
11
+ s.license = "MIT"
12
+ s.summary = "Quandl Data Format"
13
+ s.description = "Data will be loaded and dumped."
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "rake", "~> 10.0"
21
+ s.add_development_dependency "rspec", "~> 2.13"
22
+ s.add_development_dependency "fivemat", "~> 1.2"
23
+ s.add_development_dependency "pry"
24
+ s.add_development_dependency "simplecov"
25
+
26
+ s.add_runtime_dependency "quandl_operation", "~> 0.1"
27
+
28
+ end
@@ -0,0 +1,54 @@
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
@@ -0,0 +1,40 @@
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
+ data: [["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],
13
+ ["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]],
14
+ }}
15
+
16
+ subject{ Quandl::Format::Node.new(attributes) }
17
+
18
+ its(:code){ should eq 'DATASET_CODE_2' }
19
+ its(:source_code){ should eq 'SOURCE_CODE' }
20
+ its(:name){ should eq 'Test Dataset Name 2' }
21
+ its(:description){ should eq "Here is a description with multiple lines.\n This is the second line." }
22
+ its(:column_names){ should eq ['Date', 'Value', 'High', 'Low'] }
23
+ its(:data){ should eq [["2013-11-20", "9.99470588235294", "11.003235294117646", "14.00164705882353"],
24
+ ["2013-11-19", "10.039388096885814", nil, "14.09718770934256"]] }
25
+
26
+ its(:attributes){ should eq attributes }
27
+
28
+ its(:to_qdf){ should eq %Q{source_code: SOURCE_CODE
29
+ code: DATASET_CODE_2
30
+ name: Test Dataset Name 2
31
+ description: |-
32
+ Here is a description with multiple lines.
33
+ This is the second line.
34
+ -
35
+ Date,Value,High,Low
36
+ 2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353
37
+ 2013-11-19,10.039388096885814,,14.09718770934256
38
+ }}
39
+
40
+ end
@@ -0,0 +1,10 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ end
5
+
6
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
7
+
8
+ require "rspec"
9
+ require "quandl/format"
10
+ require 'pry'
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quandl_format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Blake Hilscher
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '2.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fivemat
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: quandl_operation
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '0.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '0.1'
97
+ description: Data will be loaded and dumped.
98
+ email:
99
+ - blake@hilscher.ca
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE
107
+ - README.md
108
+ - Rakefile
109
+ - UPGRADE.md
110
+ - lib/quandl/format.rb
111
+ - lib/quandl/format/dump.rb
112
+ - lib/quandl/format/load.rb
113
+ - lib/quandl/format/node.rb
114
+ - lib/quandl/format/version.rb
115
+ - quandl_format.gemspec
116
+ - spec/lib/quandl/format/load_spec.rb
117
+ - spec/lib/quandl/format/node_spec.rb
118
+ - spec/spec_helper.rb
119
+ homepage: http://blake.hilscher.ca/
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.1.10
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Quandl Data Format
143
+ test_files:
144
+ - spec/lib/quandl/format/load_spec.rb
145
+ - spec/lib/quandl/format/node_spec.rb
146
+ - spec/spec_helper.rb