Class: Subsonic::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/subsonic-api/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
32
33
34
35
36
# File 'lib/subsonic-api/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

  if(@api_version == '1.5.0')
    @uri_prepend = '/rest'
  end

  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-api/client.rb', line 14

def api_version
  @api_version
end

#uri_prependObject

Returns the value of attribute uri_prepend.



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

def uri_prepend
  @uri_prepend
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

#versionObject

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#add_song(*ids) ⇒ Object



68
69
70
71
72
73
# File 'lib/subsonic-api/client.rb', line 68

def add_song(*ids)
  count = ids.length
  ids = ids.join(',').gsub(/\s/, '')
  response = self.class.post(@uri_prepend + '/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-api/client.rb', line 50

def messages
  response = self.class.get(@uri_prepend + '/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



38
39
40
41
42
43
# File 'lib/subsonic-api/client.rb', line 38

def now_playing
  response = self.class.get(@uri_prepend + '/getNowPlaying.view')
  if response.code == 200
    response.parsed_response['subsonic-response']['nowPlaying']['entry']
  end
end

#random_songsObject



61
62
63
64
65
66
# File 'lib/subsonic-api/client.rb', line 61

def random_songs
  response = self.class.get(@uri_prepend + '/getRandomSongs')
  if response.code == 200
    response['subsonic-response']['randomSongs']['song']
  end
end

#say(message) ⇒ Object



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

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