Class: Hallon::Link

Inherits:
Base
  • Object
show all
Defined in:
lib/hallon/link.rb

Overview

Wraps Spotify URIs in a class, giving access to methods performable on them.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from, from_link, #is_linkable?, #session, to_link, #to_pointer

Constructor Details

#initialize(uri) ⇒ Link

Note:

You must initialize a Session before you call this method.

Parse the given Spotify URI into a Link.

Parameters:

Raises:

  • (ArgumentError)

    link could not be parsed


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).

Parameters:

  • spotify_uri (#to_s)

Returns:

  • (Boolean)

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.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

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

#lengthFixnum

Returns spotify URI length.

Returns:

  • (Fixnum)

    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.

Parameters:

  • expected_type (Symbol) (defaults to: nil)

    if given, makes sure the link is of this type

Returns:

  • (Spotify::ManagedPointer)

    the underlying Spotify::ManagedPointer.

Raises:

  • ArgumentError if type is given and does not match link #type


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_sString

Returns string representation of the Link.

Returns:

  • (String)

    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.

Parameters:

  • length (Fixnum) (defaults to: length)

    truncate to this size

Returns:

  • (String)

    spotify URI representation of this Link.

See Also:


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_urlString

Returns full Spotify HTTP URL.

Returns:

  • (String)

    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

#typeSymbol

Returns link type as a symbol (e.g. :playlist).

Returns:

  • (Symbol)

    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