Class: Kanye::Track

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Track

Returns a new instance of Track.



8
9
10
11
12
13
14
# File 'lib/kanye/track.rb', line 8

def initialize(params={})
  @id     = params[:id]
  @key    = params[:key]
  @title  = params[:title]
  @artist = params[:artist]
  @cookie = params[:cookie]
end

Instance Attribute Details

#artistObject (readonly)

Returns the value of attribute artist.



16
17
18
# File 'lib/kanye/track.rb', line 16

def artist
  @artist
end

Returns the value of attribute cookie.



16
17
18
# File 'lib/kanye/track.rb', line 16

def cookie
  @cookie
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/kanye/track.rb', line 16

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



16
17
18
# File 'lib/kanye/track.rb', line 16

def key
  @key
end

#titleObject (readonly)

Returns the value of attribute title.



16
17
18
# File 'lib/kanye/track.rb', line 16

def title
  @title
end

Instance Method Details

#download!Object

Raises:



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
# File 'lib/kanye/track.rb', line 22

def download!
  raise(NoKeyError, "Couldn't find :key for '#{self}'") if key.nil?

  response = HTTParty.get(url, :headers => {'cookie' => cookie})
  raise "Response Code '#{response.code}' - Something has changed." unless response.code == 200

  print self, "[ Downloading…"

  mp3_url = URI.escape(response.parsed_response["url"])

  if mp3_url =~ /http:\/\/.*\.s3\.amazonaws\.com\//
    mp3_url = correct_s3_url!(mp3_url)
  end

  puts mp3_url if Kanye.config["debug"]

  mp3_response = HTTParty.get(mp3_url)

  File.open(filename, "wb") do |f|
    f.write(mp3_response.parsed_response)
  end

  set_id3_tags!
  puts " complete ]"
end

#filenameObject



54
55
56
57
# File 'lib/kanye/track.rb', line 54

def filename
  name = [artist,title].join('-').gsub(/[ \/]/, "-").downcase
  File.join(Kanye.download_path, name + ".mp3")
end

#to_sObject



48
49
50
51
52
# File 'lib/kanye/track.rb', line 48

def to_s
  t = truncate(title,  42, '').force_encoding('utf-8').ljust(42)
  a = truncate(artist, 42, '').force_encoding('utf-8').ljust(32)
  [t, "| ", a].join
end

#urlObject



18
19
20
# File 'lib/kanye/track.rb', line 18

def url
  "http://#{BASE_URL}/serve/source/" + id + '/' + key
end