Sha256: 2133bc73f444f621f378348b5b9407ae0ec2896bfa81138ef9247ca7ecadc6ef

Contents?: true

Size: 1.57 KB

Versions: 24

Compression:

Stored size: 1.57 KB

Contents

module DeployGate
  class Session
    class LoginError < DeployGate::NotIssueError
    end

    attr_reader :name, :token

    module ENVKey
      DG_USER_NAME = 'DG_USER_NAME'
      DG_TOKEN = 'DG_TOKEN'
    end

    @@login = nil

    # @return [DeployGate::Session]
    def initialize
      load_setting
    end

    # @return [Boolean]
    def login?
      @@login = @@login.nil? ? API::V1::Session.check(@name, @token) : @@login
    end

    def show_login_user
      API::V1::Session.show(@token)
    end

    # @param [String] email
    # @param [String] password
    # @return [void]
    def self.login(email, password)
      data = API::V1::Session.login(email, password)
      raise LoginError, data[:message] if data[:error]

      name = data[:name]
      token = data[:token]
      save(name, token)
      @@login = true
    end

    # @param [String] name
    # @param [String] token
    # @return [void]
    def self.save(name, token)
      settings = {
          :name => name,
          :token => token
      }
      Config::Credential.write(settings)
    end

    # @return [void]
    def self.delete
      save('', '') # delete config values
      @@login = false
    end

    private

    # @return [void]
    def load_setting
      return if load_env
      return unless Config::Credential.exist?
      settings = Config::Credential.read

      @name = settings['name']
      @token = settings['token']
    end

    # @return [Boolean]
    def load_env
      @name = ENV[ENVKey::DG_USER_NAME]
      @token = ENV[ENVKey::DG_TOKEN]

      @name.present? && @token.present?
    end

  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
deploygate-0.6.7 lib/deploygate/session.rb
deploygate-0.6.6 lib/deploygate/session.rb
deploygate-0.6.5 lib/deploygate/session.rb
deploygate-0.6.4 lib/deploygate/session.rb
deploygate-0.6.3 lib/deploygate/session.rb
deploygate-0.6.2 lib/deploygate/session.rb
deploygate-0.6.1 lib/deploygate/session.rb
deploygate-0.6.0 lib/deploygate/session.rb
deploygate-0.5.6 lib/deploygate/session.rb
deploygate-0.5.5 lib/deploygate/session.rb
deploygate-0.5.4 lib/deploygate/session.rb
deploygate-0.5.3 lib/deploygate/session.rb
deploygate-0.5.2 lib/deploygate/session.rb
deploygate-0.5.1 lib/deploygate/session.rb
deploygate-0.5.0 lib/deploygate/session.rb
deploygate-0.4.1 lib/deploygate/session.rb
deploygate-0.4.0 lib/deploygate/session.rb
deploygate-0.3.3 lib/deploygate/session.rb
deploygate-0.3.2 lib/deploygate/session.rb
deploygate-0.3.1 lib/deploygate/session.rb