Class: Hallon::Link
Overview
Wraps Spotify URIs in a class, giving access to methods performable on them.
Class Method Summary collapse
-
.valid?(spotify_uri) ⇒ Boolean
True if the given Spotify URI is valid (parsable by libspotify).
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compare the Link to other.
-
#initialize(uri) ⇒ Link
constructor
Parse the given Spotify URI into a Link.
-
#length ⇒ Fixnum
Spotify URI length.
-
#pointer(expected_type = nil) ⇒ Spotify::ManagedPointer
The underlying Spotify::ManagedPointer.
-
#to_s ⇒ String
String representation of the Link.
-
#to_str(length = length) ⇒ String
(also: #to_uri)
Spotify URI representation of this Link.
-
#to_url ⇒ String
Full Spotify HTTP URL.
-
#type ⇒ Symbol
Link type as a symbol (e.g.
:playlist
).
Methods inherited from Base
from, from_link, #is_linkable?, #session, to_link, #to_pointer
Constructor Details
#initialize(uri) ⇒ Link
You must initialize a Session before you call this method.
Parse the given Spotify URI into a Link.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hallon/link.rb', line 29 def initialize(uri) unless Session.instance? raise NoSessionError, "You must have initialized a session to create links" end # we support any #to_linkāable object if uri.respond_to?(:to_link) uri = uri.to_link.pointer end @pointer = to_pointer(uri, Spotify::Link) do Spotify.link_create_from_string(uri.to_str) end end |
Class Method Details
.valid?(spotify_uri) ⇒ Boolean
True if the given Spotify URI is valid (parsable by libspotify).
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/hallon/link.rb', line 11 def self.valid?(spotify_uri) unless Session.instance? raise NoSessionError, "You must have initialized a session to create links" end if spotify_uri.is_a?(Link) true else link = Spotify.link_create_from_string(spotify_uri.to_s) not link.null? end end |
Instance Method Details
#==(other) ⇒ Boolean
Compare the Link to other. If other is a Link, also compare
their to_str
if necessary.
76 77 78 79 80 |
# File 'lib/hallon/link.rb', line 76 def ==(other) super or if other.is_a?(Link) to_str == other.to_str end end |
#length ⇒ Fixnum
Returns spotify URI length.
50 51 52 |
# File 'lib/hallon/link.rb', line 50 def length Spotify.link_as_string(pointer, nil, 0) end |
#pointer(expected_type = nil) ⇒ Spotify::ManagedPointer
Returns the underlying Spotify::ManagedPointer.
90 91 92 93 94 95 |
# File 'lib/hallon/link.rb', line 90 def pointer(expected_type = nil) unless type == expected_type or (expected_type == :playlist and type == :starred) raise ArgumentError, "expected #{expected_type} link, but it is of type #{type}" end if expected_type super() end |
#to_s ⇒ String
Returns string representation of the Link.
83 84 85 |
# File 'lib/hallon/link.rb', line 83 def to_s "<#{self.class.name} #{to_str}>" end |
#to_str(length = length) ⇒ String Also known as: to_uri
Returns spotify URI representation of this Link.
57 58 59 60 61 62 |
# File 'lib/hallon/link.rb', line 57 def to_str(length = length) FFI::Buffer.alloc_out(length + 1) do |b| Spotify.link_as_string(pointer, b, b.size) return b.get_string(0).force_encoding("UTF-8") end end |
#to_url ⇒ String
Returns full Spotify HTTP URL.
67 68 69 |
# File 'lib/hallon/link.rb', line 67 def to_url "http://open.spotify.com/%s" % to_str[8..-1].gsub(':', '/') end |
#type ⇒ Symbol
Returns link type as a symbol (e.g. :playlist
).
45 46 47 |
# File 'lib/hallon/link.rb', line 45 def type Spotify.link_type(pointer) end |