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

- (Account) initialize(attributes = {})

Initialize an Account

Examples:

Initialize an account with the email 'me@example.com'

ContextIO::Account.new(:email => 'me@example.com')

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.



116
117
118
119
120
# File 'lib/context-io/account.rb', line 116

def initialize(attributes={})
  @email_addresses = [attributes[:email]] if attributes[:email]
  @first_name = attributes[:first_name]
  @last_name = attributes[:last_name]
end

Instance Attribute Details

- (Time) created (readonly)

The account creation time.

Returns:

  • (Time)

    The account creation time.



21
22
23
# File 'lib/context-io/account.rb', line 21

def created
  @created
end

- (Array<String>) email_addresses (readonly)

The email addresses associated with the account.

Returns:

  • (Array<String>)

    The email addresses associated with the account.



30
31
32
# File 'lib/context-io/account.rb', line 30

def email_addresses
  @email_addresses
end

- (String) first_name

The first name of the account holder.

Returns:

  • (String)

    The first name of the account holder.



34
35
36
# File 'lib/context-io/account.rb', line 34

def first_name
  @first_name
end

- (String) id (readonly)

The unique ID of the account.

Returns:

  • (String)

    The unique ID of the account.



13
14
15
# File 'lib/context-io/account.rb', line 13

def id
  @id
end

- (String) last_name

The last name of the account holder.

Returns:

  • (String)

    The last name of the account holder.



38
39
40
# File 'lib/context-io/account.rb', line 38

def last_name
  @last_name
end

- (Time?) password_expired (readonly)

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.



43
44
45
# File 'lib/context-io/account.rb', line 43

def password_expired
  @password_expired
end

- (Array<ContextIO::Source>) sources (readonly)

The sources associated with this account.

Returns:



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

def sources
  @sources
end

- (Time?) suspended (readonly)

The account suspension time, or nil if the account isn't suspended.

Returns:

  • (Time, nil)

    The account suspension time, or nil if the account isn't suspended.



26
27
28
# File 'lib/context-io/account.rb', line 26

def suspended
  @suspended
end

- (String) username (readonly)

The username assigned to the account.

Returns:

  • (String)

    The username assigned to the account.



17
18
19
# File 'lib/context-io/account.rb', line 17

def username
  @username
end

Class Method Details

+ (Array<ContextIO::Account>) all(query = {})

Get all the accounts, optionally filtered with a query

Examples:

Fetch all accounts

ContextIO::Account.all

Fetch all accounts with the email address me@example.com

ContextIO::Account.all(:email => 'me@example.com')

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<ContextIO::Account>)

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



79
80
81
82
83
84
85
# File 'lib/context-io/account.rb', line 79

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

+ (ContextIO::Account) find(id)

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:



97
98
99
# File 'lib/context-io/account.rb', line 97

def self.find(id)
  from_json(get("/2.0/accounts/#{id}"))
end

+ (Account) from_json(json)

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.



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/context-io/account.rb', line 260

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

- (true, false) save

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 => 'me@example.com')
.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.



142
143
144
# File 'lib/context-io/account.rb', line 142

def save
  id ? update_record : create_record
end

- (void) sync!

Note:

This call is metered and charged for each source with basic service level. See Context.IO's pricing page for details.

This method returns an undefined value.

Trigger a sync of all sources on the account



205
206
207
# File 'lib/context-io/account.rb', line 205

def sync!
  post("/2.0/accounts/#{self.id}/sync")
end

- (Hash) sync_info

Get information about the last time mailboxes in this account were synced

The timestamps are unix timestamps (seconds since the epoch). Feed them into Time.at to get a Time object.

Examples:

.sync_info
# => {
#      'me@example.com::mail.example.com' => {
#        'INBOX' => {
#          'initial_import_finished' => true,
#          'last_expunge' => 1234567890,
#          'last_sync_start' => 1234567890,
#          'last_sync_stop' => 1234567892
#        }
#      }
#    }

Returns:

  • (Hash)

    Information about when mailboxes were synced. See the example for information about the hash format.



192
193
194
# File 'lib/context-io/account.rb', line 192

def sync_info
  get("/2.0/accounts/#{self.id}/sync")
end

- (true, false) update_attributes(attributes)

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.



160
161
162
163
164
165
166
167
# File 'lib/context-io/account.rb', line 160

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