class Napster::Client

The Client class implements a client object that prepares information such as api_key, api_secret, and :redirect_uri needed to call Napster API.

Constants

AUTH_METHODS
MODELS_LIST

Attributes

access_token[RW]
api_key[RW]
api_secret[RW]
auth_code[RW]
expires_in[RW]
password[RW]
redirect_uri[RW]
refresh_token[RW]
request[RW]
state[RW]
username[RW]

Public Class Methods

new(options) click to toggle source

Instantiate a client object @note request attribute is always overwritten by Napster::Request

object.

@param options [Hash] Required options are :api_key and :api_secret

# File lib/napster/client.rb, line 26
def initialize(options)
  options.each do |name, value|
    instance_variable_set("@#{name}", value)
  end

  request_hash = {
    api_key: @api_key,
    api_secret: @api_secret
  }
  @request = Napster::Request.new(request_hash)
  authenticate_client
  set_models
end

Public Instance Methods

authenticate(auth_method) click to toggle source

Main method for authenticating against Napster API @param auth_method [Symbol] authentication methods that are

:password_grant or :oauth2

@return [Hash] response from Napster API

# File lib/napster/client.rb, line 131
def authenticate(auth_method)
  validate_authenticate(auth_method)

  return auth_password_grant if auth_method == :password_grant
  return auth_oauth2 if auth_method == :oauth2
end
authentication_url() click to toggle source

Get URL for OAuth2 authentication flow @return [String] OAuth2 authentication URL

# File lib/napster/client.rb, line 140
def authentication_url
  validate_authentication_url
  query_params = {
    client_id: @api_key,
    redirect_uri: @redirect_uri,
    response_type: 'code'
  }
  query_params[:state] = @state if @state
  query_params_string = URI.encode_www_form(query_params)
  Napster::Request::HOST_URL + '/oauth/authorize?' + query_params_string
end
connect() click to toggle source

Smarter method for authentication via password_grant or oauth2 @return [Client]

# File lib/napster/client.rb, line 121
def connect
  return authenticate(:password_grant) if authenticate_via_password_grant?
  return authenticate(:oauth2) if authenticate_via_oauth2?
  raise ArgumentError
end
delete(path, options = {}) click to toggle source

Make a delete request to Napster API @param path [String] API path @param options [Hash] Faraday apapter options accepting

headers, params

@return [Hash] parsed response from Napster API

# File lib/napster/client.rb, line 105
def delete(path, options = {})
  validate_request(path, options)
  raw_response = @request.faraday.delete do |req|
    req.url path, options[:params]
    req.headers['apikey'] = @api_key
    if options[:headers]
      options[:headers].each do |key, value|
        req.headers[key] = value
      end
    end
  end
  handle_response(raw_response)
end
get(path, options = {}) click to toggle source

Make a get request to Napster API @param path [String] API path @param options [Hash] Faraday adapter options accepting

headers

@return [Hash] parsed response from Napster API

# File lib/napster/client.rb, line 65
def get(path, options = {})
  validate_request(path, options)
  raw_response = @request.faraday.get do |req|
    req.url path, options[:params]
    req.headers['apikey'] = @api_key
    if options[:headers]
      options[:headers].each do |key, value|
        req.headers[key] = value
      end
    end
  end
  handle_response(raw_response)
end
me() click to toggle source

Include Me module for calling authenticated methods

# File lib/napster/client.rb, line 153
def me
  Napster::Me.new(self)
end
post(path, body = {}, options = {}) click to toggle source

Make a post request to Napster API @param path [String] API path @param body [Hash] Body for the post request @param options [Hash] Faraday adapter options @return [Hash] parsed response from Napster API

# File lib/napster/client.rb, line 45
def post(path, body = {}, options = {})
  validate_request(path, options)
  raw_response = @request.faraday.post do |req|
    req.url path, options[:params]
    req.body = body
    req.headers['apikey'] = @api_key
    if options[:headers]
      options[:headers].each do |key, value|
        req.headers[key] = value
      end
    end
  end
  handle_response(raw_response)
end
put(path, body = {}, options = {}) click to toggle source

Make a put request to Napster API @param path [String] API path @param body [Hash] Body for the put request @param options [Hash] Faraday apapter options accepting

headers, params

@return [Hash] parsed response from Napster API

# File lib/napster/client.rb, line 85
def put(path, body = {}, options = {})
  validate_request(path, options)
  raw_response = @request.faraday.put do |req|
    req.url path, options[:params]
    req.body = body
    req.headers['apikey'] = @api_key
    if options[:headers]
      options[:headers].each do |key, value|
        req.headers[key] = value
      end
    end
  end
  handle_response(raw_response)
end

Private Instance Methods

auth_oauth2() click to toggle source
# File lib/napster/client.rb, line 229
def auth_oauth2
  validate_auth_oauth2
  response_body = post('/oauth/access_token', auth_oauth2_post_body, {})
  @access_token = response_body['access_token']
  @refresh_token = response_body['refresh_token']
  @expires_in = response_body['expires_in']
  self
end
auth_oauth2_post_body() click to toggle source
# File lib/napster/client.rb, line 238
def auth_oauth2_post_body
  {
    client_id: @api_key,
    client_secret: @api_secret,
    response_type: 'code',
    grant_type: 'authorization_code',
    code: @auth_code,
    redirect_uri: @redirect_uri
  }
end
auth_password_grant() click to toggle source
# File lib/napster/client.rb, line 197
def auth_password_grant
  validate_auth_password_grant
  body = post('/oauth/token',
              auth_password_grant_post_body,
              auth_password_grant_post_options)
  @access_token = body['access_token']
  @refresh_token = body['refresh_token']
  @expires_in = body['expires_in']

  self
end
auth_password_grant_post_body() click to toggle source
# File lib/napster/client.rb, line 209
def auth_password_grant_post_body
  {
    response_type: 'code',
    grant_type: 'password',
    username: @username,
    password: @password
  }
end
auth_password_grant_post_options() click to toggle source
# File lib/napster/client.rb, line 218
def auth_password_grant_post_options
  basic_auth = Faraday::Request::BasicAuthentication.header(@api_key,
                                                            @api_secret)
  { headers: { Authorization: basic_auth } }
end
authenticate_client() click to toggle source

Helper method for .new, choose authentication method, and authenticate @return [Client] Authenticated client

# File lib/napster/client.rb, line 161
def authenticate_client
  return authenticate(:password_grant) if authenticate_via_password_grant?
  self
end
authenticate_via_oauth2?() click to toggle source
# File lib/napster/client.rb, line 173
def authenticate_via_oauth2?
  @api_key && @api_secret && @redirect_uri && @auth_code
end
authenticate_via_password_grant?() click to toggle source

Helper method for authenticate_client, check if password_grant auth is

possible with current attributes.

@return [Boolean]

# File lib/napster/client.rb, line 169
def authenticate_via_password_grant?
  @api_key && @api_secret && @username && @password
end
handle_response(response) click to toggle source
# File lib/napster/client.rb, line 271
def handle_response(response)
  raise ResponseError.new(response) if response && !response.success?
  Oj.load(response.body)
end
model_class_name(model) click to toggle source
# File lib/napster/client.rb, line 267
def model_class_name(model)
  "Napster::Models::#{model.capitalize}"
end
set_models() click to toggle source
# File lib/napster/client.rb, line 258
def set_models
  MODELS_LIST.each do |model|
    define_singleton_method("#{model}s") do
      Object.const_get(model_class_name(model)).new(client: self)
    end
  end
  self
end
validate_auth_oauth2() click to toggle source
# File lib/napster/client.rb, line 249
def validate_auth_oauth2
  raise 'The client is missing redirect_uri' unless @redirect_uri
  raise 'The client is missing auth_code' unless @auth_code
end
validate_auth_password_grant() click to toggle source
# File lib/napster/client.rb, line 224
def validate_auth_password_grant
  raise 'The client is missing username' unless @username
  raise 'The client is missing password' unless @password
end
validate_authenticate(auth_method) click to toggle source
# File lib/napster/client.rb, line 185
def validate_authenticate(auth_method)
  unless auth_method.is_a?(Symbol)
    err = 'Authentication method must be passed as a symbol'
    raise ArgumentError, err
  end

  unless AUTH_METHODS.include?(auth_method)
    err = "Wrong authentication method. Valid methods are #{AUTH_METHODS}"
    raise ArgumentError, err
  end
end
validate_authentication_url() click to toggle source
# File lib/napster/client.rb, line 254
def validate_authentication_url
  raise 'The client is missing redirect_uri' unless @redirect_uri
end
validate_request(path, options) click to toggle source
# File lib/napster/client.rb, line 177
def validate_request(path, options)
  raise ArgumentError, 'path is missing' unless path
  raise ArgumentError, 'options should be a hash' unless options.is_a?(Hash)
  if options[:headers] && !options[:headers].is_a?(Hash)
    raise ArgumentError, 'options[:headers] should be a hash'
  end
end