Sha256: be6d9215fd887329af8e15ac60b2b0db9da4b2cb3b739c1ff068b661b32092e8

Contents?: true

Size: 1.6 KB

Versions: 8

Compression:

Stored size: 1.6 KB

Contents

# typed: strict
# frozen_string_literal: true

require "fileutils"

module ShopifyAPI
  module Auth
    class FileSessionStorage
      extend T::Sig
      extend T::Helpers
      include ShopifyAPI::Auth::SessionStorage

      sig { returns(String) }
      attr_accessor :path

      sig { params(path: String).void }
      def initialize(path: "/tmp/shopify_api_sessions")
        @path = path
        FileUtils.mkdir_p(path) unless Dir.exist?(path)
      end

      sig do
        override.params(session: Session)
          .returns(T::Boolean)
      end
      def store_session(session)
        File.write(session_file_path(session.id), session.serialize) > 0
      end

      sig do
        override.params(id: String)
          .returns(T.nilable(Session))
      end
      def load_session(id)
        session_path = session_file_path(id)
        if File.exist?(session_path)
          ShopifyAPI::Auth::Session.deserialize(File.read(session_path))
        end
      end

      sig do
        override.params(id: String)
          .returns(T::Boolean)
      end
      def delete_session(id)
        session_path = session_file_path(id)
        File.delete(session_path) if File.exist?(session_path)
        true
      end

      alias_method :eql?, :==
      sig { params(other: T.nilable(FileSessionStorage)).returns(T::Boolean) }
      def ==(other)
        if other
          @path == other.path
        else
          false
        end
      end

      private

      sig do
        params(id: String)
          .returns(String)
      end
      def session_file_path(id)
        "#{@path}/#{id}"
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
shopify_api-12.5.0 lib/shopify_api/auth/file_session_storage.rb
shopify_api-12.4.0 lib/shopify_api/auth/file_session_storage.rb
shopify_api-12.3.0 lib/shopify_api/auth/file_session_storage.rb
shopify_api-12.2.1 lib/shopify_api/auth/file_session_storage.rb
shopify_api-12.2.0 lib/shopify_api/auth/file_session_storage.rb
shopify_api-12.1.0 lib/shopify_api/auth/file_session_storage.rb
shopify_api-12.0.0 lib/shopify_api/auth/file_session_storage.rb
shopify_api-11.1.0 lib/shopify_api/auth/file_session_storage.rb