Class: Quandl::Client::Dataset

Inherits:
Base
  • Object
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)
  # enforce code formatting
  if value.is_a?(String)
    # strip extra whitespace
    value = value.strip.rstrip
    # ensure slashes are forward facing
    value = value.gsub("\\","/").gsub(".","/")
    # ensure uppercase
    value = value.upcase
  end
  # short-circuit if value is illegal
  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 (protected)



156
157
158
159
160
161
162
163
# File 'lib/quandl/client/models/dataset.rb', line 156

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

#dataObject

DATA



92
93
94
# File 'lib/quandl/client/models/dataset.rb', line 92

def data
  defined?(@data) ? @data : data_scope
end

#data=(value) ⇒ Object



96
97
98
# File 'lib/quandl/client/models/dataset.rb', line 96

def data=(value)
  @data = Quandl::Data.new(value).sort_descending
end

#data?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/quandl/client/models/dataset.rb', line 100

def data?
  @data.is_a?(Quandl::Data)
end

#data_columns_should_not_exceed_column_names!Object (protected)



165
166
167
168
169
170
171
# File 'lib/quandl/client/models/dataset.rb', line 165

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 (protected)



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/quandl/client/models/dataset.rb', line 190

def data_row_count_should_match_column_count!
  # skip validation unless data and column_names present
  return true unless data? && data.present? && column_names.present?
  # count the number of expected columns
  column_count = column_names.count
  # check each row
  data.each_with_index do |row, index|
    # the row is valid if it's count matches the first row's count
    next if row.count == column_count
    # the row is invalid if the count is mismatched
    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 validation failure
    return false
  end
  true
end

#data_rows_should_have_equal_columns!Object (protected)



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/quandl/client/models/dataset.rb', line 173

def data_rows_should_have_equal_columns!
  # skip validation unless data is present
  return true unless data? && data.present?
  # use first row as expected column count
  column_count = data[0].count
  # check each row
  data.each_with_index do |row, index|
    # the row is valid if it's count matches the first row's count
    next if row.count == column_count
    # the row is invalid if the count is mismatched
    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 validation failure
    return false
  end
  true
end

#data_scopeObject



120
121
122
# File 'lib/quandl/client/models/dataset.rb', line 120

def data_scope
  @data_scope ||= Quandl::Client::Dataset::Data.with_id(id)
end

#data_should_be_valid!Object (protected)



140
141
142
143
144
145
146
# File 'lib/quandl/client/models/dataset.rb', line 140

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_dataObject



124
125
126
# File 'lib/quandl/client/models/dataset.rb', line 124

def dataset_data
  @dataset_data ||= Quandl::Client::Dataset::Data.new( id: id )
end

#dataset_data?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/quandl/client/models/dataset.rb', line 128

def dataset_data?
  @dataset_data.is_a?(Quandl::Client::Dataset::Data)
end

#dataset_data_should_be_valid!Object (protected)



148
149
150
151
152
153
154
# File 'lib/quandl/client/models/dataset.rb', line 148

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_dataObject



104
105
106
107
108
109
# File 'lib/quandl/client/models/dataset.rb', line 104

def delete_data
  # cant delete unsaved records
  return false if new_record?
  # delete and return success / failure
  self.class.destroy_existing("#{id}/data").saved?
end

#delete_rows(*dates) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/quandl/client/models/dataset.rb', line 111

def delete_rows(*dates)
  # cant delete unsaved records
  return false if new_record?
  # collect dates
  query = { dates: Array(dates).flatten }.to_query
  # delete and return success / failure
  self.class.destroy_existing("#{id}/data/rows?#{query}").saved?
end

#enforce_required_formatsObject (protected)



232
233
234
235
236
237
# File 'lib/quandl/client/models/dataset.rb', line 232

def enforce_required_formats
  # self.data = Quandl::Data.new(data).to_csv
  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_codeObject



86
87
88
# File 'lib/quandl/client/models/dataset.rb', line 86

def full_code
  File.join(self.source_code.to_s, self.code.to_s)
end

#full_urlObject



82
83
84
# File 'lib/quandl/client/models/dataset.rb', line 82

def full_url
  File.join(Quandl::Client::Base.url.gsub('api/', ''), full_code)
end

#inherit_errors(object) ⇒ Object (protected)



221
222
223
224
225
226
227
228
229
230
# File 'lib/quandl/client/models/dataset.rb', line 221

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
  [:status] = object.status
  object
end

#reference_urlObject



74
75
76
# File 'lib/quandl/client/models/dataset.rb', line 74

def reference_url
  self.display_url
end

#reference_url=(value) ⇒ Object



77
78
79
80
# File 'lib/quandl/client/models/dataset.rb', line 77

def reference_url=(value)
  value = "http://#{value}" if value.present? && !(value =~ /:\/\//)
  self.display_url = value
end

#reloadObject



132
133
134
135
136
# File 'lib/quandl/client/models/dataset.rb', line 132

def reload
  @dataset_data = nil
  @data_scope = nil
  @full_code = nil
end

#save_dataset_dataObject (protected)



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/quandl/client/models/dataset.rb', line 207

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
  # update dataset's attributes with dataset_data's attributes
  attributes.each{|k,v| attributes[k] = dataset_data.attributes[k] if dataset_data.attributes.has_key?(k) }
  # update dataset errors with dataset_data
  [:status] = dataset_data.status unless dataset_data.saved?
  # inherit_errors(dataset_data) unless dataset_data.saved?
end

#sourceObject

ASSOCIATIONS #



39
40
41
# File 'lib/quandl/client/models/dataset.rb', line 39

def source
  @source ||= Quandl::Client::Source.find(self.source_code)
end