Module: Cyclid::Client::User

Included in:
Tilapia
Defined in:
lib/cyclid/client/user.rb

Overview

User related methods

Instance Method Summary collapse

Instance Method Details

#user_add(username, email, name = nil, password = nil, secret = nil) ⇒ Hash

Create a new user

Parameters:

  • username (String)

    User name of the new user.

  • name (String) (defaults to: nil)

    Users real name.

  • email (String)

    Users email address.

  • password (String) (defaults to: nil)

    Unencrypted initial password

  • secret (String) (defaults to: nil)

    Initial HMAC signing secret

Returns:

  • (Hash)

    Decoded server response object.

See Also:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cyclid/client/user.rb', line 56

def user_add(username, email, name = nil, password = nil, secret = nil)
  # Create the user object
  user = { 'username' => username, 'email' => email }

  # Add the real name is one was supplied
  user['name'] = name unless name.nil?

  # Add the HMAC secret if one was supplied
  user['secret'] = secret unless secret.nil?

  # Encrypt & add the password if one was supplied
  user['password'] = BCrypt::Password.create(password).to_s unless password.nil?

  @logger.debug user

  # Sign & send the request
  uri = server_uri('/users')
  res_data = api_json_post(uri, user)
  @logger.debug res_data

  return res_data
end

#user_delete(username) ⇒ Hash

Delete a user

Parameters:

  • username (String)

    User name of the user to delete.

Returns:

  • (Hash)

    Decoded server response object.

See Also:



125
126
127
128
129
130
131
# File 'lib/cyclid/client/user.rb', line 125

def user_delete(username)
  uri = server_uri("/users/#{username}")
  res_data = api_delete(uri)
  @logger.debug res_data

  return res_data
end

#user_get(username) ⇒ Hash

Get details of a specific user

Parameters:

  • username (String)

    User name of the user to retrieve.

Returns:

  • (Hash)

    Decoded server response object.



39
40
41
42
43
44
45
# File 'lib/cyclid/client/user.rb', line 39

def user_get(username)
  uri = server_uri("/users/#{username}")
  res_data = api_get(uri)
  @logger.debug res_data

  return res_data
end

#user_listArray

Retrieve the list of users from a server

Returns:

  • (Array)

    List of user names.



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cyclid/client/user.rb', line 23

def user_list
  uri = server_uri('/users')
  res_data = api_get(uri)
  @logger.debug res_data

  users = []
  res_data.each do |item|
    users << item['username']
  end

  return users
end

#user_modify(username, args) ⇒ Hash

Modify a user

Examples:

Change the email address of the user ‘leslie’

user_modify('leslie', email: '[email protected]')

Change the password & secret of the user ‘bob’

user_modify('bob', secret: 'sekrit', password: 'm1lkb0ne')

Parameters:

  • username (String)

    User name of the new user.

  • args (Hash)

    options to modify the user.

Options Hash (args):

  • name (String)

    Users real name.

  • email (String)

    Users email address.

  • secret (String)

    Initial HMAC signing secret

  • password (String)

    Unencrypted initial password

Returns:

  • (Hash)

    Decoded server response object.

See Also:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cyclid/client/user.rb', line 93

def user_modify(username, args)
  # Create the user object
  user = {}

  # Add the real name is one was supplied
  user['name'] = args[:name] if args.key? :name and args[:name]

  # Add the email address if one was supplied
  user['email'] = args[:email] if args.key? :email and args[:email]

  # Add the HMAC secret if one was supplied
  user['secret'] = args[:secret] if args.key? :secret and args[:secret]

  # Encrypt & add the password if one was supplied
  user['password'] = BCrypt::Password.create(args[:password]).to_s \
    if args.key? :password and args[:password]

  @logger.debug user

  # Sign & send the request
  uri = server_uri("/users/#{username}")
  res_data = api_json_put(uri, user)
  @logger.debug res_data

  return res_data
end