Sha256: 32d6c1333c3e0ecf2aba60fd60bcdf741bb79e1b7b5842c1bc3975e1e353eb18

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module NCMB
  class User < NCMB::Object
    include NCMB
    
    def initialize(params = {})
      super('users', params)
    end
    
    def signUp
      begin
        result = @@client.post path, @fields
      rescue => e
        @@last_error = e
        return false
      end
      @fields.merge!(result)
      @@current_user = self
      self
    end
    
    def base_path
      path = "/#{@@client.api_version}/#{@name}"
    end
    
    def put
      params = @fields.clone
      session_key = params[:sessionToken]
      [:objectId, :createDate, :updateDate, :sessionToken, :password].each do |name|
        params.delete name
      end
      result = @@client.put path, params, session_key
      @fields[:updateDate] = result[:updateDate]
      self
    end
    alias :update :put
    
    def delete
      response = @@client.delete path, {}, @fields[:sessionToken]
      if response == true
        @@current_user = nil
        return true
      else
        @@last_error = response
        return false
      end
    end
    
    def self.login(userid_or_email, password, authType = :id)
      params = {password: password}
      case authType.to_sym
      when :id
        params[:userName] = userid_or_email
      when :email
        params[:mailAddres] = userid_or_email
      else
        raise NCMB::APIError.new("No support #{authType} authentication. We support only id or email.")
      end
      begin
        path = "/#{@@client.api_version}/login"
        result = @@client.get path, params
      rescue => e
        @@last_error = e
        return false
      end
      @@current_user = NCMB::User.new(result)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ncmb-ruby-client-0.2.0 lib/ncmb/user.rb