Class: Nehm::Track

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

Overview

Primitive for SoundCloud track

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Track

Returns a new instance of Track.



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

def initialize(hash)
  @hash = hash
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



9
10
11
# File 'lib/nehm/track.rb', line 9

def hash
  @hash
end

Instance Method Details

#artistObject



15
16
17
# File 'lib/nehm/track.rb', line 15

def artist
  name[0]
end

#artworkObject



19
20
21
# File 'lib/nehm/track.rb', line 19

def artwork
  Artwork.new(self)
end

#durationObject



23
24
25
26
27
28
29
30
# File 'lib/nehm/track.rb', line 23

def duration
  seconds = @hash['duration'] / 1000

  time = Time.at(seconds)
  time -= time.utc_offset

  time.hour > 0 ? time.strftime('%H:%M:%S') : time.strftime('%M:%S')
end

#file_nameObject



32
33
34
# File 'lib/nehm/track.rb', line 32

def file_name
  "#{full_name.tr(",./'\\\"$%", '')}.mp3"
end

#file_pathObject



36
37
38
# File 'lib/nehm/track.rb', line 36

def file_path
  File.join(ENV['dl_path'], file_name)
end

#full_nameObject



40
41
42
# File 'lib/nehm/track.rb', line 40

def full_name
  "#{artist} - #{title}"
end

#idObject



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

def id
  @hash['id']
end

#nameObject

Returns artist and title in array



49
50
51
52
53
54
55
56
57
58
# File 'lib/nehm/track.rb', line 49

def name
  title = @hash['title']

  separators = [' - ', ' ~ ']
  separators.each do |sep|
    return title.split(sep, 2) if title.include? sep
  end

  [@hash['user']['username'], title]
end

#titleObject



60
61
62
# File 'lib/nehm/track.rb', line 60

def title
  name[1]
end

#urlObject



64
65
66
67
68
# File 'lib/nehm/track.rb', line 64

def url
  # API V2 track hash has no 'stream_url' but 'uri'
  dl_url = @hash['uri'] ? "#{@hash['uri']}/stream" : @hash['stream_url']
  "#{dl_url}?client_id=#{HTTPClient::CLIENT_ID}"
end

#yearObject



70
71
72
# File 'lib/nehm/track.rb', line 70

def year
  @hash['created_at'][0..3].to_i
end