Class: RedHaze::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/red_haze/client.rb

Constant Summary collapse

TOKEN_ENDPOINT =
"https://api.soundcloud.com/oauth2/token"
CONNECT_ENDPOINT =
"https://soundcloud.com/connect"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
# File 'lib/red_haze/client.rb', line 12

def initialize(options={})
  options_check(options)
  update_from_response(options)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



6
7
8
# File 'lib/red_haze/client.rb', line 6

def access_token
  @access_token
end

#client_idObject

Returns the value of attribute client_id.



6
7
8
# File 'lib/red_haze/client.rb', line 6

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



6
7
8
# File 'lib/red_haze/client.rb', line 6

def client_secret
  @client_secret
end

#expires_inObject

Returns the value of attribute expires_in.



6
7
8
# File 'lib/red_haze/client.rb', line 6

def expires_in
  @expires_in
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



6
7
8
# File 'lib/red_haze/client.rb', line 6

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



6
7
8
# File 'lib/red_haze/client.rb', line 6

def refresh_token
  @refresh_token
end

Instance Method Details

#authorize_urlObject



17
18
19
20
21
# File 'lib/red_haze/client.rb', line 17

def authorize_url
  raise "Missing redirect_uri" unless redirect_uri
  params = "client_id=#{client_id}&redirect_uri=#{redirect_uri}&response_type=code"
  URI.encode "#{CONNECT_ENDPOINT}?#{params}"
end

#get_token_from_code(code) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/red_haze/client.rb', line 23

def get_token_from_code(code)
  body =  { client_id:      client_id,
            client_secret:  client_secret,
            redirect_uri:   redirect_uri,
            code:           code,
            grant_type:     'authorization_code' }

  response = self.class.post TOKEN_ENDPOINT, body: body,
              headers: {'Content-Type' => 'application/x-www-form-urlencoded'}

  raise response.inspect unless response.code == 200
  update_from_response(response)
  self
end