Sha256: dbbbdb79e6f06320ca03d9d07f439aad85875f82237e6a5a5abe94b008c65307

Contents?: true

Size: 1.51 KB

Versions: 9

Compression:

Stored size: 1.51 KB

Contents

module DomoscioRails
  module AuthorizationToken
    # Token Manager
    class Manager
      class << self
        def storage
          @storage ||= DomoscioRails.configuration.file_storage ? FileStorage.new : ManualSetting.new
        end

        def token
          storage.get
        end
      end
    end

    # Manages tokens with configuration
    class ManualSetting
      def get
        {
          access_token: DomoscioRails.configuration.access_token,
          refresh_token: DomoscioRails.configuration.refresh_token
        }
      end

      def store(token)
        DomoscioRails.configuration.access_token = token[:access_token]
        DomoscioRails.configuration.refresh_token = token[:refresh_token]
      end
    end

    # Manages tokens with local File
    class FileStorage
      require 'yaml'

      def initialize
        raise 'Path to temporary folder is not defined' unless DomoscioRails.configuration.temp_dir
      end

      def get
        f = File.open(file_path, File::RDONLY)
        f.flock(File::LOCK_SH)
        txt = f.read
        f.close
        YAML.safe_load(txt) || nil
      rescue Errno::ENOENT
        nil
      end

      def store(token)
        File.open(file_path, File::RDWR|File::CREAT, 0o644) do |f|
          f.flock(File::LOCK_EX)
          f.truncate(0)
          f.rewind
          f.puts(YAML.dump(token))
        end
      end

      def file_path
        File.join(DomoscioRails.configuration.temp_dir, 'DomoscioRails.AuthorizationToken.FileStore.tmp')
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
domoscio_rails-0.4.8 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.7 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.6.2 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.6 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.5 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.4 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.2 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.1 lib/domoscio_rails/authorization_token.rb
domoscio_rails-0.4.0 lib/domoscio_rails/authorization_token.rb