Module: Rapid::Services::Requests::PlaylistReferenceParser

Included in:
MockPrp, PlayerHandler, PlaylistHandler
Defined in:
lib/ury_rapid/services/requests/playlist_reference_parser.rb

Overview

Mixin for anything that needs to parse playlist references

Playlist references refer to items in playlists inside the model's playlist set, by their position indices and playlist IDs respectively.

A playlist reference is either a local or foreign playlist reference:

  • Local references are the pseudo-URL body '<playlist-index>' or the hash { index: <playlist-index> }, and reference an index in the playlist whose ID is returned by #local_playlist_id;

  • Foreign references are the pseudo-URL body '<playlist-id>/<playlist-index>' or the hash { playlist: <playlist-id>, index: <playlist-index>, and reference an index in the playlist with the specified ID.

Implementors must implement the method #local_playlist_id to return the ID of the 'current' playlist from the implementor's perspective, or raise an error if there is no such playlist.

Instance Method Summary collapse

Instance Method Details

#local_playlist?(id) ⇒ Boolean

Given a playlist ID, determines whether it is the local playlist

Examples:

Passing the local playlist ID.

p.local_playlist_id
#=> :local_id
p.local_playlist?(:local_id)
#=> true

Passing a foreign playlist ID.

p.local_playlist_id
#=> :local_id
p.local_playlist?(:foreign_id)
#=> false

Parameters:

  • id (Symbol)

    The playlist ID to check.

Returns:

  • (Boolean)

    True if the playlist is local; false otherwise.


40
41
42
# File 'lib/ury_rapid/services/requests/playlist_reference_parser.rb', line 40

def local_playlist?(id)
  id == local_playlist_id
end

#parse_playlist_reference_hash(hash) ⇒ Array

Parses a hash as a playlist reference

Examples:

Parsing a local playlist reference.

p.local_playlist_id
#=> :local_id
p.parse_playlist_reference_hash({index: 20})
#=> [:local_id, 20]

Parsing a foreign playlist reference.

p.local_playlist_id
#=> :local_id
p.parse_playlist_reference_hash({playlist: :foreign_id, index: 20})
#=> [:foreign_id, 20]

Parameters:

  • hash (Hash)

    The hash to parse.

Returns:

  • (Array)

    A tuple containing the playlist ID and index referred to by this reference.


86
87
88
89
90
# File 'lib/ury_rapid/services/requests/playlist_reference_parser.rb', line 86

def parse_playlist_reference_hash(hash)
  playlist = hash.fetch(:playlist, local_playlist_id)
  index    = hash.fetch(:index)
  [to_valid_id(playlist), Integer(index)]
end

#parse_playlist_reference_url(url) ⇒ Array

Parses a URL body as a playlist reference

Examples:

Parsing a local playlist reference.

parser.local_playlist_id
#=> :local_id
parser.parse_playlist_reference_url('20')
#=> [:local_id, 20]

Parsing a foreign playlist reference.

p.local_playlist_id
#=> :local_id
p.parse_playlist_reference_url('foreign_id/20')
#=> [:foreign_id, 20]

Parameters:

  • url (String)

    The URL body to parse. This should not have the protocol, as this should already have been dealt with.

Returns:

  • (Array)

    A tuple containing the playlist ID and index referred to by this reference.


63
64
65
66
# File 'lib/ury_rapid/services/requests/playlist_reference_parser.rb', line 63

def parse_playlist_reference_url(url)
  playlist, index = split_url(url)
  [to_valid_id(playlist), Integer(index)]
end