Class: Foscam::Model::User

Inherits:
Base
  • Object
show all
Defined in:
lib/foscam/model/user.rb

Constant Summary collapse

MAX_NUMBER =

Max number of users supported by foscam

8

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#connect, #initialize, #persisted?

Constructor Details

This class inherits a constructor from Foscam::Model::Base

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/foscam/model/user.rb', line 14

def id
  @id
end

#passwordObject

Returns the value of attribute password.



24
25
26
# File 'lib/foscam/model/user.rb', line 24

def password
  @password
end

#privilegeObject

Returns the value of attribute privilege.



29
30
31
# File 'lib/foscam/model/user.rb', line 29

def privilege
  @privilege
end

#usernameObject

Returns the value of attribute username.



19
20
21
# File 'lib/foscam/model/user.rb', line 19

def username
  @username
end

Class Method Details

.allArray

Get all the users

Returns:

  • (Array)

    of Users



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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 = {}) ⇒ User?

Create a user with the specified parameters

Parameters:

  • params (Hash) (defaults to: {})

    User attributes

Options Hash (params):

  • :username (String)
  • :password (String)
  • :privilege (Symbol)

Returns:

  • (User, nil)

    Returns the user if successfully saved



74
75
76
77
# File 'lib/foscam/model/user.rb', line 74

def self.create(params ={})
	user = User.new(params)
	user.save ? user : nil
end

.delete(id) ⇒ FalseClass, TrueClass

Delete a user by the id

Parameters:

  • id (Fixnum)

    The id of the user

Returns:

  • (FalseClass, TrueClass)

    Whether or not the user was successfully deleted



100
101
102
103
# 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) ⇒ User

Find a specific user by name

Parameters:

  • id (Fixnum)

    The id of the user

Returns:



84
85
86
87
88
89
90
91
92
93
94
# 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

Instance Method Details

#==(other) ⇒ FalseClass, TrueClass

Check for User equality

Parameters:

Returns:

  • (FalseClass, TrueClass)

    Whether or not the users are the same



126
127
128
# 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

#destroyFalseClass, TrueClass

Delete the current user

Returns:

  • (FalseClass, TrueClass)

    Whether or not the user was successfully deleted



133
134
135
136
137
138
139
140
141
142
# 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

#saveFalseClass, TrueClass

Save the current user to the camera

Returns:

  • (FalseClass, TrueClass)

    Whether or not the user was successfully saved



109
110
111
112
113
114
115
116
117
118
119
120
# 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