Sha256: 04db8970b5b1ca7884b48cf949137b574c5904a172c176125c2c37cae805e69c

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module BeyondCanvas
  module Models
    module Utils # :nodoc:
      extend ActiveSupport::Concern

      included do
        ##############################################################################
        # Instance methods
        ##############################################################################

        #
        # Generates a new access_token and refresh_token
        #
        def refresh_token
          beyond_session = BeyondApi::Session.new(api_url: beyond_api_url, refresh_token: beyond_refresh_token)
          beyond_session.token.refresh

          update(beyond_access_token: beyond_session.access_token,
                 beyond_refresh_token: beyond_session.refresh_token)
        end

        #
        # Generates a new access_token and refresh_token if they have expired
        #
        def refresh_token_if_needed
          token_timestamp = JWT.decode(beyond_access_token, nil, false).first['exp']
          current_timestamp = DateTime.now.to_i
          return unless token_timestamp - current_timestamp <= 3600

          refresh_token
        end

        #
        # Returns a BeyondApi::Session object with api_url, access_token and refresh_token attributes
        #
        def to_session
          BeyondApi::Session.new(api_url: beyond_api_url,
                                 access_token: beyond_access_token,
                                 refresh_token: beyond_refresh_token)
        end

        ##############################################################################
        # Class methods
        ##############################################################################

        def self.find_session(id)
          shop = find(id)
          shop.to_session
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
beyond_canvas-0.16.2.pre lib/beyond_canvas/models/utils.rb
beyond_canvas-0.16.1.pre lib/beyond_canvas/models/utils.rb
beyond_canvas-0.16.0.pre lib/beyond_canvas/models/utils.rb