Sha256: cd0570a74ec774545af7a17e9309393c61f0b756aa8e4f24cf8ffee93164498c

Contents?: true

Size: 945 Bytes

Versions: 9

Compression:

Stored size: 945 Bytes

Contents

module Services
  module Authorization
    module Tokens
      class Refresh < Tokens::Base
        def initialize(tokens:, request:)
          @tokens = tokens.with_indifferent_access
          @request = request
        end

        def call
          @tokens = refresh_tokens if Time.current > @tokens[:expires_at]
          @tokens
        end

        private

        def refresh_tokens
          tokens_api = ::Hubspot::OAuth::TokensApi.new
          tokens = tokens_api.post_oauth_v1_token(
            grant_type: :refresh_token,
            refresh_token: @tokens[:refresh_token],
            redirect_uri: redirect_uri,
            client_id: ENV['HUBSPOT_CLIENT_ID'],
            client_secret: ENV['HUBSPOT_CLIENT_SECRET'],
            return_type: 'Object'
          )
          tokens[:expires_at] = expires_at(tokens[:expires_in])
          Token.instance.update!(tokens)
          tokens
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hubspot-api-client-3.2.0 sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-3.1.1 sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-3.1.0.pre.beta sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-3.0.0.pre.beta sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-2.3.2 sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-2.3.1 sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-2.2.0 sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-2.1.0 sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb
hubspot-api-client-2.0.0 sample-apps/webhooks-contacts-app/app/lib/services/authorization/tokens/refresh.rb