Class: SFRest::User
- Inherits:
-
Object
- Object
- SFRest::User
- Defined in:
- lib/sfrest/user.rb
Overview
Do User actions within the Site Factory
Instance Method Summary collapse
-
#create_user(name, email, datum = nil) ⇒ Object
Creates a user.
-
#delete_user(uid) ⇒ Object
Delete a user.
-
#get_user_data(uid) ⇒ Hash
Gets the data for user UID.
-
#get_user_id(username) ⇒ Integer
gets the site ID for the site named sitename will page through all the sites available searching for the site.
-
#initialize(conn) ⇒ User
constructor
A new instance of User.
-
#regenerate_apikey(uid) ⇒ Object
Regenerata a users apikey.
-
#regenerate_apikeys ⇒ Object
Regenerata all users apikeys.
-
#update_user(uid, datum = nil) ⇒ Object
Updates a user.
-
#user_data_from_results(res, username, key) ⇒ Object
Extract the user data for ‘key’ based on the user result object.
-
#user_list ⇒ Hash{'count' => Integer, 'users' => Hash}
Gets the complete list of users Makes multiple requests to the factory to get all the users on the factory.
Constructor Details
#initialize(conn) ⇒ User
Returns a new instance of User.
5 6 7 |
# File 'lib/sfrest/user.rb', line 5 def initialize(conn) @conn = conn end |
Instance Method Details
#create_user(name, email, datum = nil) ⇒ Object
Creates a user.
82 83 84 85 86 87 |
# File 'lib/sfrest/user.rb', line 82 def create_user(name, email, datum = nil) current_path = '/api/v1/users' payload = { name: name, mail: email } payload.merge!(datum) unless datum.nil? @conn.post(current_path, payload.to_json) end |
#delete_user(uid) ⇒ Object
Delete a user.
102 103 104 105 |
# File 'lib/sfrest/user.rb', line 102 def delete_user(uid) current_path = "/api/v1/users/#{uid}" @conn.delete(current_path) end |
#get_user_data(uid) ⇒ Hash
Gets the data for user UID
72 73 74 |
# File 'lib/sfrest/user.rb', line 72 def get_user_data(uid) @conn.get('/api/v1/users/' + uid.to_s) end |
#get_user_id(username) ⇒ Integer
gets the site ID for the site named sitename will page through all the sites available searching for the site
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sfrest/user.rb', line 40 def get_user_id(username) pglimit = 100 res = @conn.get('/api/v1/users&limit=' + pglimit.to_s) usercount = res['count'].to_i id = user_data_from_results(res, username, 'uid') return id if id pages = (usercount / pglimit) + 1 2.upto(pages) do |i| res = @conn.get('/api/v1/users&limit=' + pglimit.to_s + '?page=' + i.to_s) id = user_data_from_results(res, username, 'uid') return id if id end nil end |
#regenerate_apikey(uid) ⇒ Object
Regenerata a users apikey.
109 110 111 112 |
# File 'lib/sfrest/user.rb', line 109 def regenerate_apikey(uid) current_path = "/api/v1/users/#{uid}/api-keys" @conn.delete(current_path) end |
#regenerate_apikeys ⇒ Object
Regenerata all users apikeys.
115 116 117 118 |
# File 'lib/sfrest/user.rb', line 115 def regenerate_apikeys current_path = '/api/v1/users/all/api-keys' @conn.delete(current_path) end |
#update_user(uid, datum = nil) ⇒ Object
Updates a user.
94 95 96 97 98 |
# File 'lib/sfrest/user.rb', line 94 def update_user(uid, datum = nil) current_path = "/api/v1/users/#{uid}/update" payload = datum.to_json unless datum.nil? @conn.put(current_path, payload) end |
#user_data_from_results(res, username, key) ⇒ Object
Extract the user data for ‘key’ based on the user result object
61 62 63 64 65 66 67 |
# File 'lib/sfrest/user.rb', line 61 def user_data_from_results(res, username, key) users = res['users'] users.each do |user| return user[key] if user['name'] == username end nil end |
#user_list ⇒ Hash{'count' => Integer, 'users' => Hash}
Gets the complete list of users Makes multiple requests to the factory to get all the users on the factory
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sfrest/user.rb', line 12 def user_list page = 1 not_done = true count = 0 while not_done current_path = '/api/v1/users?page=' << page.to_s res = @conn.get(current_path) if res['users'] == [] not_done = false elsif !res['message'].nil? return { 'message' => res['message'] } elsif page == 1 count = res['count'] users = res['users'] else res['users'].each do |user| users << user end end page += 1 end { 'count' => count, 'users' => users } end |