Class: RJL::Itunes

Inherits:
Object
  • Object
show all
Defined in:
lib/rjl/itunes.rb

Overview

RJL_itunes::Itunes contains the public api for Itunes.

Examples:

require 'rjl/itunes'

itunes = RJL::Itunes.new
albums = itunes.albums

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(playlist = nil) ⇒ Itunes

Create a new RJL_itunes::Itunes. Can be called with a Playlist name to only get the tracks in that Playlist - useful for testing, and may be deprecated

Parameters:

  • playlist (String) (defaults to: nil)

    optional name of a Playlist to open



22
23
24
25
26
27
# File 'lib/rjl/itunes.rb', line 22

def initialize( playlist = nil )
  @tracks = get_tracks( playlist )
  @albums = get_albums( @tracks )
  @playlists = fetch_playlists( kind: :none )
  @playlist_folders = fetch_playlists( kind: :none )
end

Instance Attribute Details

#albums(album_artist: nil, title: nil) ⇒ List of Album, Album (readonly)

The albums, or a single album

Examples:

albums = itunes.albums
puts albums.count # => 5

album = itunes.albums(album_artist: "Simply Red", title: "Greatest Hits" )
puts album.genre # => "Pop/Rock"

Parameters:

  • artist (String)

    the album artist

  • title (String) (defaults to: nil)

    the album title

Returns:

  • (List of Album)

    if more than one album

  • (Album)

    if only one album



46
47
48
# File 'lib/rjl/itunes.rb', line 46

def albums
  @albums
end

#versionString (readonly)

iTunes version number

Returns:

  • (String)

    the iTunes application version number.



31
32
33
# File 'lib/rjl/itunes.rb', line 31

def version
  @version
end

Instance Method Details

#create_playlist(playlist_name: "Playlist", track_list: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/rjl/itunes.rb', line 79

def create_playlist( playlist_name: "Playlist", track_list: nil )
  playlist_obj = app("iTunes").make(
    :new => :playlist,
    :with_properties => {:name => playlist_name}
    )
  playlist = Playlist.new( playlist_obj )
  playlist.tracks = track_list unless track_list.nil?
  @playlists << playlist
  return playlist
end

#create_playlist_folder(folder_name: "untitled folder", playlists: nil) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/rjl/itunes.rb', line 95

def create_playlist_folder( folder_name: "untitled folder", playlists: nil )
  playlist_obj = app("iTunes").make(
    :new => :folder_playlist,
    :with_properties => {:name => folder_name}
    )
  # TODO add playlists
  # TODO make and add to @playlist_folders
end

#destroy_playlist(playlist_name) ⇒ Object



90
91
92
93
# File 'lib/rjl/itunes.rb', line 90

def destroy_playlist( playlist_name )
  app("iTunes").sources["Library"].user_playlists[playlist_name].delete
  # TODO remove from @playlists
end

#destroy_playlist_folder(folder_name: nil) ⇒ Object



104
105
106
# File 'lib/rjl/itunes.rb', line 104

def destroy_playlist_folder( folder_name: nil )
  app("iTunes").sources["Library"].folder_playlists[folder_name].delete
end

#playlist(playlist_name) ⇒ Playlist

Get a playlist of the specified name. Raises error if more than one.

Parameters:

  • playlist_name (String)

    The name of the playlist to get

Returns:



70
71
72
73
74
75
76
77
# File 'lib/rjl/itunes.rb', line 70

def playlist( playlist_name )
  play_list = @playlists.select { |playlist| playlist.name == playlist_name }
  if play_list.count == 1
    return play_list[0]
  else
    raise "ERROR: #{play_list.count} playlists with name #{playlist_name}"
  end
end

#playlistsList of Playlist

Return a list of the playlists.

Returns:



63
64
65
# File 'lib/rjl/itunes.rb', line 63

def playlists
  return @playlists
end