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.blank?

  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!
  print " complete ]"
end

#filenameObject



52
53
54
55
# File 'lib/kanye/track.rb', line 52

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

#to_sObject



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

def to_s
  [truncate(title, 42, '').ljust(42), "| ", truncate(artist, 42, '').ljust(32)].join
end

#urlObject



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

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