m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/lib/song.rb
blob: 40769567c4919b2b2950986a10310e0a9373d1da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require 'yaml'

require './lib/recording'

class Song
  attr_accessor :title, :id, :lyrics, :recordings
  def initialize title, id
    @title = title
    @id = id
    @recordings = []
  end

  def self.from_yaml song_id
    metadata = YAML.load(File.read(src(song_meta song_id)))
    song = Song.new metadata['title'], song_id

    song.lyrics = nil
    lyrics_file = src (song_lyrics song_id)
    if File.file? lyrics_file
      song.lyrics = File.read lyrics_file
    end

    if metadata['recordings']
      metadata['recordings'].each do |recording|
        if recording['type'] == 'youtube'
          song.recordings.push YouTubeRecording.new(recording['link'])
        elsif recording['type'] == 'bandcamp'
          song.recordings.push BandcampRecording.new(recording['link'])
        end
      end
    end
    song
  end

  def split_lyrics
    @lyrics.split("\n\n").map { |paragraph| paragraph.split "\n" }
  end

  def render_sheet_music
    if File.file? (src (song_ly id))
      template = ERB.new(File.read('templates/sheet_music.html.erb'), trim_mode: '-')
      template.result binding
    else
      ""
    end
  end
end