lib/grooveshark/request.rb in grooveshark-0.2.1 vs lib/grooveshark/request.rb in grooveshark-0.2.2
- old
+ new
@@ -1,20 +1,23 @@
module Grooveshark
module Request
- API_BASE = 'cowbell.grooveshark.com'
- UUID = 'E2AB1A59-C6B7-480E-992A-55DE1699D7F8'
- CLIENT = 'htmlshark'
- CLIENT_REV = '20101012.37'
- COUNTRY = {"CC2" => "0","IPR" => "1","CC1" => "0","ID" => "1","CC4" => "0","CC3" => "0"}
+ API_BASE = 'cowbell.grooveshark.com'
+ UUID = 'A3B724BA-14F5-4932-98B8-8D375F85F266'
+ CLIENT = 'htmlshark'
+ CLIENT_REV = '20110606.04'
+ COUNTRY = {"CC2" => "0", "IPR" => "353", "CC4" => "1073741824", "CC3" => "0", "CC1" => "0", "ID" => "223"}
+ TOKEN_TTL = 120 # 2 minutes
# Client overrides for different methods
METHOD_CLIENTS = {
'getStreamKeyFromSongIDEx' => 'jsqueue'
}
# Perform API request
def request(method, params={}, secure=false)
+ refresh_token if @comm_token
+
agent = METHOD_CLIENTS.key?(method) ? METHOD_CLIENTS[method] : CLIENT
url = "#{secure ? 'https' : 'http'}://#{API_BASE}/more.php?#{method}"
body = {
'header' => {
'session' => @session,
@@ -39,9 +42,19 @@
raise GeneralError # Need define error handling
end
data = JSON.parse(data)
data = data.normalize if data.kind_of?(Hash)
- return data['result'] unless data['fault']
+
+ if data.key?('fault')
+ raise ApiError.new(data['fault'])
+ else
+ data['result']
+ end
+ end
+
+ # Refresh communications token on ttl
+ def refresh_token
+ get_comm_token if Time.now.to_i - @comm_token_ttl > TOKEN_TTL
end
end
end
\ No newline at end of file