Class: VkMusic::Audio
- Inherits:
-
Object
- Object
- VkMusic::Audio
- Defined in:
- lib/vk_music/audio.rb
Overview
Class representing VK audio
Instance Attribute Summary collapse
-
#artist ⇒ String
readonly
Name of artist.
-
#duration ⇒ Integer
readonly
Duration of track in seconds.
-
#title ⇒ String
readonly
Title of song.
-
#url_encoded ⇒ String?
readonly
Encoded URL which can be manually decoded if client ID is known.
Instance Method Summary collapse
- #full_id ⇒ String?
-
#id_matches?(data) ⇒ Boolean
Id-based comparison.
-
#initialize(id: nil, owner_id: nil, secret1: nil, secret2: nil, artist: '', title: '', duration: 0, url_encoded: nil, url: nil, client_id: nil) ⇒ Audio
constructor
Initialize new audio.
-
#like?(audio) ⇒ Boolean
Whether artist, title and duration are same.
-
#to_s ⇒ String
Pretty-printed audio name.
-
#update(audio) ⇒ Object
Update audio data from another one.
- #url ⇒ String?
-
#url_accessable? ⇒ Boolean
Whether it’s possible to get download URL with Client#from_id.
-
#url_available? ⇒ Boolean
Whether able to get download URL without web requests.
-
#url_cached? ⇒ Boolean
Whether URL saved into url attribute.
Constructor Details
#initialize(id: nil, owner_id: nil, secret1: nil, secret2: nil, artist: '', title: '', duration: 0, url_encoded: nil, url: nil, client_id: nil) ⇒ Audio
Initialize new audio
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vk_music/audio.rb', line 26 def initialize(id: nil, owner_id: nil, secret1: nil, secret2: nil, artist: '', title: '', duration: 0, url_encoded: nil, url: nil, client_id: nil) @id = id @owner_id = owner_id @secret1 = secret1 @secret2 = secret2 @artist = artist.to_s.strip @title = title.to_s.strip @duration = duration @url_encoded = url_encoded @url_decoded = url @client_id = client_id end |
Instance Attribute Details
#artist ⇒ String (readonly)
Returns name of artist.
7 8 9 |
# File 'lib/vk_music/audio.rb', line 7 def artist @artist end |
#duration ⇒ Integer (readonly)
Returns duration of track in seconds.
11 12 13 |
# File 'lib/vk_music/audio.rb', line 11 def duration @duration end |
#title ⇒ String (readonly)
Returns title of song.
9 10 11 |
# File 'lib/vk_music/audio.rb', line 9 def title @title end |
#url_encoded ⇒ String? (readonly)
Returns encoded URL which can be manually decoded if client ID is known.
13 14 15 |
# File 'lib/vk_music/audio.rb', line 13 def url_encoded @url_encoded end |
Instance Method Details
#full_id ⇒ String?
63 64 65 66 67 |
# File 'lib/vk_music/audio.rb', line 63 def full_id return unless @id && @owner_id && @secret1 && @secret2 "#{@owner_id}_#{@id}_#{@secret1}_#{@secret2}" end |
#id_matches?(data) ⇒ Boolean
Returns id-based comparison.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/vk_music/audio.rb', line 92 def id_matches?(data) data_owner_id, data_id = case data when Audio then [data.owner_id, data.id] when Array then data.first(2).reverse.map(&:to_i) when String then data.split('_').first(2).map(&:to_i) else return false end owner_id == data_owner_id && id == data_id end |
#like?(audio) ⇒ Boolean
Returns whether artist, title and duration are same.
86 87 88 |
# File 'lib/vk_music/audio.rb', line 86 def like?(audio) artist == audio.artist && title == audio.title && duration == audio.duration end |
#to_s ⇒ String
Returns pretty-printed audio name.
104 105 106 |
# File 'lib/vk_music/audio.rb', line 104 def to_s "#{@artist} - #{@title} [#{@duration}s]" end |
#update(audio) ⇒ Object
Update audio data from another one
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/vk_music/audio.rb', line 51 def update(audio) VkMusic.log.warn('Audio') { "Performing update of #{self} from #{audio}" } unless like?(audio) @id = audio.id @owner_id = audio.owner_id @secret1 = audio.secret1 @secret2 = audio.secret2 @url_encoded = audio.url_encoded @url_decoded = audio.url_decoded @client_id = audio.client_id end |
#url ⇒ String?
42 43 44 45 46 47 48 |
# File 'lib/vk_music/audio.rb', line 42 def url return @url_decoded if @url_decoded return unless @url_encoded && @client_id Utility::LinkDecoder.call(@url_encoded, @client_id) end |
#url_accessable? ⇒ Boolean
Returns whether it’s possible to get download URL with Client#from_id.
80 81 82 |
# File 'lib/vk_music/audio.rb', line 80 def url_accessable? !!full_id end |
#url_available? ⇒ Boolean
Returns whether able to get download URL without web requests.
75 76 77 |
# File 'lib/vk_music/audio.rb', line 75 def url_available? url_cached? || !!(@url_encoded && @client_id) end |
#url_cached? ⇒ Boolean
Returns whether URL saved into url attribute.
70 71 72 |
# File 'lib/vk_music/audio.rb', line 70 def url_cached? !!@url_decoded end |