Class: SkyJam::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/skyjam/track.rb

Defined Under Namespace

Modules: Source

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info) ⇒ Track

Returns a new instance of Track.



19
20
21
22
23
24
25
26
27
# File 'lib/skyjam/track.rb', line 19

def initialize(info)
  @id = info[:id]
  @title = info[:title]
  @album = info[:album]
  @album_artist = info[:album_artist] unless info[:album_artist].nil? || info[:album_artist].empty?
  @artist = info[:artist]
  @number = info[:track_number]
  @size = info[:track_size]
end

Instance Attribute Details

#albumObject (readonly)

Returns the value of attribute album.



11
12
13
# File 'lib/skyjam/track.rb', line 11

def album
  @album
end

#album_artistObject (readonly)

Returns the value of attribute album_artist.



11
12
13
# File 'lib/skyjam/track.rb', line 11

def album_artist
  @album_artist
end

#artistObject (readonly)

Returns the value of attribute artist.



11
12
13
# File 'lib/skyjam/track.rb', line 11

def artist
  @artist
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/skyjam/track.rb', line 11

def id
  @id
end

#numberObject (readonly)

Returns the value of attribute number.



11
12
13
# File 'lib/skyjam/track.rb', line 11

def number
  @number
end

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/skyjam/track.rb', line 11

def size
  @size
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/skyjam/track.rb', line 11

def title
  @title
end

Instance Method Details

#data(remote: false) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/skyjam/track.rb', line 67

def data(remote: false)
  if remote || !local?
    url = client.download_url(id)
    client.download_track(url)
  else
    File.binread(path)
  end
end

#dirnameObject



37
38
39
40
41
42
# File 'lib/skyjam/track.rb', line 37

def dirname
  path_components = [library.path,
                     escape_path_component(album_artist || artist),
                     escape_path_component(album)]
  File.join(path_components)
end

#download(lazy: false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/skyjam/track.rb', line 52

def download(lazy: false)
  return if !lazy || (lazy && local?)

  file = Tempfile.new(filename)
  begin
    file << data(remote: true)
  rescue SkyJam::Client::Error
    file.close!
    raise
  else
    make_dir
    FileUtils.mv(file.path, path)
  end
end

#extnameObject



33
34
35
# File 'lib/skyjam/track.rb', line 33

def extname
  '.mp3'
end

#filenameObject



29
30
31
# File 'lib/skyjam/track.rb', line 29

def filename
  escape_path_component("%02d - #{title}" % number) << extname
end

#local?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/skyjam/track.rb', line 48

def local?
  File.exist?(path)
end

#pathObject



44
45
46
# File 'lib/skyjam/track.rb', line 44

def path
  File.join(dirname, filename)
end

#uploadObject



76
77
78
# File 'lib/skyjam/track.rb', line 76

def upload
  fail NotImplementedError
end