Class: VideoData::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/video_data/parser.rb

Overview

Class that handles the extraction of the attributes and video data from the csv file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Parser

Extracts the data inside the csv file given in the path The attributes and data are separated here.

  • Args :

    • path The file path to the csv file.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/video_data/parser.rb', line 13

def initialize(path)
  raise ArgumentError, "Expected a csv file" unless File.exists?(path) && path =~ /(.*)\.csv/
  @data = File.read(path)

  raw_attributes = @data.slice!(/.*?\n/)
  raise VideoData::NotAWellFormedCSV unless raw_attributes

  attributes = raw_attributes.strip.split(',')
  @attributes = attributes.collect do |attr|
    attr.downcase.gsub(/:/, '_').to_sym
  end

  [:videos, :date, :celebrity].each do |attr|
    raise VideoData::NotAWellFormedCSV, "#{attr} column is missing in csv file" unless @attributes.include?(attr)
  end
end

Instance Attribute Details

#attributesObject (readonly)

The first row of the csv file defines the attributes the videos have.



7
8
9
# File 'lib/video_data/parser.rb', line 7

def attributes
  @attributes
end

Instance Method Details

#videosObject

For each row in the csv file a VideoData::Video is created.

  • Returns :

    • An array of VideoData::Video objects.



33
34
35
36
37
38
39
40
# File 'lib/video_data/parser.rb', line 33

def videos
  videos = []
  @data.lines do |line|
    videos << VideoData::Video.new(attributes, line)
  end

  videos
end