Sha256: 94439c7a667f0a5dd024ef700b0d7ef286e5b8ad018ca3b5afe1275e0403107a

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

module FaaStRuby
  class User < BaseObject
    def self.create(email:, password:)
      api = API.new
      user = User.new(email: email)
      response = api.signup(email: email, password: password)
      user.status_code = response.code
      if response.errors.any?
        user.errors += response.errors
        return user
      end
      case response.code
      when 422
        user.errors += ['(422) Unprocessable Entity', response.body]
      when 200, 201
        true
      else
        user.errors << "(#{response.code}) Error"
      end
      return user
    end

    attr_accessor :email, :password, :status_code, :errors, :api_key, :api_secret, :confirmation_token

    def logout(all: false)
      response = call_api { @api.logout(api_key: @api_key, api_secret: @api_secret, all: all) }

      self
    end

    def assign_credentials(response)
      return if response.body['credentials'].nil?

      @api_key = response.body['credentials']['api_key']
      @api_secret = response.body['credentials']['api_secret']
    end

    def login
      response = call_api { @api.login(email: @email, password: @password) }
      assign_credentials(response) unless response.errors.any?

      self
    end

    def has_credentials?
      @api_key && @api_secret
    end

    def confirm_account!
      response = call_api { @api.confirm_account(@confirmation_token) }
      assign_credentials(response) unless response.errors.any?

      self
    end

    def send_confirmation_code
      response = call_api { @api.send_confirmation_code(@email) }

      self
    end

    def save_credentials

      credentials_file = NewCredentials::CredentialsFile.new
      credentials_file.save(email: @email, api_key: @api_key, api_secret: @api_secret)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
faastruby-0.5.30 lib/faastruby/user.rb
faastruby-0.5.29 lib/faastruby/user.rb
faastruby-0.5.28 lib/faastruby/user.rb
faastruby-0.5.27 lib/faastruby/user.rb
faastruby-0.5.26 lib/faastruby/user.rb