Sha256: dbcac12ddaa1e7aeee3116a0d0959507a7441204032478a4f2cad6efe3c8ade8

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

class Rhapsody::Authentication
  attr_accessor :client,
                :access_token,
                :refresh_token,
                :expires_in

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

  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

  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

  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
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rhapsody-0.1.1 lib/rhapsody/authentication.rb
rhapsody-0.1.0 lib/rhapsody/authentication.rb