Class: Gigya::User
- Inherits:
-
Object
- Object
- Gigya::User
- Defined in:
- lib/gigya/user.rb
Overview
A model to represent Gigya Account records provided by the API (as JSON hash)
Constant Summary collapse
- @@extra_profile_fields =
["locale", "phones"]
- @@default_gigya_user_class =
nil
- @@cache_options =
{}
Instance Attribute Summary collapse
-
#gigya_connection ⇒ Object
Returns the value of attribute gigya_connection.
-
#gigya_details ⇒ Object
Returns the value of attribute gigya_details.
Class Method Summary collapse
- .cache_options ⇒ Object
- .cache_options=(val) ⇒ Object
-
.create_gigya_user_through_notify_login(email, opts = {}) ⇒ Object
Creates a gigya user through the ‘notify_login` pathway.
-
.create_gigya_user_through_register(email, opts = {}) ⇒ Object
Options: :password => Set a password, :source => the registration source :account => hash of any account defaults you want to set.
- .default_gigya_user_class ⇒ Object
- .default_gigya_user_class=(val) ⇒ Object
- .extra_profile_fields ⇒ Object
- .extra_profile_fields=(val) ⇒ Object
-
.find(uid, opts = {}) ⇒ Object
Find a Gigya account record by its UID attribute.
- .find_by_email(email, opts = {}) ⇒ Object
- .from_string(str) ⇒ Object
- .load_from_cache(uid) ⇒ Object
Instance Method Summary collapse
- #birthday ⇒ Object
- #created_at ⇒ Object
- #email ⇒ Object
- #first_name ⇒ Object
- #full_name ⇒ Object
- #gender ⇒ Object
- #gender_string ⇒ Object
-
#initialize(json = {}, needs_caching = true) ⇒ User
constructor
A user can be initialized with a JSON hash of a Gigya account record.
- #last_name ⇒ Object
- #locale ⇒ Object
- #reload ⇒ Object
- #save ⇒ Object
- #save_to_cache ⇒ Object
- #set_attributes(json = {}) ⇒ Object
-
#uid ⇒ Object
Gigya accessors.
Constructor Details
#initialize(json = {}, needs_caching = true) ⇒ User
A user can be initialized with a JSON hash of a Gigya account record
40 41 42 43 44 45 46 |
# File 'lib/gigya/user.rb', line 40 def initialize(json = {}, needs_caching = true) # needs_caching is used for internal methods which load the record from cache and therefore don't need to save to cache set_attributes(json) save_to_cache if needs_caching return nil end |
Instance Attribute Details
#gigya_connection ⇒ Object
Returns the value of attribute gigya_connection.
4 5 6 |
# File 'lib/gigya/user.rb', line 4 def gigya_connection @gigya_connection end |
#gigya_details ⇒ Object
Returns the value of attribute gigya_details.
3 4 5 |
# File 'lib/gigya/user.rb', line 3 def gigya_details @gigya_details end |
Class Method Details
.cache_options ⇒ Object
31 32 33 |
# File 'lib/gigya/user.rb', line 31 def self. @@cache_options end |
.cache_options=(val) ⇒ Object
35 36 37 |
# File 'lib/gigya/user.rb', line 35 def self.(val) @@cache_options = val end |
.create_gigya_user_through_notify_login(email, opts = {}) ⇒ Object
Creates a gigya user through the ‘notify_login` pathway
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/gigya/user.rb', line 181 def self.create_gigya_user_through_notify_login(email, opts = {}) conn = opts[:gigya_connection] || Gigya::Connection.shared_connection # Create UUID new_uid = opts[:UID] || "#{SecureRandom.uuid.gsub("-", "")}#{SecureRandom.uuid.gsub("-", "")}" # Is the address available? email_is_available = conn.api_get("accounts", "isAvailableLoginID", { "loginID" => email }, :debug_connection => opts[:debug])["isAvailable"] rescue false raise "Username is unavailable" unless email_is_available # Register UUID response = conn.api_get("accounts", "notifyLogin", {"siteUID" => new_uid}, :debug_connection => opts[:debug]) raise "Could not register UID" unless response["errorCode"] == 0 || response["errorCode"] == 206001 # Start the registration process regtoken = conn.api_get("accounts", "initRegistration", {}, :debug_connection => opts[:debug])["regToken"] rescue nil raise "Could not initiate registration" if regtoken.blank? # Create the data record account_info = opts[:account] || {} # This allows the caller to send us defaults account_info["UID"] = new_uid # Primary key account_info["regToken"] = regtoken # Ties it to the initial registration account_info["securityOverride"] = true # Allows us to set passwords if we want account_info["profile"] ||= {} account_info["profile"]["email"] = email # Actual login username account_info["profile"] = account_info["profile"].to_json account_info["preferences"] = account_info["preferences"].to_json account_info["regSource"] = opts[:source] || "nm-gigya" # Optional data record pieces account_info["isVerified"] = true if opts[:verified] account_info["newPassword"] = opts[:password] unless opts[:password].blank? # Create the registration with the data record results = conn.api_post("accounts", "setAccountInfo", account_info, :debug_connection => opts[:debug]) # If not everything got set correctly (NOTE - doesn't work if :password is not also sent) if opts[:force] response = conn.api_get("accounts", "login", {"loginID" => email, "password" => opts[:password]}, :debug_connection => opts[:debug]) if response["errorCode"] != 0 verify_reg_token = response["regToken"] response = conn.api_get("accounts", "finalizeRegistration", {"regToken" => verify_reg_token, "include" => "emails, profile"}, :debug_connection => opts[:debug]) unless response["errorCode"] == 0 || response["errorCode"] == 206002 || response["errorCode"] == 206001 raise "Unable to finalize registration" end end end if opts[:send_verification] conn.api_get("accounts", "resendVerificationCode", {"UID" => new_uid, "email" => email}) end if opts[:send_password_change] conn.api_get("accounts", "resetPassword", {"UID" => new_uid, "loginID" => email, "email" => email}) end return new_uid end |
.create_gigya_user_through_register(email, opts = {}) ⇒ Object
Options:
:password => Set a password,
:source => the registration source
:account => hash of any account defaults you want to set. Profile defaults should be under the "profile" key.
:debug => will print out call information
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/gigya/user.rb', line 248 def self.create_gigya_user_through_register(email, opts = {}) conn = opts[:gigya_connection] || Gigya::Connection.shared_connection new_password = opts[:password] || SecureRandom.urlsafe_base64(8) # Create UUID new_uid = opts[:UID] || "#{SecureRandom.uuid.gsub("-", "")}#{SecureRandom.uuid.gsub("-", "")}" # Is the address available? email_is_available = conn.api_get("accounts", "isAvailableLoginID", { "loginID" => email }, :debug_connection => opts[:debug])["isAvailable"] rescue false raise "Username is unavailable" unless email_is_available # Start the registration process regtoken = conn.api_get("accounts", "initRegistration", {}, :debug_connection => opts[:debug])["regToken"] rescue nil raise "Could not initiate registration" if regtoken.blank? # Create the data record account_info = opts[:account] || {} # This allows the caller to send us defaults account_info["siteUID"] = new_uid # Primary key account_info["regToken"] = regtoken # Ties it to the initial registration account_info["profile"] ||= {} account_info["email"] = email account_info["profile"]["email"] = email # Actual login username account_info["profile"] = account_info["profile"].to_json account_info["preferences"] = account_info["preferences"].to_json unless account_info["preferences"].nil? account_info["regSource"] = opts[:source] unless opts[:source].blank? account_info["password"] = new_password account_info["data"] = account_info["data"].to_json unless account_info["data"].nil? # Complete the registration process conn.api_post("accounts", "register", account_info, :debug_connection => opts[:debug]) if opts[:send_verification] conn.api_get("accounts", "resendVerificationCode", {"UID" => new_uid, "email" => email}) end if opts[:send_password_change] conn.api_get("accounts", "resetPassword", {"UID" => new_uid, "loginID" => email, "email" => email}) end return new_uid end |
.default_gigya_user_class ⇒ Object
16 17 18 |
# File 'lib/gigya/user.rb', line 16 def self.default_gigya_user_class @@default_gigya_user_class end |
.default_gigya_user_class=(val) ⇒ Object
20 21 22 |
# File 'lib/gigya/user.rb', line 20 def self.default_gigya_user_class=(val) @@default_gigya_user_class = val end |
.extra_profile_fields ⇒ Object
11 12 13 |
# File 'lib/gigya/user.rb', line 11 def self.extra_profile_fields @@extra_profile_fields end |
.extra_profile_fields=(val) ⇒ Object
7 8 9 |
# File 'lib/gigya/user.rb', line 7 def self.extra_profile_fields=(val) @@extra_profile_fields = val end |
.find(uid, opts = {}) ⇒ Object
Find a Gigya account record by its UID attribute
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/gigya/user.rb', line 98 def self.find(uid, opts = {}) # Find a Gigya account record by its UID attribute opts = {} if opts.nil? opts[:cache] = true if opts[:cache].nil? cache_info = load_from_cache(uid) if cache_info.present? && opts[:cache] return self.new(cache_info, false) else connection = opts[:connection] || Gigya::Connection.shared_connection response = connection.api_get("accounts", "getAccountInfo", {UID: uid, include:"profile,data,subscriptions,userInfo,preferences", extraProfileFields:@@extra_profile_fields.join(",")}) obj = self.new(response) obj.gigya_connection = connection return obj end end |
.find_by_email(email, opts = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/gigya/user.rb', line 88 def self.find_by_email(email, opts = {}) email = email.gsub('"', '') # get rid of quotes opts = {} if opts.nil? conn = opts[:connection] || Gigya::Connection.shared_connection resp = conn.api_get("accounts", "search", {:query => "SELECT UID FROM accounts WHERE profile.email = \"#{email}\""}) uid = resp["results"][0]["UID"] rescue nil return nil if uid.blank? return self.find(uid, opts) end |
.from_string(str) ⇒ Object
24 25 26 27 28 |
# File 'lib/gigya/user.rb', line 24 def self.from_string(str) uc = @@default_gigya_user_class || Gigya::User uc = Kernel.const_get(uc) if String === uc uc.find(str) end |
.load_from_cache(uid) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/gigya/user.rb', line 80 def self.load_from_cache(uid) if defined?(Rails) return Rails.cache.read("gigya-user-#{uid}") else return nil end end |
Instance Method Details
#birthday ⇒ Object
139 140 141 142 |
# File 'lib/gigya/user.rb', line 139 def birthday profile = gigya_details["profile"] rescue nil Date.new(profile["birthYear"], profile["birthMonth"], profile["birthDay"]) rescue nil end |
#created_at ⇒ Object
119 120 121 |
# File 'lib/gigya/user.rb', line 119 def created_at DateTime.strptime(gigya_details["createdTimestamp"].to_s, "%Q") rescue nil end |
#email ⇒ Object
135 136 137 |
# File 'lib/gigya/user.rb', line 135 def email gigya_details["profile"]["email"].to_s.downcase rescue nil end |
#first_name ⇒ Object
127 128 129 |
# File 'lib/gigya/user.rb', line 127 def first_name gigya_details["profile"]["firstName"].to_s.capitalize rescue nil end |
#full_name ⇒ Object
123 124 125 |
# File 'lib/gigya/user.rb', line 123 def full_name [first_name, last_name].join(" ") end |
#gender ⇒ Object
144 145 146 |
# File 'lib/gigya/user.rb', line 144 def gender gigya_details["profile"]["gender"] rescue nil end |
#gender_string ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/gigya/user.rb', line 152 def gender_string begin case gigya_details["profile"]["gender"] when "f" "Female" when "m" "Male" else nil end rescue nil end end |
#last_name ⇒ Object
131 132 133 |
# File 'lib/gigya/user.rb', line 131 def last_name gigya_details["profile"]["lastName"].to_s.capitalize rescue nil end |
#locale ⇒ Object
148 149 150 |
# File 'lib/gigya/user.rb', line 148 def locale gigya_details["profile"]["locale"] rescue nil end |
#reload ⇒ Object
52 53 54 55 |
# File 'lib/gigya/user.rb', line 52 def reload conn = my_gigya_connection set_attributes(conn.api_get("accounts", "getAccountInfo", {UID: uid, include:"profile,data,subscriptions,userInfo,preferences", extraProfileFields:@@extra_profile_fields.join(",")})) end |
#save ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gigya/user.rb', line 57 def save info = {UID: uid} info["profile"] = gigya_details["profile"].to_json if gigya_details["profile"].present? info["data"] = gigya_details["data"].to_json if gigya_details["data"].present? # What about isActive, isVerified?, password/newPassword, preferences, add/removeLoginEmails, subscriptions, lang, rba conn = my_gigya_connection conn.api_post("accounts", "setAccountInfo", info) save_to_cache return true end |
#save_to_cache ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/gigya/user.rb', line 70 def save_to_cache if defined?(Rails) u = uid return if u.blank? # Don't save a blank object Rails.cache.write("gigya-user-#{u}", gigya_details) else # Nothing to do end end |
#set_attributes(json = {}) ⇒ Object
48 49 50 |
# File 'lib/gigya/user.rb', line 48 def set_attributes(json = {}) self.gigya_details = json end |
#uid ⇒ Object
Gigya accessors
115 116 117 |
# File 'lib/gigya/user.rb', line 115 def uid gigya_details["UID"] rescue nil end |