Class: ContextIO::Source

Inherits:
Resource show all
Defined in:
lib/context-io/source.rb

Overview

A message source. Create one of these for each mailbox a user has

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Request

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

Constructor Details

#initialize(account_id, attributes = {}) ⇒ Source

Returns a new instance of Source.

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/context-io/source.rb', line 57

def initialize(, attributes = {})
  raise ArgumentError if .to_s.empty?

  @account_id = .to_s
  @email = attributes['email']
  @label = attributes['label'] || ''
  @authentication_type = attributes['authentication_type']
  @port = attributes['port'] || 143
  @service_level = attributes['service_level']
  @username = attributes['username']
  @server = attributes['server']
  @source_type = attributes['type'] || 'IMAP'
  @sync_period = attributes['sync_period']
  @use_ssl = attributes['use_ssl'] || false
  @status = attributes['status']
  @password = attributes['password']
  @provider_token = attributes['provider_token']
  @provider_token_secret = attributes['provider_token_secret']
  @provider_consumer_key = attributes['provider_consumer_key']
end

Instance Attribute Details

#account_idObject (readonly)



10
11
12
# File 'lib/context-io/source.rb', line 10

def 
  @account_id
end

#authentication_typeObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def authentication_type
  @authentication_type
end

#emailObject (readonly)



10
11
12
# File 'lib/context-io/source.rb', line 10

def email
  @email
end

#labelObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def label
  @label
end

#portObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def port
  @port
end

#serverObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def server
  @server
end

#service_levelObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def service_level
  @service_level
end

#source_typeObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def source_type
  @source_type
end

#statusObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def status
  @status
end

#sync_periodObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def sync_period
  @sync_period
end

#use_sslObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def use_ssl
  @use_ssl
end

#usernameObject



8
9
10
# File 'lib/context-io/source.rb', line 8

def username
  @username
end

Class Method Details

.all(account, query = {}) ⇒ Object

Public: Get all sources for given account.

query - An optional Hash (default: {}) containing a query to filter the

responses. For possible values see Context.IO API documentation.

Returns an Array of Source objects.

account - Account object or ID



19
20
21
22
23
24
25
26
# File 'lib/context-io/source.rb', line 19

def self.all(, query = {})
  return [] if .nil?

   = .is_a?(Account) ? .id : .to_s
  get("/2.0/accounts/#{}/sources", query).map do |msg|
    Source.from_json(, msg)
  end
end

.find(account, label) ⇒ Source

Find a source for given ID

Examples:

Find the source with the labe ‘foobar’

ContextIO::Source.find('abcdef012345', 'foobar')

Parameters:

  • label (String)

    The label of the source to look up.

Returns:

  • (Source)

    The source with the given label.



38
39
40
41
42
43
# File 'lib/context-io/source.rb', line 38

def self.find(, label)
  return nil if .nil? or label.to_s.empty?
   = .is_a?(Account) ? .id : .to_s

  Source.from_json(, get("/2.0/accounts/#{}/sources/#{label.to_s}"))
end

.from_json(account_id, json) ⇒ Source

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 a Source instance from the JSON returned by the Context.IO server

Parameters:

  • The (Hash)

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

Returns:

  • (Source)

    A source with the given attributes.



53
54
55
# File 'lib/context-io/source.rb', line 53

def self.from_json(, json)
  source = new(, json)
end

Instance Method Details

#destroyObject

Destroys current source object



103
104
105
106
107
108
109
110
# File 'lib/context-io/source.rb', line 103

def destroy
  return false if @label.to_s.empty?

  response = delete("/2.0/accounts/#{@account_id}/sources/#{@label}")
  @label = '' if response['success']

  response['success']
end

#foldersObject

Returns all source’s folders.



79
80
81
# File 'lib/context-io/source.rb', line 79

def folders
  ContextIO::Folder.all(@account_id, @label)
end

#savetrue, false

Sends the source data to Context.IO

If the source has been sent to Context.IO before, this will update allowed source attributes.

Examples:

Create a source

source = ContextIO::Source.new(@account.id, {'email' => '[email protected]', 'server' => '[email protected]',
   'username' => "me", 'use_ssl' => true, 'port' => 143, 'type' => 'IMAP'})
source.save

Returns:

  • (true, false)

    Whether the save succeeded or not.

Raises:

  • (ArgumentError)

    If required arguments are missing.



98
99
100
# File 'lib/context-io/source.rb', line 98

def save
  @label.to_s.empty? ? create_record : update_record
end

#update_attributes(attributes = {}) ⇒ true, false

Update attributes on the Source object and then send them to Context.IO

attributes are status, sync period, service level, password, provider token, provider token secret and provider consumer key

Examples:

Update the Source sync period to one day

source.update_attributes('sync_period' => '1d')

Parameters:

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

    The attributes to update. Allowed

Returns:

  • (true, false)

    Whether the update succeeded or not.

Raises:

  • (ArgumentError)


124
125
126
127
128
129
130
131
132
133
# File 'lib/context-io/source.rb', line 124

def update_attributes(attributes = {})
  raise ArgumentError.new("Cannot set attributes on new record") if @label.to_s.empty?
  
  attributes.each do |k,v|
    if ["status", "sync_period", "service_level", "password", "provider_token", "provider_token_secret", "provider_consumer_key"].include? k
      send("#{k}=", v)
    end
  end
  update_record
end