Class: ContextIO::Account
- Defined in:
- lib/context-io/account.rb
Overview
An account. Create one of these for every user.
This does not represent a mail account. An Account can have several mail accounts attached to it as Sources.
Only the #first_name and #last_name can be changed after creation.
Instance Attribute Summary collapse
-
#created ⇒ Time
readonly
When the account was created.
-
#email_addresses ⇒ Array<String>
readonly
The email addresses associated with the account.
-
#first_name ⇒ String
The first name of the account holder.
-
#id ⇒ String
readonly
The unique ID of the account.
-
#last_name ⇒ String
The last name of the account holder.
-
#password_expired ⇒ Time?
readonly
When the account password expired, or nil if the password hasn’t expired.
-
#sources ⇒ Array<Source>
readonly
The sources associated with this account.
-
#suspended ⇒ Time?
readonly
When the account was suspended, or nil if the account isn’t suspended.
-
#username ⇒ String
readonly
The username of the account.
Class Method Summary collapse
-
.all(query = {}) ⇒ Array<Account>
Get all the accounts, optionally filtered with a query.
-
.find(id) ⇒ Account
Find an account given its ID.
-
.from_json(json) ⇒ Account
private
Create an Account instance from the JSON returned by the Context.IO server.
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Account
constructor
Initialize an Account.
-
#save ⇒ true, false
Send the account info to Context.IO.
-
#update_attributes(attributes) ⇒ true, false
Update attributes on the account object and then send them to Context.IO.
Methods included from Request
#delete, #get, #post, #put, #request
Constructor Details
#initialize(attributes = {}) ⇒ Account
Initialize an Account
119 120 121 122 123 |
# File 'lib/context-io/account.rb', line 119 def initialize(attributes={}) @email_addresses = [attributes[:email]] if attributes[:email] @first_name = attributes[:first_name] @last_name = attributes[:last_name] end |
Instance Attribute Details
#created ⇒ Time (readonly)
Returns When the account was created.
23 24 25 |
# File 'lib/context-io/account.rb', line 23 def created @created end |
#email_addresses ⇒ Array<String> (readonly)
Returns The email addresses associated with the account.
32 33 34 |
# File 'lib/context-io/account.rb', line 32 def email_addresses @email_addresses end |
#first_name ⇒ String
Returns The first name of the account holder.
36 37 38 |
# File 'lib/context-io/account.rb', line 36 def first_name @first_name end |
#id ⇒ String (readonly)
Returns The unique ID of the account.
15 16 17 |
# File 'lib/context-io/account.rb', line 15 def id @id end |
#last_name ⇒ String
Returns The last name of the account holder.
40 41 42 |
# File 'lib/context-io/account.rb', line 40 def last_name @last_name end |
#password_expired ⇒ Time? (readonly)
Returns When the account password expired, or nil if the password hasn’t expired.
45 46 47 |
# File 'lib/context-io/account.rb', line 45 def password_expired @password_expired end |
#sources ⇒ Array<Source> (readonly)
Returns The sources associated with this account.
49 50 51 |
# File 'lib/context-io/account.rb', line 49 def sources @sources end |
#suspended ⇒ Time? (readonly)
Returns When the account was suspended, or nil if the account isn’t suspended.
28 29 30 |
# File 'lib/context-io/account.rb', line 28 def suspended @suspended end |
#username ⇒ String (readonly)
Returns The username of the account.
19 20 21 |
# File 'lib/context-io/account.rb', line 19 def username @username end |
Class Method Details
.all(query = {}) ⇒ Array<Account>
Get all the accounts, optionally filtered with a query
80 81 82 83 84 85 86 87 88 |
# File 'lib/context-io/account.rb', line 80 def self.all(query={}) query[:status] = query[:status].to_s.upcase if query[:status] if query.has_key?(:status_ok) query[:status_ok] = query[:status_ok] ? '1' : '0' end get('/2.0/accounts', query).map do |account| Account.from_json(account) end end |
.find(id) ⇒ Account
Find an account given its ID
100 101 102 |
# File 'lib/context-io/account.rb', line 100 def self.find(id) Account.from_json(get("/2.0/accounts/#{id}")) end |
.from_json(json) ⇒ Account
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create an Account instance from the JSON returned by the Context.IO server
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/context-io/account.rb', line 223 def self.from_json(json) account = new account.instance_eval do @id = json['id'] @username = json['username'] if json['created'] == 0 @created = nil else @created = Time.at(json['created']) end if json['suspended'] == 0 @suspended = nil else @suspended = Time.at(json['suspended']) end @email_addresses = json['email_addresses'] @first_name = json['first_name'] @last_name = json['last_name'] if json['password_expired'] == 0 @password_expired = nil else @password_expired = json['password_expired'] end @sources = json['sources'].map do |source| Source.from_json(@id, source) end end account end |
Instance Method Details
#save ⇒ true, false
Send the account info to Context.IO
If this is the first time the account is sent to Context.IO, the first email address set will be sent as the primary email address, and the first and last name will be sent if they are specified. You are required to specify one email address.
If the account has been sent to Context.IO before, this will update the first and last name.
145 146 147 |
# File 'lib/context-io/account.rb', line 145 def save self.id ? update_record : create_record end |
#update_attributes(attributes) ⇒ true, false
Update attributes on the account object and then send them to Context.IO
163 164 165 166 167 168 169 170 |
# File 'lib/context-io/account.rb', line 163 def update_attributes(attributes) @first_name = attributes[:first_name] if attributes[:first_name] @last_name = attributes[:last_name] if attributes[:last_name] response = put("/2.0/accounts/#{self.id}", attributes) response['success'] end |