Module: Bowie::SongUtils

Defined in:
lib/bowie/song_utils.rb

Class Method Summary collapse

Class Method Details

.check_songs_dirObject



119
120
121
122
123
# File 'lib/bowie/song_utils.rb', line 119

def self.check_songs_dir
  unless File.directory? self.get_bowie_dir
    FileUtils.mkdir self.get_bowie_dir
  end
end

.create_empty_lyrics_fileObject



125
126
127
# File 'lib/bowie/song_utils.rb', line 125

def self.create_empty_lyrics_file
  File.open("./#{self.get_bowie_dir}/lyrics.yml", "w"){|f| YAML.dump(Hash.new, f)}
end

.create_empty_songs_fileObject



115
116
117
# File 'lib/bowie/song_utils.rb', line 115

def self.create_empty_songs_file
  File.open("./songs.yml", "w"){|f| YAML.dump(Array.new, f)}
end

.get_bowie_dirObject



4
5
6
7
8
9
10
11
# File 'lib/bowie/song_utils.rb', line 4

def self.get_bowie_dir
  if File.exist? '.bowierc'
    f = YAML.load_file '.bowierc'
    (f['bowie-dir'] == nil) ? (return 'bowie_songs') : (return f['bowie-dir'])
  else 
    return 'bowie_songs'
  end
end

.get_local_lyricsObject

Retrive the local lyrics file



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bowie/song_utils.rb', line 39

def self.get_local_lyrics
  if File.exist? "#{self.get_bowie_dir}/lyrics.yml"
    if self.valid_lyrics_file? "#{self.get_bowie_dir}/lyrics.yml"
      return YAML.load_file("#{self.get_bowie_dir}/lyrics.yml")
    else
      raise "#{self.get_bowie_dir}/lyrics.yml is not valid"
    end
  else
    self.check_songs_dir
    self.create_empty_lyrics_file
    return Hash.new
  end
end

.get_local_songsObject

Retrive the local registry of installed songs



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bowie/song_utils.rb', line 23

def self.get_local_songs
  if File.exist? 'songs.yml'
    if self.valid_songs_file? 'songs.yml'
      self.check_songs_dir
      return YAML.load_file('songs.yml')
    else
      raise "songs.yml is not valid"
    end
  else
    self.create_empty_songs_file
    self.check_songs_dir
    return Array.new
  end
end

.get_songsObject

Retrive the online registry of available songs



14
15
16
17
18
19
20
# File 'lib/bowie/song_utils.rb', line 14

def self.get_songs
  begin
    YAML.parse(open("https://raw.github.com/axyz/bowie-songs/master/songs.yml")).to_ruby
  rescue
    raise "cannot connect to github.com"
  end
end

.get_songs_dirsObject

Return an array with all the direcotry inside bowye_songs



54
55
56
57
58
59
60
61
62
# File 'lib/bowie/song_utils.rb', line 54

def self.get_songs_dirs
  result = []
  Dir.foreach(self.get_bowie_dir) do |el|
    unless (el == "." or el == ".." or el == "lyrics.yml")
      result.push el
    end
  end
  result
end

.installed_song?(song) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bowie/song_utils.rb', line 92

def self.installed_song? (song)
  @songs = self.get_local_songs
  @dirs = self.get_songs_dirs

  unless (@songs.grep /#{song}/).empty?
    if (@dirs.grep /#{song}/).empty?
      return false
    else 
      return true
    end
  end
  return false
end

.parse_song_name(s) ⇒ Object

Parse the name of a package in the form name#version



65
66
67
# File 'lib/bowie/song_utils.rb', line 65

def self.parse_song_name(s)
  s.split("#")[0]
end

.parse_song_version(s) ⇒ Object

Parse the version of a package in the form name#version



70
71
72
# File 'lib/bowie/song_utils.rb', line 70

def self.parse_song_version(s)
  s.split("#")[1]
end

.valid_lyrics_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
# File 'lib/bowie/song_utils.rb', line 106

def self.valid_lyrics_file?(file)
  begin
    f = YAML.load_file file
    (f.is_a? Hash)? true : false
  rescue
    false
  end
end

.valid_song?(string) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/bowie/song_utils.rb', line 88

def self.valid_song?(string)
  (string =~ /\A[a-z0-9]+(-?[a-z0-9])*(#\d+.\d+.\d+)?\z/) == nil ? false : true
end

.valid_songs_file?(file) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bowie/song_utils.rb', line 74

def self.valid_songs_file?(file)
  begin
    f = YAML.load_file file
    f.each do |s|
      unless valid_song? s
        return false
      end
    end
    return true
  rescue
    return false
  end
end