Class: Napster::Client
- Inherits:
-
Object
- Object
- Napster::Client
- 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
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
Returns the value of attribute api_secret.
-
#auth_code ⇒ Object
Returns the value of attribute auth_code.
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
-
#password ⇒ Object
Returns the value of attribute password.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
#request ⇒ Object
Returns the value of attribute request.
-
#state ⇒ Object
Returns the value of attribute state.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
-
#authenticate(auth_method) ⇒ Hash
Main method for authenticating against Napster API.
-
#authentication_url ⇒ String
Get URL for OAuth2 authentication flow.
-
#connect ⇒ Client
Smarter method for authentication via password_grant or oauth2.
-
#delete(path, options = {}) ⇒ Hash
Make a delete request to Napster API.
-
#get(path, options = {}) ⇒ Hash
Make a get request to Napster API.
-
#initialize(options) ⇒ Client
constructor
Instantiate a client object.
-
#me ⇒ Object
Include Me module for calling authenticated methods.
-
#post(path, body = {}, options = {}) ⇒ Hash
Make a post request to Napster API.
-
#put(path, body = {}, options = {}) ⇒ Hash
Make a put request to Napster API.
Constructor Details
#initialize(options) ⇒ Client
request attribute is always overwritten by Napster::Request object.
Instantiate a client object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/napster/client.rb', line 26 def initialize() .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_token ⇒ Object
Returns the value of attribute access_token.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def access_token @access_token end |
#api_key ⇒ Object
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def api_key @api_key end |
#api_secret ⇒ Object
Returns the value of attribute api_secret.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def api_secret @api_secret end |
#auth_code ⇒ Object
Returns the value of attribute auth_code.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def auth_code @auth_code end |
#expires_in ⇒ Object
Returns the value of attribute expires_in.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def expires_in @expires_in end |
#password ⇒ Object
Returns the value of attribute password.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def password @password end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def redirect_uri @redirect_uri end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def refresh_token @refresh_token end |
#request ⇒ Object
Returns the value of attribute request.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def request @request end |
#state ⇒ Object
Returns the value of attribute state.
10 11 12 |
# File 'lib/napster/client.rb', line 10 def state @state end |
#username ⇒ Object
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
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_url ⇒ String
Get URL for OAuth2 authentication flow
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 |
#connect ⇒ Client
Smarter method for authentication via password_grant or oauth2
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
105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/napster/client.rb', line 105 def delete(path, = {}) validate_request(path, ) raw_response = @request.faraday.delete do |req| req.url path, [:params] req.headers['apikey'] = @api_key if [:headers] [: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
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/napster/client.rb', line 65 def get(path, = {}) validate_request(path, ) raw_response = @request.faraday.get do |req| req.url path, [:params] req.headers['apikey'] = @api_key if [:headers] [:headers].each do |key, value| req.headers[key] = value end end end handle_response(raw_response) end |
#me ⇒ Object
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
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 = {}, = {}) validate_request(path, ) raw_response = @request.faraday.post do |req| req.url path, [:params] req.body = body req.headers['apikey'] = @api_key if [:headers] [: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
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 = {}, = {}) validate_request(path, ) raw_response = @request.faraday.put do |req| req.url path, [:params] req.body = body req.headers['apikey'] = @api_key if [:headers] [:headers].each do |key, value| req.headers[key] = value end end end handle_response(raw_response) end |