Class: Cyclid::UI::Models::User
- Inherits:
-
Object
- Object
- Cyclid::UI::Models::User
- Defined in:
- app/cyclid_ui/models/user.rb
Overview
A User object. This is really no more than a simple wrapper around the Cyclid user data that is returned from the API, with the added layer of Memecached caching of the object to avoid API calls.
Instance Attribute Summary collapse
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#organizations ⇒ Object
readonly
Returns the value of attribute organizations.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.get(args) ⇒ Object
Try to find the user object in Memcached; if it does not exist, fallback to the API.
- .user_fetch(args) ⇒ Object
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ User
constructor
A new instance of User.
- #to_hash ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ User
Returns a new instance of User.
25 26 27 28 29 30 31 |
# File 'app/cyclid_ui/models/user.rb', line 25 def initialize(args = {}) @username = args['username'] || nil @email = args['email'] || nil @name = args['name'] || nil @organizations = args['organizations'] || [] @id = args['id'] || nil end |
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
23 24 25 |
# File 'app/cyclid_ui/models/user.rb', line 23 def email @email end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
23 24 25 |
# File 'app/cyclid_ui/models/user.rb', line 23 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
23 24 25 |
# File 'app/cyclid_ui/models/user.rb', line 23 def name @name end |
#organizations ⇒ Object (readonly)
Returns the value of attribute organizations.
23 24 25 |
# File 'app/cyclid_ui/models/user.rb', line 23 def organizations @organizations end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
23 24 25 |
# File 'app/cyclid_ui/models/user.rb', line 23 def username @username end |
Class Method Details
.get(args) ⇒ Object
Try to find the user object in Memcached; if it does not exist, fallback to the API. If the API returns the user data, it will be cached into Memcache for future use.
If we have to fall back to the API we assume that the username is valid and either the HTTP Basic password or an API token are available and valid.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/cyclid_ui/models/user.rb', line 48 def self.get(args) username = args[:username] || args['username'] memcache = Memcache.new(server: Cyclid.config.memcached) user_data = begin memcache.cache username do user_fetch(args) end rescue Memcached::ServerIsMarkedDead => ex Cyclid.logger.fatal "cannot connect to memcached: #{ex}" # Fall back to a direct API connection user_fetch(args) end new(user_data) end |
.user_fetch(args) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/cyclid_ui/models/user.rb', line 65 def self.user_fetch(args) username = args[:username] || args['username'] password = args[:password] || args['password'] token = args[:token] || args['token'] auth_method = token.nil? ? Client::AUTH_BASIC : Client::AUTH_TOKEN user_data = nil begin Cyclid.logger.debug "api=#{Cyclid.config.server_api.inspect}" client = Client::Tilapia.new(auth: auth_method, server: Cyclid.config.server_api.host, port: Cyclid.config.server_api.port, username: username, password: password, token: token) user_data = client.user_get(username) Cyclid.logger.debug "got #{user_data}" rescue StandardError => ex Cyclid.logger.fatal "failed to get user details: #{ex}" raise ex end user_data end |
Instance Method Details
#to_hash ⇒ Object
33 34 35 36 37 38 39 |
# File 'app/cyclid_ui/models/user.rb', line 33 def to_hash { 'username' => @username, 'email' => @email, 'name' => @name, 'organizations' => @organizations, 'id' => @id } end |