Class: ContextIO::Account

Inherits:
Object
  • Object
show all
Includes:
ContextIO::API::Resource
Defined in:
lib/contextio/account.rb

Instance Attribute Summary collapse

Attributes included from ContextIO::API::Resource

#api_attributes, #primary_key, #resource_url

Instance Method Summary collapse

Instance Attribute Details

#created_atObject (readonly)



54
55
56
# File 'lib/contextio/account.rb', line 54

def created_at
  @created_at ||= Time.at(created)
end

#first_nameString (readonly)

Returns The account holder's first name.

Returns:

  • (String)

    The account holder's first name.



28
29
# File 'lib/contextio/account.rb', line 28

lazy_attributes :id, :username, :created, :suspended, :first_name,
:last_name, :password_expired, :nb_messages, :nb_files

#idString (readonly)

Returns The id assigned to this account by Context.IO.

Returns:

  • (String)

    The id assigned to this account by Context.IO.



28
29
# File 'lib/contextio/account.rb', line 28

lazy_attributes :id, :username, :created, :suspended, :first_name,
:last_name, :password_expired, :nb_messages, :nb_files

#last_nameString (readonly)

Returns The account holder's last name.

Returns:

  • (String)

    The account holder's last name.



28
29
# File 'lib/contextio/account.rb', line 28

lazy_attributes :id, :username, :created, :suspended, :first_name,
:last_name, :password_expired, :nb_messages, :nb_files

#password_expired?Boolean (readonly)

Returns:

  • (Boolean)


86
87
88
# File 'lib/contextio/account.rb', line 86

def password_expired?
  !!password_expired_at
end

#password_expired_atObject (readonly)



76
77
78
79
80
81
82
# File 'lib/contextio/account.rb', line 76

def password_expired_at
  return @password_expired_at if instance_variable_defined?(:@password_expired_at)

  @password_expired_at = password_expired == 0 ? nil : Time.at(password_expired)

  @password_expired_at
end

#suspended?Boolean (readonly)

Returns:

  • (Boolean)


70
71
72
# File 'lib/contextio/account.rb', line 70

def suspended?
  !!suspended_at
end

#suspended_atObject (readonly)



60
61
62
63
64
65
66
# File 'lib/contextio/account.rb', line 60

def suspended_at
  return @suspended_at if instance_variable_defined?(:@suspended_at)

  @suspended_at = suspended == 0 ? nil : Time.at(suspended)

  @suspended_at
end

#usernameString (readonly)

Returns The username assigned to this account by Context.IO.

Returns:

  • (String)

    The username assigned to this account by Context.IO.



28
29
# File 'lib/contextio/account.rb', line 28

lazy_attributes :id, :username, :created, :suspended, :first_name,
:last_name, :password_expired, :nb_messages, :nb_files

Instance Method Details

#deleteObject



128
129
130
# File 'lib/contextio/account.rb', line 128

def delete
  api.request(:delete, resource_url)['success']
end

#email_addressesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/contextio/account.rb', line 32

def email_addresses
  # It would be nice if the data returned from the API were formatted like
  # other resources, but it isn't. So hacks.
  @email_addresses = nil if @email_addresses.is_a?(Array)

  return @email_addresses if @email_addresses

  association_class = ContextIO::API::AssociationHelpers.class_for_association_name(:email_addresses)

  reconstructed_email_hashes = api_attributes['email_addresses'].collect do |addy|
    {'email' => addy}
  end

  @email_addresses = association_class.new(
    api,
    self.class.association_name => self,
    attribute_hashes: reconstructed_email_hashes
  )
end

#sync!(options = {}) ⇒ Object



124
125
126
# File 'lib/contextio/account.rb', line 124

def sync!(options={})
  api.request(:post, "#{resource_url}/sync", options)['success']
end

#sync_dataObject



114
115
116
117
118
119
120
121
122
# File 'lib/contextio/account.rb', line 114

def sync_data
  return @sync_data if @sync_data

  sync_hashes = api.request(:get, "#{resource_url}/sync")

  @sync_data = ContextIO::AccountSyncData.new(sync_hashes)

  return @sync_data
end

#update(options = {}) ⇒ Object

Updates the account.

Parameters:

  • options (Hash{String, Symbol => String}) (defaults to: {})

    You can update first_name or last_name (or both).



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/contextio/account.rb', line 94

def update(options={})
  first_name = options[:first_name] || options['first_name']
  last_name = options[:last_name] || options['last_name']

  attrs = {}
  attrs[:first_name] = first_name if first_name
  attrs[:last_name] = last_name if last_name

  return nil if attrs.empty?

  it_worked = api.request(:post, resource_url, attrs)['success']

  if it_worked
    @first_name = first_name || @first_name
    @last_name = last_name || @last_name
  end

  it_worked
end