class Foscam::Model::User

Constants

MAX_NUMBER

Max number of users supported by foscam

Attributes

id[R]

@!attribute [r] id

@return [Fixnum] The id of the user
password[R]

@!attribute [rw] password

@return [String] The password of the user
privilege[R]

@!attribute [rw] privilege

@return [Symbol] The privilege of the user
username[R]

@!attribute [rw] username

@return [String] The name of the user

Public Class Methods

all() click to toggle source

Get all the users @return [Array] of Users

# File lib/foscam/model/user.rb, line 52
def self.all
        cam_params = client.get_params
        users = []
        unless cam_params.empty?
                (1..8).each do |i|
                        unless cam_params["user#{i}_name".to_sym].empty?
                                user = User.new(:username => cam_params["user#{i}_name".to_sym], :password => cam_params["user#{i}_pwd".to_sym], :privilege => cam_params["user#{i}_pri".to_sym])
                                user.instance_variable_set(:@id, i)
                                users << user
                        end
                end
        end
        users
end
create(params ={}) click to toggle source

Create a user with the specified parameters @param params [Hash] User attributes @option params [String] :username @option params [String] :password @option params [Symbol] :privilege @return [User, nil] Returns the user if successfully saved

# File lib/foscam/model/user.rb, line 74
def self.create(params ={})
        user = User.new(params)
        user.save ? user : nil
end
delete(id) click to toggle source

Delete a user by the id @param id [Fixnum] The id of the user @return [FalseClass, TrueClass] Whether or not the user was successfully deleted

# File lib/foscam/model/user.rb, line 100
def self.delete(id)
        params = {"user#{id}".to_sym => "", "pwd#{id}".to_sym => "", "pri#{id}".to_sym => 0}
        id > 0 && id <= MAX_NUMBER ? client.set_users(params) : false
end
find(id) click to toggle source

Find a specific user by name @param id [Fixnum] The id of the user @return [User]

# File lib/foscam/model/user.rb, line 84
def self.find(id)
        user = nil
        if id > 0 && id <= MAX_NUMBER
                cam_params = client.get_params
                if !cam_params.empty? && !cam_params["user#{id}_name".to_sym].empty?
                        user = User.new(:username => cam_params["user#{id}_name".to_sym], :password => cam_params["user#{id}_pwd".to_sym], :privilege => cam_params["user#{id}_pri".to_sym])
                        user.instance_variable_set(:@id, id)
                end
        end
        user
end

Public Instance Methods

==(other) click to toggle source

Check for User equality @param other [User] @return [FalseClass, TrueClass] Whether or not the users are the same

# File lib/foscam/model/user.rb, line 126
def ==(other)
        other.equal?(self) || ( other.instance_of?(self.class) && other.id == @id && !other.id.nil? && !@id.nil?)
end
destroy() click to toggle source

Delete the current user @return [FalseClass, TrueClass] Whether or not the user was successfully deleted

# File lib/foscam/model/user.rb, line 133
def destroy
        run_callbacks :destroy do
                self.username = ""
                self.password = ""
                self.privilege = 0
                flag = @id.nil? ? false : client.set_users(dirty_params_hash)
                @changed_attributes.clear
                flag
        end
end
password=(val) click to toggle source
# File lib/foscam/model/user.rb, line 39
def password=(val)
        password_will_change! unless val == @password
        @password = val
end
privilege=(val) click to toggle source
# File lib/foscam/model/user.rb, line 44
def privilege=(val)
        privilege_will_change! unless val == @privilege
        @privilege = val
end
save() click to toggle source

Save the current user to the camera @return [FalseClass, TrueClass] Whether or not the user was successfully saved

# File lib/foscam/model/user.rb, line 109
def save
        run_callbacks :save do
                flag = false
                if changed? && is_valid? && set_id
                        @previously_changed = changes
                        # Get the first user that is not taken
                        flag = client.set_users(dirty_params_hash)
                        @changed_attributes.clear if flag
                end
                flag
        end
end
username=(val) click to toggle source
# File lib/foscam/model/user.rb, line 34
def username=(val)
        username_will_change! unless val == @username
        @username = val
end

Private Instance Methods

dirty_params_hash() click to toggle source
# File lib/foscam/model/user.rb, line 146
def dirty_params_hash
        h = {}
        h.merge!({"user#{@id}".to_sym => @username}) if username_changed?
        h.merge!({"pwd#{@id}".to_sym => @password}) if password_changed?
        h.merge!({"pri#{@id}".to_sym => @privilege}) if privilege_changed?
        h
end
set_id() click to toggle source
# File lib/foscam/model/user.rb, line 154
def set_id
        flag = false
        if @id.nil?
                cam_params = client.get_params
                unless cam_params.empty?
                        (1..8).each do |i|
                                if cam_params["user#{i}_name".to_sym].empty?
                                        @id = i
                                        flag = true
                                        break
                                end
                        end
                end
        else
                flag = true
        end
        flag
end