class Rdioid::Client
Once Rdioid is configured with all required values, Rdioid::Client is able to make requests to the Web Service API.
Attributes
Public Class Methods
Initializes a new Client
for making OAuth and Web Service API requests.
# File lib/rdioid/client.rb, line 69 def initialize self.http_client = HTTPClient.new(:base_url => Rdioid::BASE_URL, :force_basic_auth => true) http_client.set_auth(Rdioid::OAUTH_TOKEN_ENDPOINT, Rdioid.config.client_id, Rdioid.config.client_secret) end
Public Instance Methods
Sends a request to the Web Service API.
@param access_token [String] OAuth token required for sending requests to the Web Service API @param body [Hash] values to send in the body of the request @option body [String] :method method for the request: www.rdio.com/developers/docs/web-service/methods/ @option body [Hash] :* additional Arguments unique to each method
@return [Hash] response from the Web Service API
@example
rdioid_client = Rdioid::Client.new access_token = 'AAWEAAVWMgAAAABVsG3t' rdioid_client.api_request(access_token) # => { "status" => "error", "message" => "You must pass a method name as an HTTP POST parameter named \"method\".", "code" => 400 } rdioid_client.api_request(access_token, :method => 'getTopCharts', :type => 'Artist', :count => 3, :extras => '-*,name') # => { "status" => "ok", "result" => [{ "name" => "Future" }, { "name" => "Tame Impala" }, { "name" => "Ratatat" }] } rdioid_client.api_request(access_token, :method => 'searchSuggestions', :query => 'Mac', :types => 'Artist', :count => 3, :extras => '-*,name') # => { "status" => "ok", "result" => [{ "name" => "Macklemore & Ryan Lewis" }, { "name" => "Mac Miller" }, { "name" => "Macy Gray" }] } rdioid_client.api_request(access_token, :method => 'getAlbumsInCollection', :extras => '-*,name') # => { "status" => "ok", "result" => [{ "name" => "Adore" }, { "name" => "Against The Grain (Reissue)" }, { "name" => "Agony & Irony" }] } rdioid_client.api_request(access_token, :method => 'getFavorites') # => { "error_description" => "Invalid or expired access token", "error" => "invalid_token" }
# File lib/rdioid/client.rb, line 104 def api_request(access_token, body = {}) header = { :Authorization => "Bearer #{access_token}" } request(Rdioid::API_ENDPOINT, :header => header, :body => body) end
Requests an OAuth device_code
for the Device Code Grant.
@param body [Hash] additional request values @option body [String] :scope the desired scope you wish to have access to
@return [Hash] response from the Web Service API
@example
rdioid_client = Rdioid::Client.new rdioid_client.request_device_code # => { "expires_in_s" => 1800, "device_code" => "2479RA", "interval_s" => 5, "verification_url" => "rdio.com/device" }
# File lib/rdioid/client.rb, line 126 def request_device_code(body = {}) body[:client_id] = Rdioid.config.client_id request(Rdioid::OAUTH_DEVICE_CODE_ENDPOINT, :body => body) end
Requests an OAuth access_token
using the Client Credentials.
@param body [Hash] additional request values @option body [String] :scope the desired scope you wish to have access to
@return [Hash] response from the Web Service API
@example
rdioid_client = Rdioid::Client.new rdioid_client.request_token_with_client_credentials # => { "access_token" => "AAAdmanFxdWxlayip", "token_type" => "bearer", "expires_in" => 43200, "scope" => "" }
# File lib/rdioid/client.rb, line 173 def request_token_with_client_credentials(body = {}) body[:grant_type] = 'client_credentials' request(Rdioid::OAUTH_TOKEN_ENDPOINT, :body => body) end
Requests an OAuth access_token
using the Device Code Grant.
@param device_code [String] device_code
received from calling #request_device_code
@param body [Hash] additional request values @option body [String] :scope the desired scope you wish to have access to
@return [Hash] response from the Web Service API
@example
rdioid_client = Rdioid::Client.new device_code = 'BX55E2' rdioid_client.request_token_with_device_code(device_code) # => { "error_description" => "user has not approved this code yet", "error" => "pending_authorization" } rdioid_client.request_token_with_device_code(device_code) # => { "access_token" => "AAAA3lB6RbI3l8", "token_type" => "bearer", "expires_in" => 43200, "refresh_token" => "AAAFxdWxlbX1z", "scope" => "" } rdioid_client.request_token_with_device_code(device_code) # => { "error_description" => "no entry found for given device_code", "error" => "invalid_request" }
# File lib/rdioid/client.rb, line 201 def request_token_with_device_code(device_code, body = {}) body[:grant_type] = 'device_code' body[:device_code] = device_code request(Rdioid::OAUTH_TOKEN_ENDPOINT, :body => body) end
Requests an OAuth access_token
using the Resource Owner Credential.
@param username [String] email address for the User @param password [String] password for the User @param body [Hash] additional request values @option body [String] :scope the desired scope you wish to have access to
@return [Hash] response from the Web Service API
@example
rdioid_client = Rdioid::Client.new username = 'rdioid@test.com' password = 'ruby<3' rdioid_client.request_token_with_password(username, password) # => { "access_token" => "AAp2Y2dmWxlan", "token_type" => "bearer", "expires_in" => 43200, "refresh_token" => "AAAX1z4mNk84", "scope" => "" } rdioid_client.request_token_with_password(username, password) # => { "error_description" => "This client is not authorized to use the password grant", "error" => "unauthorized_client" }
# File lib/rdioid/client.rb, line 229 def request_token_with_password(username, password, body = {}) body[:grant_type] = 'password' body[:username] = username body[:password] = password request(Rdioid::OAUTH_TOKEN_ENDPOINT, :body => body) end
Requests an OAuth access_token
using the Refresh Token.
@param refresh_token [String] refresh token previously issued during an access_token
request @param body [Hash] additional request values @option body [String] :scope the desired scope you wish to have access to
@return [Hash] response from the Web Service API
@example
rdioid_client = Rdioid::Client.new refresh_token = 'AAxlayVmNkMzwlYY64TNB' rdioid_client.request_token_with_refresh_token(refresh_token) # => { "access_token" => "AAJ3bXHQWqh5ueD6", "token_type" => "bearer", "expires_in" => 43200, "refresh_token" => "AAAoyYWJ3beClfGsm", "scope" => "" } rdioid_client.request_token_with_refresh_token(refresh_token) # => { "error_description" => "invalid refresh token", "error" => "invalid_grant" }
# File lib/rdioid/client.rb, line 256 def request_token_with_refresh_token(refresh_token, body = {}) body[:grant_type] = 'refresh_token' body[:refresh_token] = refresh_token request(Rdioid::OAUTH_TOKEN_ENDPOINT, :body => body) end
Private Instance Methods
# File lib/rdioid/client.rb, line 266 def request(endpoint, options = {}) response = http_client.post(endpoint, options) JSON.parse(response.body) end