quandl_operation 0.1.17 → 0.1.18

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: 359bf212c7078b884d1206f6fe807e9e53e2c61e
4
- data.tar.gz: 1321e7ada3550f892d6df76be5369ad4117cd7fa
3
+ metadata.gz: 87874fab0a0c7090d8a8d659e7492ff91fe7e98b
4
+ data.tar.gz: 0ea2c0c04aa165ba162eeeea6473da15dc6565c6
5
5
  SHA512:
6
- metadata.gz: 626e0270f2336c022940bfea4e6e83a80aa1355da0e10ed4d113279a6080ccb0c1931064ab6037816ad5982a3bd93e457de667f72c5f69e25558a4785ee87ca1
7
- data.tar.gz: f07f2d54e7e4e6fbe38712591f006ae75adb98bc5c6b5bf281e1bf806b45d98efdd7d3fe6f6405b718795a0234f9b4a5b004cbdf1db16bc61de99004d261445a
6
+ metadata.gz: 597a729a3f03e538c4876bfa0ab3fdbb56fe309bae13f3348c8cd1312c3548ca77a051981aaca5858b9bcf566114768792195457aff332e251258fceb4baf9a4
7
+ data.tar.gz: f7ccf8150ee7f1ee3ad1161f6f3c18b9cb40276d093bb4d117b9db059081d4052454611f4d049404266c449912bb913a4818f3b9ece52b663cfa29a2137ce572
data/UPGRADE.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.18
2
+
3
+ * Add QDFormat
4
+
5
+
1
6
  ## 0.1.17
2
7
 
3
8
  * bump quandl_logger to 0.2.0
@@ -0,0 +1,126 @@
1
+ class Quandl::Operation::QDFormat
2
+
3
+ attr_accessor :source_code, :code, :name, :description, :data, :headers
4
+
5
+ class << self
6
+
7
+ def read(path)
8
+ parse(File.read(path).strip)
9
+ end
10
+
11
+ def parse(output)
12
+ datasets = []
13
+ attributes = {}
14
+ # for each line
15
+ output.each_line do |line|
16
+ # strip whitespace
17
+ line = line.strip.rstrip
18
+
19
+ # skip blank
20
+ if line.blank?
21
+ next
22
+
23
+ # - denotes new dataset
24
+ elsif line[0] == '-'
25
+ # append current dataset
26
+ datasets << attributes unless attributes.blank?
27
+ # reset attributes for next dataset
28
+ attributes = {}
29
+
30
+ # without the data key we're still adding metadata
31
+ elsif !attributes.has_key?(:data)
32
+ # for each rule
33
+ matched = false
34
+ rules.each do |name, rule|
35
+ # test line
36
+ m = line.match(rule)
37
+ # if the rule matches
38
+ if !matched && m.present?
39
+ matched = true
40
+ case name
41
+ when :full_code then attributes[:full_code] = line
42
+ when :attribute then attributes[m[1].to_sym] = m[2]
43
+ end
44
+ end
45
+ end
46
+ # if no match was found for this line it must be data
47
+ attributes[:data] = line if !matched
48
+ # otherwise we're adding data
49
+ else
50
+ attributes[:data] += "\n" + line
51
+ end
52
+ end
53
+ datasets << attributes unless attributes.blank?
54
+ datasets.collect{|attrs| self.new(attrs) }
55
+ end
56
+
57
+ def rules
58
+ {
59
+ full_code: /^([A-Z0-9_]+)\/([A-Z0-9_]+)$/,
60
+ attribute: /^([a-z0-9_]+): (.+)/
61
+ }
62
+ end
63
+
64
+ end
65
+
66
+ def initialize(attrs)
67
+ assign_attributes(attrs)
68
+ end
69
+
70
+ def assign_attributes(attrs)
71
+ attrs.each do |key, value|
72
+ self.send("#{key}=", value) if self.respond_to?(key)
73
+ end
74
+ end
75
+
76
+ def attributes
77
+ { name: name, source_code: source_code, code: code, description: description, column_names: headers, data: data }
78
+ end
79
+
80
+ def inspect
81
+ "<##{self.class.name}" +
82
+ [:full_code, :name, :description, :headers].inject({}){|m,k| m[k] = self.send(k); m }.to_s +
83
+ " >"
84
+ end
85
+
86
+ def headers=(names)
87
+ names = names.split(",").collect(&:strip) if names.is_a?(String)
88
+ @headers = Array(names).flatten
89
+ end
90
+ def headers_as_qdf
91
+ headers.join(', ') if headers.is_a?(Array)
92
+ end
93
+
94
+ def full_code=(value)
95
+ value = value.split('/')
96
+ self.source_code = value[0]
97
+ self.code = value[1]
98
+ end
99
+
100
+ def full_code
101
+ [source_code, code].join('/')
102
+ end
103
+
104
+ def data_as_qdf
105
+ o = data
106
+ o = o.to_a if o.respond_to?(:to_a)
107
+ o = o.collect(&:to_csv).join if o.respond_to?(:to_csv) && o.first.is_a?(Array)
108
+ o = o.to_csv if o.respond_to?(:to_csv)
109
+ o
110
+ end
111
+
112
+ def data=(rows)
113
+ @data = rows
114
+ end
115
+
116
+ def to_qdf
117
+ output = [full_code]
118
+ [:name, :description].each do |attr_name|
119
+ output << "#{attr_name}: #{self.send(attr_name)}" if self.send(attr_name).present?
120
+ end
121
+ output << "headers: #{headers_as_qdf}" if headers_as_qdf.present?
122
+ output << data_as_qdf
123
+ output.join("\n")
124
+ end
125
+
126
+ end
@@ -1,5 +1,5 @@
1
1
  module Quandl
2
2
  module Operation
3
- VERSION = "0.1.17"
3
+ VERSION = "0.1.18"
4
4
  end
5
5
  end
@@ -0,0 +1,66 @@
1
+ # encoding: utf-8
2
+ require 'quandl/operation/qdformat'
3
+ require 'spec_helper'
4
+
5
+ describe Quandl::Operation::QDFormat do
6
+
7
+ let(:full_code){ "SOURCE_CODE/DATASET_CODE" }
8
+ let(:name){ 'name: Test dataset name' }
9
+ let(:description){ 'description: Dataset description' }
10
+ let(:headers){ 'headers: Date, value, high, low' }
11
+ let(:data){ "2013-11-20,9.99470588235294,11.003235294117646,14.00164705882353\n2013-11-19,10.039388096885814,,14.09718770934256\n2013-11-18,10.03702588792184,11.040801329322205,14.148600982164867\n2013-11-16,10.019903902583621,10.99988541851354,14.186053161235304\n2013-11-15,9.98453953586862,10.922239168500502,\n2013-11-14,10.004508614940358,10.894612328250766,\n2013-11-13,,10.877309120435308,14.187437960548612\n2013-11-12,,10.838918617657301,14.22499294338536\n2013-11-11,9.965116185761039,10.827442115591547,14.178970907392053\n2013-11-09,9.881291973139637,10.924889094631869" }
12
+
13
+ let(:output){
14
+ %Q{
15
+ ---
16
+ #{full_code}
17
+ #{name}
18
+ #{description}
19
+ #{headers}
20
+ #{data}
21
+ -
22
+ - I am a comment.
23
+ -
24
+ #{full_code}
25
+ #{name}
26
+ #{description}
27
+ #{headers}
28
+ #{data}
29
+ -----
30
+ }
31
+ }
32
+ let(:collection){ Quandl::Operation::QDFormat.parse(output) }
33
+
34
+ describe ".parse" do
35
+
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::Operation::QDFormat }
44
+ its(:source_code){ should eq 'SOURCE_CODE' }
45
+ its(:code){ should eq 'DATASET_CODE' }
46
+ its(:name){ should eq 'Test dataset name' }
47
+ its(:description){ should eq 'Dataset description' }
48
+ its(:headers){ should eq ['Date', 'value', 'high', 'low'] }
49
+ its(:data){ should eq data }
50
+ end
51
+ end
52
+
53
+ describe "#to_qdf" do
54
+ subject{ collection.first }
55
+ its(:to_qdf){ should eq [full_code, name, description, headers, data].join("\n")}
56
+
57
+ context "data Array" do
58
+ let(:data){ [['2013-11-20',9.94,11.2],['2013-11-19',9.94,11.2],['2013-11-18',9.94,11.2]] }
59
+ subject{ Quandl::Operation::QDFormat.new( full_code: "TEST/OIL", data: data ) }
60
+
61
+ its(:to_qdf){ should eq ["TEST/OIL", data.collect(&:to_csv).join].join("\n") }
62
+
63
+ end
64
+ end
65
+
66
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,7 @@
1
- require 'simplecov'
2
- SimpleCov.start
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+ end
3
5
 
4
6
  $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
5
7
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quandl_operation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.17
4
+ version: 0.1.18
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-14 00:00:00.000000000 Z
11
+ date: 2013-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -148,12 +148,14 @@ files:
148
148
  - lib/quandl/operation/core_ext/string.rb
149
149
  - lib/quandl/operation/core_ext/time.rb
150
150
  - lib/quandl/operation/parse.rb
151
+ - lib/quandl/operation/qdformat.rb
151
152
  - lib/quandl/operation/transform.rb
152
153
  - lib/quandl/operation/version.rb
153
154
  - quandl_operation.gemspec
154
155
  - spec/lib/quandl/operation/collapse_spec.rb
155
156
  - spec/lib/quandl/operation/date_spec.rb
156
157
  - spec/lib/quandl/operation/parse_spec.rb
158
+ - spec/lib/quandl/operation/qdformat_spec.rb
157
159
  - spec/lib/quandl/operation/transform_spec.rb
158
160
  - spec/spec_helper.rb
159
161
  homepage: http://blake.hilscher.ca/
@@ -184,5 +186,6 @@ test_files:
184
186
  - spec/lib/quandl/operation/collapse_spec.rb
185
187
  - spec/lib/quandl/operation/date_spec.rb
186
188
  - spec/lib/quandl/operation/parse_spec.rb
189
+ - spec/lib/quandl/operation/qdformat_spec.rb
187
190
  - spec/lib/quandl/operation/transform_spec.rb
188
191
  - spec/spec_helper.rb