Sha256: 34e09092861ff223ba2e9547021fa0a1e5787d6d145722ec0ffd4606115ddd9b

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

require "fmrest/v1/token_store/base"

module FmRest
  module V1
    module TokenStore
      # Heavily inspired by Moneta's ActiveRecord store:
      #
      #   https://github.com/minad/moneta/blob/master/lib/moneta/adapters/activerecord.rb
      #
      class ActiveRecord < Base
        DEFAULT_TABLE_NAME = "fmrest_session_tokens".freeze

        @connection_lock = ::Mutex.new
        class << self
          attr_reader :connection_lock
        end

        attr_reader :connection_pool, :model

        delegate :with_connection, to: :connection_pool

        def initialize(host, database, options = {})
          super

          @connection_pool = ::ActiveRecord::Base.connection_pool

          create_table

          @model = Class.new(::ActiveRecord::Base)
          @model.table_name = table_name
        end

        def clear
          model.where(scope: scope).delete_all
        end

        def fetch
          model.where(scope: scope).pluck(:token).first
        end

        def store(token)
          record = model.find_or_initialize_by(scope: scope)
          record.token = token
          record.save!
          token
        end

        private

        def create_table
          with_connection do |conn|
            return if conn.table_exists?(table_name)

            # Prevent multiple connections from attempting to create the table simultaneously.
            self.class.connection_lock.synchronize do
              conn.create_table(table_name, id: false) do |t|
                t.string :scope, null: false
                t.string :token, null: false
                t.datetime :updated_at
              end
              conn.add_index(table_name, :scope, unique: true)
              conn.add_index(table_name, [:scope, :token])
            end
          end
        end

        def table_name
          options[:table_name] || DEFAULT_TABLE_NAME
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fmrest-0.1.0 lib/fmrest/v1/token_store/active_record.rb