Class: VkMusic::Audio

Inherits:
Object
  • Object
show all
Defined in:
lib/vk_music/audio.rb

Overview

Class representing VK audio

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • id (Integer, nil) (defaults to: nil)
  • owner_id (Integer, nil) (defaults to: nil)
  • secret1 (String, nil) (defaults to: nil)
  • secret2 (String, nil) (defaults to: nil)
  • artist (String) (defaults to: '')
  • title (String) (defaults to: '')
  • duration (Integer) (defaults to: 0)
  • url_encoded (String, nil) (defaults to: nil)
  • url (String, nil) (defaults to: nil)

    decoded URL

  • client_id (Integer, nil) (defaults to: nil)


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

#artistString (readonly)

Returns name of artist.

Returns:

  • (String)

    name of artist



7
8
9
# File 'lib/vk_music/audio.rb', line 7

def artist
  @artist
end

#durationInteger (readonly)

Returns duration of track in seconds.

Returns:

  • (Integer)

    duration of track in seconds



11
12
13
# File 'lib/vk_music/audio.rb', line 11

def duration
  @duration
end

#titleString (readonly)

Returns title of song.

Returns:

  • (String)

    title of song



9
10
11
# File 'lib/vk_music/audio.rb', line 9

def title
  @title
end

#url_encodedString? (readonly)

Returns encoded URL which can be manually decoded if client ID is known.

Returns:

  • (String?)

    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_idString?

Returns:

  • (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.

Parameters:

  • (Audio, Array(owner_id, audio_id, secret1, secret2), String)

Returns:

  • (Boolean)

    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.

Parameters:

Returns:

  • (Boolean)

    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_sString

Returns pretty-printed audio name.

Returns:

  • (String)

    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

#urlString?

Returns:

  • (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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    whether URL saved into url attribute



70
71
72
# File 'lib/vk_music/audio.rb', line 70

def url_cached?
  !!@url_decoded
end