Class: Quandl::Client::Dataset
- Inherits:
-
Base
- Object
- Base
- Quandl::Client::Dataset
show all
- Defined in:
- lib/quandl/client/models/dataset.rb
Defined Under Namespace
Classes: Data
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
her_api, models, models_use_her_api!, url_with_version, use
Class Method Details
.find(value) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/quandl/client/models/dataset.rb', line 13
def find(value)
if value.is_a?(String)
value = value.strip.rstrip
value = value.gsub("\\","/").gsub(".","/")
value = value.upcase
end
return nil unless value.is_a?(Integer) || value.to_s =~ %r{^#{Quandl::Pattern.full_code}$}
super(value)
end
|
.touch_existing(id) ⇒ Object
9
10
11
|
# File 'lib/quandl/client/models/dataset.rb', line 9
def touch_existing(id)
put(File.join(Quandl::Client::Base.url_with_version, "datasets/#{id}/touch")).exists?
end
|
Instance Method Details
#ambiguous_code_requires_source_code! ⇒ Object
130
131
132
133
134
135
136
137
|
# File 'lib/quandl/client/models/dataset.rb', line 130
def ambiguous_code_requires_source_code!
if code.to_s.numeric? && source_code.blank?
message = %Q{Pure numerical codes like "#{code}" are not allowed unless you include a source code. Do this:\nsource_code: <USERNAME>\ncode: #{code}}
self.errors.add( :data, message )
return false
end
true
end
|
#data ⇒ Object
90
91
92
|
# File 'lib/quandl/client/models/dataset.rb', line 90
def data
dataset_data.data? ? dataset_data.data : data_scope
end
|
#data=(value) ⇒ Object
94
95
96
|
# File 'lib/quandl/client/models/dataset.rb', line 94
def data=(value)
dataset_data.data = value
end
|
#data_columns_should_not_exceed_column_names! ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/quandl/client/models/dataset.rb', line 139
def data_columns_should_not_exceed_column_names!
if errors.size == 0 && dataset_data.data? && column_names.present? && data.first.count != column_names.count
self.errors.add( :data, "You may not change the number of columns in a dataset. This dataset has #{column_names.count} columns but you tried to send #{data.first.count} columns." )
return false
end
true
end
|
#data_row_count_should_match_column_count! ⇒ Object
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'lib/quandl/client/models/dataset.rb', line 164
def data_row_count_should_match_column_count!
return true unless dataset_data.data? && column_names.present?
column_count = column_names.count
data.each_with_index do |row, index|
next if row.count == column_count
self.errors.add( :data, "Unexpected number of points in this row:\n#{row.join(',')}\nFound #{row.size-1} but expected #{column_names.count-1} based on precedent from the header row (#{column_names.join(',')})" )
return false
end
true
end
|
#data_rows_should_have_equal_columns! ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/quandl/client/models/dataset.rb', line 147
def data_rows_should_have_equal_columns!
return true unless dataset_data.data?
column_count = data[0].count
data.each_with_index do |row, index|
next if row.count == column_count
self.errors.add( :data, "Unexpected number of points in this row:\n#{row.join(',')}\nFound #{row.size-1} but expected #{data[0].size-1} based on precedent from the first row (#{data[0].join(',')})" )
return false
end
true
end
|
#data_scope ⇒ Object
114
115
116
|
# File 'lib/quandl/client/models/dataset.rb', line 114
def data_scope
@data_scope ||= Quandl::Client::Dataset::Data.with_id(id)
end
|
#dataset_data ⇒ Object
118
119
120
|
# File 'lib/quandl/client/models/dataset.rb', line 118
def dataset_data
@dataset_data ||= Quandl::Client::Dataset::Data.new( id: id )
end
|
#delete_data ⇒ Object
98
99
100
101
102
103
|
# File 'lib/quandl/client/models/dataset.rb', line 98
def delete_data
return false if new_record?
self.class.destroy_existing("#{id}/data").saved?
end
|
#delete_rows(*dates) ⇒ Object
105
106
107
108
109
110
111
112
|
# File 'lib/quandl/client/models/dataset.rb', line 105
def delete_rows(*dates)
return false if new_record?
query = { dates: Array(dates).flatten }.to_query
self.class.destroy_existing("#{id}/data/rows?#{query}").saved?
end
|
192
193
194
195
196
197
|
# File 'lib/quandl/client/models/dataset.rb', line 192
def enforce_required_formats
self.source_code = self.source_code.to_s.upcase
self.code = self.code.to_s.upcase
self.locations_attributes = locations_attributes.to_json if locations_attributes.respond_to?(:to_json) && !locations_attributes.kind_of?(String)
end
|
#full_code ⇒ Object
84
85
86
|
# File 'lib/quandl/client/models/dataset.rb', line 84
def full_code
File.join(self.source_code.to_s, self.code.to_s)
end
|
#full_url ⇒ Object
80
81
82
|
# File 'lib/quandl/client/models/dataset.rb', line 80
def full_url
File.join(Quandl::Client::Base.url.gsub('api/', ''), full_code)
end
|
#reference_url ⇒ Object
72
73
74
|
# File 'lib/quandl/client/models/dataset.rb', line 72
def reference_url
self.display_url
end
|
#reference_url=(value) ⇒ Object
75
76
77
78
|
# File 'lib/quandl/client/models/dataset.rb', line 75
def reference_url=(value)
value = "http://#{value}" if value.present? && !(value =~ /:\/\//)
self.display_url = value
end
|
#reload ⇒ Object
122
123
124
125
126
|
# File 'lib/quandl/client/models/dataset.rb', line 122
def reload
@dataset_data = nil
@data_scope = nil
@full_code = nil
end
|
#save_dataset_data ⇒ Object
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/quandl/client/models/dataset.rb', line 181
def save_dataset_data
return if (!saved? && id.blank?)
return if !dataset_data.data?
dataset_data.id = id
dataset_data.save
attributes.each{|k,v| attributes[k] = dataset_data.attributes[k] if dataset_data.attributes.has_key?(k) }
@metadata[:status] = dataset_data.status
end
|
#source ⇒ Object
39
40
41
|
# File 'lib/quandl/client/models/dataset.rb', line 39
def source
@source ||= Quandl::Client::Source.find(self.source_code)
end
|