Sha256: 6e1b2c3b0d2472176c53545c073e524c093a9eda6943483bc211c91a29957ff7

Contents?: true

Size: 1.18 KB

Versions: 3

Compression:

Stored size: 1.18 KB

Contents

module DeployGate
  class Session
    class LoginError < StandardError
    end

    attr_reader :name, :token

    @@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

    # @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.write(settings)
    end

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

    private

    # @return [void]
    def load_setting
      return unless Config.exist?
      settings = Config.read
      @name = settings['name']
      @token = settings['token']
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
deploygate-0.0.3 lib/deploygate/session.rb
deploygate-0.0.2 lib/deploygate/session.rb
deploygate-0.0.1 lib/deploygate/session.rb