Class: ContextIO::Account

Inherits:
Resource show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

#delete, #get, #post, #put, #request

Constructor Details

#initialize(attributes = {}) ⇒ Account

Initialize an Account

Examples:

Initialize an account with the email ‘[email protected]

ContextIO::Account.new(:email => '[email protected]')

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to set on the account (all values are optional).

Options Hash (attributes):

  • :email (String)

    The primary email address of the account holder.

  • :first_name (String)

    The first name of the account holder.

  • :last_name (String)

    The last name of the account holder.



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

#createdTime (readonly)

Returns When the account was created.

Returns:

  • (Time)

    When the account was created.



23
24
25
# File 'lib/context-io/account.rb', line 23

def created
  @created
end

#email_addressesArray<String> (readonly)

Returns The email addresses associated with the account.

Returns:

  • (Array<String>)

    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_nameString

Returns The first name of the account holder.

Returns:

  • (String)

    The first name of the account holder.



36
37
38
# File 'lib/context-io/account.rb', line 36

def first_name
  @first_name
end

#idString (readonly)

Returns The unique ID of the account.

Returns:

  • (String)

    The unique ID of the account.



15
16
17
# File 'lib/context-io/account.rb', line 15

def id
  @id
end

#last_nameString

Returns The last name of the account holder.

Returns:

  • (String)

    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_expiredTime? (readonly)

Returns When the account password expired, or nil if the password hasn’t expired.

Returns:

  • (Time, nil)

    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

#sourcesArray<Source> (readonly)

Returns The sources associated with this account.

Returns:

  • (Array<Source>)

    The sources associated with this account.



49
50
51
# File 'lib/context-io/account.rb', line 49

def sources
  @sources
end

#suspendedTime? (readonly)

Returns When the account was suspended, or nil if the account isn’t suspended.

Returns:

  • (Time, nil)

    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

#usernameString (readonly)

Returns The username of the account.

Returns:

  • (String)

    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

Examples:

Fetch all accounts

ContextIO::Account.all

Fetch all accounts with the email address [email protected]

ContextIO::Account.all(:email => '[email protected]')

Parameters:

  • query (Hash) (defaults to: {})

    The query to filter accounts by. All fields are optional.

Options Hash (query):

  • :email (String)

    Only return accounts associated with this email address.

  • :status (:invalid_credentials, :connection_impossible, :no_access_to_all_mail, :ok, :temp_disabled, :disabled)

    Only return accounts with sources whose status is the one given. If an account has several sources, only those matching the given status will be included in the response.

  • :status_ok (true, false)

    Whether to only return accounts with sources that are working or not working properly (true/false, respectively). As with the ‘:status` filter above, only sources matching the specific value are included in the response.

  • :limit (Integer)

    The maximum number of results to return.

  • :offset (Integer)

    The offset to start the list at (0 is no offset).

Returns:

  • (Array<Account>)

    The accounts matching the query, or all if no query is given.



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.from_json()
  end
end

.find(id) ⇒ Account

Find an account given its ID

Examples:

Find the account with the ID ‘foobar’

ContextIO::Account.find('foobar')

Parameters:

  • id (String)

    The ID of the account to look up.

Returns:

  • (Account)

    The account with the given 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

Parameters:

  • json (Hash)

    The parsed JSON object returned by a Context.IO API request. See their documentation for what keys are possible.

Returns:

  • (Account)

    An account with the given attributes.



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)
   = new
  .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

  
end

Instance Method Details

#savetrue, 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.

Examples:

Create an account

 = ContextIO::Account.new(:email => '[email protected]')
.save

Returns:

  • (true, false)

    Whether the save succeeded or not.

Raises:

  • (ArgumentError)

    If there isn’t at least one email address specified in the #email_addresses field.



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

Examples:

Update the account holder’s name to “John Doe”

.update_attributes(:first_name => 'John', :last_name => 'Doe')

Parameters:

  • attributes (Hash)

    The attributes to update.

Options Hash (attributes):

  • :first_name (String)

    The first name of the account holder.

  • :last_name (String)

    The last name of the account holder.

Returns:

  • (true, false)

    Whether the update succeeded or not.



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