Class: Subsonic::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/subsonic-rb/client.rb

Constant Summary collapse

API_VERSION =
{
  '3.8'   => '1.0.0',
  '3.9'   => '1.1.1',
  '4.0'   => '1.2.0',
  '4.1'   => '1.3.0',
  '4.2'   => '1.4.0',
  '4.3.1' => '1.5.0'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, username, password, options = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/subsonic-rb/client.rb', line 16

def initialize(url, username, password, options={})
  pass_prefix = options[:enc] ? "enc:" : ""
  version = @version = options[:version] || API_VERSION.values.last
  @api_version = API_VERSION[@version] || version
  format = options[:format] || "json"

  Struct.new("User", :username, :password)
  @user = Struct::User.new(username, "#{pass_prefix}#{password}")
  username, password = @user.username, @user.password

  self.class.class_eval do
    base_uri url
    default_params :u => username, :p => password,
                   :v => version,   :f => format, :c => "subsonic-rb.gem"
  end
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



14
15
16
# File 'lib/subsonic-rb/client.rb', line 14

def api_version
  @api_version
end

#urlObject

Returns the value of attribute url.



14
15
16
# File 'lib/subsonic-rb/client.rb', line 14

def url
  @url
end

#userObject

Returns the value of attribute user.



14
15
16
# File 'lib/subsonic-rb/client.rb', line 14

def user
  @user
end

#versionObject

Returns the value of attribute version.



14
15
16
# File 'lib/subsonic-rb/client.rb', line 14

def version
  @version
end

Instance Method Details

#add_song(*ids) ⇒ Object



72
73
74
75
76
77
# File 'lib/subsonic-rb/client.rb', line 72

def add_song(*ids)
  count = ids.length
  ids = ids.join(',').gsub(/\s/, '')
  response = self.class.post('/jukeboxControl.view', :query => {:action => 'add', :id => ids})
  response.code == 200 ? "#{count} songs added" : false
end

#messagesObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/subsonic-rb/client.rb', line 50

def messages
  response = self.class.get('/getChatMessages.view')
  if response.code == 200
    chat_messages = response.parsed_response['subsonic-response']['chatMessages']['chatMessage']
    chat_messages.map do |msg|
      time = Time.at(msg['time'] / 1000)
      "[#{time.strftime("%b-%e")}] #{msg['username']}: #{msg['message']}"
    end
  end
end

#now_playingObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/subsonic-rb/client.rb', line 33

def now_playing
  response = self.class.get('/getNowPlaying.view')
  if response.code == 200
    now_playing = response.parsed_response['subsonic-response']['nowPlaying']['entry']
    if now_playing.is_a? Array
      now_playing.map {|entry| "#{entry['artist']} - #{entry['title']}"}
    else
      "#{now_playing['artist']} - #{now_playing['title']}"
    end
  end
end

#random_songsObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/subsonic-rb/client.rb', line 61

def random_songs
  response = self.class.get('/getRandomSongs')
  if response.code == 200
    songs = response['subsonic-response']['randomSongs']['song']
    songs.map do |song|
      {:song => "#{song['artist']} - #{song['title']}",
       :id => song['id']}
    end
  end
end

#say(message) ⇒ Object



45
46
47
48
# File 'lib/subsonic-rb/client.rb', line 45

def say(message)
  response = self.class.post('/addChatMessage.view', :query => {:message => message})
  response.code == 200 ? message : false
end