m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/lib/song.rb
blob: 151fa6c97d2d04e1ecc434e6a8617f56044b3f43 (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
48
49
50
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|
        case recording['type']
        when 'youtube'
          song.recordings.push YouTubeRecording.new(recording['link'])
        when 'bandcamp'
          song.recordings.push BandcampRecording.new(recording['embed'])
        when 'soundcloud'
          song.recordings.push SoundCloudRecording.new(recording['embed'])
        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