Sha256: 70c3e35e46813a413eca1a430f2c7656987700ae93b7b2d59c500d7911ebd208

Contents?: true

Size: 1.33 KB

Versions: 10

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module BeyondApi
  class Token
    class InvalidSessionError < StandardError; end

    attr_reader :session

    def initialize(session)
      @session = session
      raise InvalidSessionError.new("Invalid session") unless session.is_a? BeyondApi::Session
      raise InvalidSessionError.new("Session api_url cannot be nil") if session.api_url.nil?
    end

    def create(code)
      response, status = BeyondApi::Request.token(@session.api_url + "/oauth/token",
                                                  grant_type: "authorization_code",
                                                  code: code)

      if status.between?(200, 299)
        @session.access_token = response["access_token"]
        @session.refresh_token = response["refresh_token"]
      else
        BeyondApi::Error.new(response)
      end
    end

    def refresh
      response, status = BeyondApi::Request.token(@session.api_url + "/oauth/token",
                                                  grant_type: "refresh_token",
                                                  refresh_token: @session.refresh_token)

      if status.between?(200, 299)
        @session.access_token = response["access_token"]
        @session.refresh_token = response["refresh_token"]
      else
        BeyondApi::Error.new(response)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
beyond_api-0.7.1.pre lib/beyond_api/resources/token.rb
beyond_api-0.7.0.pre lib/beyond_api/resources/token.rb
beyond_api-0.6.0.pre lib/beyond_api/resources/token.rb
beyond_api-0.5.1.pre lib/beyond_api/resources/token.rb
beyond_api-0.5.0.pre lib/beyond_api/resources/token.rb
beyond_api-0.4.0.pre lib/beyond_api/resources/token.rb
beyond_api-0.3.0.pre lib/beyond_api/resources/token.rb
beyond_api-0.2.1.pre lib/beyond_api/resources/token.rb
beyond_api-0.2.0.pre lib/beyond_api/resources/token.rb
beyond_api-0.1.0.pre lib/beyond_api/resources/token.rb