Sha256: a9a120c70b232e9be7ab660eb8efdfcd8809f96f393bc8be147c2973a5ee093d

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module NCMB
  class User < NCMB::Object
    include NCMB
    
    def initialize(params = {}, alc = nil)
      super('users', params, alc)
    end
    
    def signUp
      path = "/#{@@client.api_version}/#{@name}"
      begin
        result = @@client.post path, @fields
      rescue => e
        @@last_error = e
        return false
      end
      @fields.merge!(result)
      @@current_user = self
      self
    end
    
    def put
      path = "/#{@@client.api_version}/#{@name}/#{@fields[:objectId]}"
      params = @fields
      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
      path = "/#{@@client.api_version}/#{@name}/#{@fields[:objectId]}"
      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.0.9 lib/ncmb/user.rb