Module: OnSIP::User::ClassMethods

Includes:
Model::ClassMethods
Included in:
OnSIP::User
Defined in:
lib/onsip/models/user.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :browse => {
    :order_by => {:k => 'OrderBy', :v => 'UserId'},
    :limit => {:k => 'Limit', :v => 100},
    :offset => {:k => 'Offset', :v => 0},
    :calc_found => {:k => 'CalcFound', :v => true}
  }
}

Instance Method Summary collapse

Methods included from Model::ClassMethods

#merge_params

Instance Method Details

#add(organization, attrs = {}) {|response| ... } ⇒ User

Adds a User to an Organization

reference at developer.onsip.com/admin-api/Users/#user-add

attrs = => ‘docs’,

'Name' => 'Docs',
'Email' => '[email protected]',
'AuthUsername' => 'example',
'Password' => 'mysuperpassword',
'PasswordConfirm' => 'mysuperpassword'

User.add(organization, attrs)

Examples:

Add User


Yields:

  • (response)

Returns:

  • (User)

    The created User.



144
145
146
147
148
149
150
151
152
153
# File 'lib/onsip/models/user.rb', line 144

def add(organization, attrs = {})
  params = attrs.merge({'Action' => 'UserAdd',
                        'SessionId' => OnSIP.session.id,
                        'OrganizationId' => organization.id,
                        'Domain' => organization.attributes.Domain,
                        'Output' => 'json'})
  response = OnSIP.connection.get('/api', params, {})
  yield response if block_given?
  process_add_user_response response
end

#browse(account_id, options = {}) {|response| ... } ⇒ Object

Yields:

  • (response)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/onsip/models/user.rb', line 73

def browse(, options = {})
  params = merge_params(DEFAULT_OPTIONS[:browse], options)
  params = params.merge({'Action' => 'UserBrowse',
                         'AccountId' => ,
                         'SessionId' => OnSIP.session.id,
                         'Output' => 'json'})

  response = OnSIP.connection.get('/api', params, {})
  yield response if block_given?
  process_browse_user_response response
end

#change_role(user_id, role) {|response| ... } ⇒ Object

Yields:

  • (response)


181
182
183
184
185
# File 'lib/onsip/models/user.rb', line 181

def change_role(user_id, role)
  response = OnSIP.connection.get('/api', {'Action' => 'UserEditRoleSubmit', 'UserId' => user_id, 'SessionId' => OnSIP.session.id, 'RoleNames[]' => role, 'Output' => 'json'}, {})
  yield response if block_given?
  process_change_role_user_response response
end

#delete!(user_id) {|response| ... } ⇒ Object

Yields:

  • (response)


95
96
97
98
99
100
# File 'lib/onsip/models/user.rb', line 95

def delete!(user_id)
  params = {'Action' => 'UserDelete', 'SessionId' => OnSIP.session.id, 'UserId' => user_id, 'Output' => 'json'}
  response = OnSIP.connection.get('/api', params, {})
  yield response if block_given?
  process_delete_user_response response
end

#edit_status(user_id, attrs = {}) {|response| ... } ⇒ Object

Yields:

  • (response)


112
113
114
115
116
117
# File 'lib/onsip/models/user.rb', line 112

def edit_status(user_id, attrs = {})
  params = attrs.merge({'Action' => 'UserEditStatus', 'SessionId' => OnSIP.session.id, 'UserId' => user_id, 'Output' => 'json'})
  response = OnSIP.connection.get('/api', params, {})
  yield response if block_given?
  process_edit_user_status_response response
end

#generate_random_password(length = 8) ⇒ Object



197
198
199
# File 'lib/onsip/models/user.rb', line 197

def generate_random_password(length = 8)
  SecureRandom.urlsafe_base64[0,8]
end

#process_add_user_response(response) ⇒ Object



155
156
157
158
159
160
161
162
163
# File 'lib/onsip/models/user.rb', line 155

def process_add_user_response(response)
  user = nil

  key_path = %w(Response Result UserAdd User)
  a = ResponseParser.parse_response response, key_path
  user = (a.map { |h| new h }).first if a

  user
end

#process_browse_user_response(response) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/onsip/models/user.rb', line 85

def process_browse_user_response(response)
  users = []

  key_path = %w(Response Result UserBrowse Users User)
  a = ResponseParser.parse_response response, key_path
  users = a.map { |h| new h } if a

  users
end

#process_change_role_user_response(response) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/onsip/models/user.rb', line 187

def process_change_role_user_response(response)
  user = nil

  key_path = %w(Response Result UserEditRoleSubmit User)
  a = ResponseParser.parse_response response, key_path
  user = (a.map { |h| new h }).first if a

  user
end

#process_delete_user_response(response) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/onsip/models/user.rb', line 102

def process_delete_user_response(response)
  r = response.env.body['Response']

  if r && r['Context'] && r['Context']['Action'] && r['Context']['Action']
    return true
  else
    raise OnSIPRequestException, 'Problem with user request'
  end
end

#process_edit_user_status_response(response) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/onsip/models/user.rb', line 119

def process_edit_user_status_response(response)
  user = nil
  r = response.env.body['Response']

  key_path = %w(Response Result UserEditStatus User)
  a = ResponseParser.parse_response response, key_path
  user = (a.map { |h| new h }).first if a

  user
end

#process_read_user_response(response) ⇒ Object



171
172
173
174
175
176
177
178
179
# File 'lib/onsip/models/user.rb', line 171

def process_read_user_response(response)
  user = nil

  key_path = %w(Response Result UserRead User)
  a = ResponseParser.parse_response response, key_path
  user = (a.map { |h| new h }).first if a

  user
end

#read(user_id) {|response| ... } ⇒ Object

Yields:

  • (response)


165
166
167
168
169
# File 'lib/onsip/models/user.rb', line 165

def read(user_id)
  response = OnSIP.connection.get('/api', {'Action' => 'UserRead', 'UserId' => user_id, 'SessionId' => OnSIP.session.id, 'Output' => 'json'}, {})
  yield response if block_given?
  process_read_user_response response
end