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
|
# File 'lib/quandl/client/models/dataset.rb', line 13
def find(value)
value = format_id(value)
return nil unless value.is_a?(Integer) || value.to_s =~ %r{^#{Quandl::Pattern.full_code}$}
super(value)
end
|
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/quandl/client/models/dataset.rb', line 22
def format_id(value)
if value.is_a?(String)
value = value.strip.rstrip
value = value.gsub("\\","/").gsub(".","/")
value = value.upcase
end
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
187
188
189
190
191
192
193
194
|
# File 'lib/quandl/client/models/dataset.rb', line 187
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
|
#code=(v) ⇒ Object
113
114
115
|
# File 'lib/quandl/client/models/dataset.rb', line 113
def code=(v)
write_attribute(:code, sanitize_code(v) )
end
|
#data ⇒ Object
101
102
103
|
# File 'lib/quandl/client/models/dataset.rb', line 101
def data
defined?(@data) ? @data : data_scope
end
|
#data=(value) ⇒ Object
105
106
107
|
# File 'lib/quandl/client/models/dataset.rb', line 105
def data=(value)
@data = Quandl::Data.new(value).sort_descending
end
|
#data? ⇒ Boolean
109
110
111
|
# File 'lib/quandl/client/models/dataset.rb', line 109
def data?
@data.is_a?(Quandl::Data)
end
|
#data_columns_should_not_exceed_column_names! ⇒ Object
196
197
198
199
200
201
202
|
# File 'lib/quandl/client/models/dataset.rb', line 196
def data_columns_should_not_exceed_column_names!
if errors.size == 0 && data? && data.present? && 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/quandl/client/models/dataset.rb', line 221
def data_row_count_should_match_column_count!
return true unless data? && data.present? && 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/quandl/client/models/dataset.rb', line 204
def data_rows_should_have_equal_columns!
return true unless data? && data.present?
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
141
142
143
|
# File 'lib/quandl/client/models/dataset.rb', line 141
def data_scope
@data_scope ||= Quandl::Client::Dataset::Data.with_id(id)
end
|
#data_should_be_valid! ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/quandl/client/models/dataset.rb', line 161
def data_should_be_valid!
if data? && !data.valid?
data.errors.each{|k,v| self.errors.add( k,v ) }
return false
end
true
end
|
#dataset_data ⇒ Object
145
146
147
|
# File 'lib/quandl/client/models/dataset.rb', line 145
def dataset_data
@dataset_data ||= Quandl::Client::Dataset::Data.new( id: id )
end
|
#dataset_data? ⇒ Boolean
149
150
151
|
# File 'lib/quandl/client/models/dataset.rb', line 149
def dataset_data?
@dataset_data.is_a?(Quandl::Client::Dataset::Data)
end
|
#dataset_data_should_be_valid! ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/quandl/client/models/dataset.rb', line 169
def dataset_data_should_be_valid!
if dataset_data? && !dataset_data.valid?
dataset_data.errors.each{|k,v| self.errors.add( k,v ) }
return false
end
true
end
|
#delete_data ⇒ Object
125
126
127
128
129
130
|
# File 'lib/quandl/client/models/dataset.rb', line 125
def delete_data
return false if new_record?
self.class.destroy_existing("#{id}/data").saved?
end
|
#delete_rows(*dates) ⇒ Object
132
133
134
135
136
137
138
139
|
# File 'lib/quandl/client/models/dataset.rb', line 132
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
|
263
264
265
266
267
268
|
# File 'lib/quandl/client/models/dataset.rb', line 263
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
95
96
97
|
# File 'lib/quandl/client/models/dataset.rb', line 95
def full_code
File.join(self.source_code.to_s, self.code.to_s)
end
|
#full_url ⇒ Object
91
92
93
|
# File 'lib/quandl/client/models/dataset.rb', line 91
def full_url
File.join(Quandl::Client::Base.url.gsub(/api\/?/, ''), full_code)
end
|
#inherit_errors(object) ⇒ Object
252
253
254
255
256
257
258
259
260
261
|
# File 'lib/quandl/client/models/dataset.rb', line 252
def inherit_errors(object)
return unless object.respond_to?(:response_errors) && object.response_errors.respond_to?(:each)
object.response_errors.each do |key, messages|
if messages.respond_to?(:each)
messages.each{|message| errors.add(key, message) }
end
end
@metadata[:status] = object.status
object
end
|
#reference_url ⇒ Object
83
84
85
|
# File 'lib/quandl/client/models/dataset.rb', line 83
def reference_url
self.display_url
end
|
#reference_url=(value) ⇒ Object
86
87
88
89
|
# File 'lib/quandl/client/models/dataset.rb', line 86
def reference_url=(value)
value = "http://#{value}" if value.present? && !(value =~ /:\/\//)
self.display_url = value
end
|
#reload ⇒ Object
153
154
155
156
157
|
# File 'lib/quandl/client/models/dataset.rb', line 153
def reload
@dataset_data = nil
@data_scope = nil
@full_code = nil
end
|
#sanitize_code(code) ⇒ Object
121
122
123
|
# File 'lib/quandl/client/models/dataset.rb', line 121
def sanitize_code(code)
code.to_s.upcase.gsub(',','')
end
|
#save_dataset_data ⇒ Object
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/quandl/client/models/dataset.rb', line 238
def save_dataset_data
return if (!saved? && id.blank?)
return if !data? || data.blank?
dataset_data.id = id
dataset_data.data = data.to_csv
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 unless dataset_data.saved?
end
|
#source ⇒ Object
46
47
48
|
# File 'lib/quandl/client/models/dataset.rb', line 46
def source
@source ||= Quandl::Client::Source.find(self.source_code)
end
|
#source_code=(v) ⇒ Object
117
118
119
|
# File 'lib/quandl/client/models/dataset.rb', line 117
def source_code=(v)
write_attribute(:source_code, sanitize_code(v) )
end
|
#source_code_should_exist! ⇒ Object
177
178
179
180
181
182
183
184
185
|
# File 'lib/quandl/client/models/dataset.rb', line 177
def source_code_should_exist!
if source_code.present?
Quandl::Client::Source.cached[source_code] = Quandl::Client::Source.find(source_code) unless Quandl::Client::Source.cached.has_key?(source_code)
source = Quandl::Client::Source.cached[source_code]
self.errors.add( :source_code, "Could not find a source with the source_code '#{source_code}'" ) if source.blank? || source.code.blank?
return false
end
true
end
|