Class: Napster::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/napster/client.rb

Overview

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

Constant Summary collapse

MODELS_LIST =
%w(artist album track genre member playlist tag
station radio favorite).freeze
AUTH_METHODS =
[:password_grant, :oauth2].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Note:

request attribute is always overwritten by Napster::Request object.

Instantiate a client object

Parameters:

  • options (Hash)

    Required options are :api_key and :api_secret



26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/napster/client.rb', line 10

def access_token
  @access_token
end

#api_keyObject

Returns the value of attribute api_key.



10
11
12
# File 'lib/napster/client.rb', line 10

def api_key
  @api_key
end

#api_secretObject

Returns the value of attribute api_secret.



10
11
12
# File 'lib/napster/client.rb', line 10

def api_secret
  @api_secret
end

#auth_codeObject

Returns the value of attribute auth_code.



10
11
12
# File 'lib/napster/client.rb', line 10

def auth_code
  @auth_code
end

#expires_inObject

Returns the value of attribute expires_in.



10
11
12
# File 'lib/napster/client.rb', line 10

def expires_in
  @expires_in
end

#passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/napster/client.rb', line 10

def password
  @password
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



10
11
12
# File 'lib/napster/client.rb', line 10

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/napster/client.rb', line 10

def refresh_token
  @refresh_token
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'lib/napster/client.rb', line 10

def request
  @request
end

#stateObject

Returns the value of attribute state.



10
11
12
# File 'lib/napster/client.rb', line 10

def state
  @state
end

#usernameObject

Returns the value of attribute username.



10
11
12
# File 'lib/napster/client.rb', line 10

def username
  @username
end

Instance Method Details

#authenticate(auth_method) ⇒ Hash

Main method for authenticating against Napster API

Parameters:

  • auth_method (Symbol)

    authentication methods that are :password_grant or :oauth2

Returns:

  • (Hash)

    response from Napster API



131
132
133
134
135
136
# 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_urlString

Get URL for OAuth2 authentication flow

Returns:

  • (String)

    OAuth2 authentication URL



140
141
142
143
144
145
146
147
148
149
150
# 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

#connectClient

Smarter method for authentication via password_grant or oauth2

Returns:

Raises:

  • (ArgumentError)


121
122
123
124
125
# 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 = {}) ⇒ Hash

Make a delete request to Napster API

Parameters:

  • path (String)

    API path

  • options (Hash) (defaults to: {})

    Faraday apapter options accepting headers, params

Returns:

  • (Hash)

    parsed response from Napster API



105
106
107
108
109
110
111
112
113
114
115
116
117
# 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 = {}) ⇒ Hash

Make a get request to Napster API

Parameters:

  • path (String)

    API path

  • options (Hash) (defaults to: {})

    Faraday adapter options accepting headers

Returns:

  • (Hash)

    parsed response from Napster API



65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

#meObject

Include Me module for calling authenticated methods



153
154
155
# File 'lib/napster/client.rb', line 153

def me
  Napster::Me.new(self)
end

#post(path, body = {}, options = {}) ⇒ Hash

Make a post request to Napster API

Parameters:

  • path (String)

    API path

  • body (Hash) (defaults to: {})

    Body for the post request

  • options (Hash) (defaults to: {})

    Faraday adapter options

Returns:

  • (Hash)

    parsed response from Napster API



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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 = {}) ⇒ Hash

Make a put request to Napster API

Parameters:

  • path (String)

    API path

  • body (Hash) (defaults to: {})

    Body for the put request

  • options (Hash) (defaults to: {})

    Faraday apapter options accepting headers, params

Returns:

  • (Hash)

    parsed response from Napster API



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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