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.
7 8 9 |
# File 'lib/sfrest/user.rb', line 7 def initialize(conn) @conn = conn end |
Instance Method Details
#create_user(name, email, datum = nil) ⇒ Object
Creates a user.
84 85 86 87 88 89 |
# File 'lib/sfrest/user.rb', line 84 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.
104 105 106 107 |
# File 'lib/sfrest/user.rb', line 104 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
74 75 76 |
# File 'lib/sfrest/user.rb', line 74 def get_user_data(uid) @conn.get("/api/v1/users/#{uid}") 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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/sfrest/user.rb', line 42 def get_user_id(username) pglimit = 100 res = @conn.get("/api/v1/users&limit=#{pglimit}") 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}?page=#{i}") id = user_data_from_results(res, username, 'uid') return id if id end nil end |
#regenerate_apikey(uid) ⇒ Object
Regenerata a users apikey.
111 112 113 114 |
# File 'lib/sfrest/user.rb', line 111 def regenerate_apikey(uid) current_path = "/api/v1/users/#{uid}/api-keys" @conn.delete(current_path) end |
#regenerate_apikeys ⇒ Object
Regenerata all users apikeys.
117 118 119 120 |
# File 'lib/sfrest/user.rb', line 117 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.
96 97 98 99 100 |
# File 'lib/sfrest/user.rb', line 96 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
63 64 65 66 67 68 69 |
# File 'lib/sfrest/user.rb', line 63 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
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sfrest/user.rb', line 14 def user_list page = 1 not_done = true count = 0 while not_done current_path = "/api/v1/users?page=#{page}" 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 |