Class: Rhapsody::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/rhapsody/authentication.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Authentication

Returns a new instance of Authentication.



7
8
9
10
11
# File 'lib/rhapsody/authentication.rb', line 7

def initialize(options)
  options.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



2
3
4
# File 'lib/rhapsody/authentication.rb', line 2

def access_token
  @access_token
end

#clientObject

Returns the value of attribute client.



2
3
4
# File 'lib/rhapsody/authentication.rb', line 2

def client
  @client
end

#expires_inObject

Returns the value of attribute expires_in.



2
3
4
# File 'lib/rhapsody/authentication.rb', line 2

def expires_in
  @expires_in
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



2
3
4
# File 'lib/rhapsody/authentication.rb', line 2

def refresh_token
  @refresh_token
end

Instance Method Details

#connectObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rhapsody/authentication.rb', line 36

def connect
  request = Rhapsody::Request.new({})

  post_hash = {
    client_id: @client.api_key,
    client_secret: @client.api_secret,
    response_type: 'code',
    grant_type: 'authorization_code',
    code: @client.auth_code,
    redirect_uri: @client.redirect_uri
  }

  raw_response = request.faraday.post('oauth/access_token', post_hash)
  body = Oj.load(raw_response.body)

  @access_token = body['access_token']
  @refresh_token = body['refresh_token']
  @expires_in = body['expires_in'].to_i

  self
end

#password_grantObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rhapsody/authentication.rb', line 13

def password_grant
  request_hash = {
    api_key: @client.api_key,
    api_secret: @client.api_secret
  }
  request = Rhapsody::Request.new(request_hash)

  post_hash = {
    response_type: 'code',
    grant_type: 'password',
    username: @client.username,
    password: @client.password
  }
  raw_response = request.faraday.post('/oauth/token', post_hash)
  body = Oj.load(raw_response.body)

  @access_token = body['access_token']
  @refresh_token = body['refresh_token']
  @expires_in = body['expires_in'].to_i

  self
end

#renewObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rhapsody/authentication.rb', line 58

def renew
  request = Rhapsody::Request.new({})

  post_hash = {
    client_id: @client.api_key,
    client_secret: @client.api_secret,
    response_type: 'code',
    grant_type: 'refresh_token',
    refresh_token: @refresh_token
  }

  raw_response = request.faraday.post('oauth/access_token', post_hash)
  body = Oj.load(raw_response.body)

  @access_token = body['access_token']
  @refresh_token = body['refresh_token']
  @expires_in = body['expires_in'].to_i

  self
end