Class: Nehm::Track
- Inherits:
-
Object
- Object
- Nehm::Track
- Defined in:
- lib/nehm/track.rb
Overview
Primitive for SoundCloud track
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #artist ⇒ Object
- #artwork ⇒ Object
- #duration ⇒ Object
- #file_name ⇒ Object
- #file_path ⇒ Object
- #full_name ⇒ Object
- #id ⇒ Object
-
#initialize(hash) ⇒ Track
constructor
A new instance of Track.
-
#name ⇒ Object
Returns artist and title in array.
- #title ⇒ Object
- #url ⇒ Object
- #year ⇒ Object
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
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
9 10 11 |
# File 'lib/nehm/track.rb', line 9 def hash @hash end |
Instance Method Details
#artist ⇒ Object
15 16 17 |
# File 'lib/nehm/track.rb', line 15 def artist name[0] end |
#duration ⇒ Object
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_name ⇒ Object
32 33 34 |
# File 'lib/nehm/track.rb', line 32 def file_name "#{full_name.tr(",./'\\\"$%", '')}.mp3" end |
#file_path ⇒ Object
36 37 38 |
# File 'lib/nehm/track.rb', line 36 def file_path File.join(ENV['dl_path'], file_name) end |
#full_name ⇒ Object
40 41 42 |
# File 'lib/nehm/track.rb', line 40 def full_name "#{artist} - #{title}" end |
#id ⇒ Object
44 45 46 |
# File 'lib/nehm/track.rb', line 44 def id @hash['id'] end |
#name ⇒ Object
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 |
#title ⇒ Object
60 61 62 |
# File 'lib/nehm/track.rb', line 60 def title name[1] end |
#url ⇒ Object
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 |
#year ⇒ Object
70 71 72 |
# File 'lib/nehm/track.rb', line 70 def year @hash['created_at'][0..3].to_i end |