class Rhapsody::Authentication

Attributes

access_token[RW]
client[RW]
expires_in[RW]
refresh_token[RW]

Public Class Methods

new(options) click to toggle source
# File lib/rhapsody/authentication.rb, line 7
def initialize(options)
  options.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

Public Instance Methods

connect() click to toggle source
# 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_grant() click to toggle source
# 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
renew() click to toggle source
# 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