Module: ContextIO::API::ResourceCollection

Includes:
Enumerable
Included in:
Lite::EmailAccountCollection, Lite::FolderCollection, Lite::MessageCollection, Lite::UserCollection, Lite::WebhookCollection
Defined in:
lib/contextio/api/resource_collection.rb

Overview

When ‘include`d into a class, this module provides some helper methods for various things a collections of resources will need or find useful.

Defined Under Namespace

Modules: DeclarativeClassSyntax

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#apiObject (readonly)

Handle for the ‘API` instance. For internal use only.



9
10
11
# File 'lib/contextio/api/resource_collection.rb', line 9

def api
  @api
end

#resource_urlString (readonly)

Returns The URL that will fetch attributes from the API.

Returns:

  • (String)

    The URL that will fetch attributes from the API.



38
39
40
# File 'lib/contextio/api/resource_collection.rb', line 38

def resource_url
  @resource_url ||= api.url_for(self)
end

#where_constraintsObject (readonly)

Returns the value of attribute where_constraints.



13
14
15
# File 'lib/contextio/api/resource_collection.rb', line 13

def where_constraints
  @where_constraints
end

Instance Method Details

#[](key) ⇒ Object

Returns a resource with the given key.

This is a lazy method, making no requests. When you try to access attributes on the object, or otherwise interact with it, it will actually make requests.

Examples:

provider = contextio.oauth_providers['1234']

Parameters:

  • key (String)

    The Provider Consumer Key for the provider you want to interact with.



105
106
107
# File 'lib/contextio/api/resource_collection.rb', line 105

def [](key)
  resource_class.new(api, associations_hash.merge(resource_class.primary_key => key))
end

#each(&block) ⇒ Object

Iterates over the resources in question.

Examples:

contextio.connect_tokens.each do |connect_token|
  puts connect_token.email
end


48
49
50
51
52
# File 'lib/contextio/api/resource_collection.rb', line 48

def each(&block)
  attribute_hashes.each do |attribute_hash|
    yield resource_class.new(api, attribute_hash.merge(associations_hash))
  end
end

#empty?Boolean

Note:

Calling this method will load the collection if not already loaded.

Returns true if self contains no elements.

Returns:

  • (Boolean)


66
67
68
# File 'lib/contextio/api/resource_collection.rb', line 66

def empty?
  size == 0
end

#initialize(api, options = {}) ⇒ Object

For internal use only. Users of this gem shouldn’t be calling this directly.

Parameters:

  • api (API)

    A handle on the Context.IO API.

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

    Optional params for the collection.

Options Hash (options):

  • :where (Hash{Symbol => String, Numeric})

    Where constraints that limit the resources that belong to this collection.

  • :attribute_hashes (Array<Hash>)

    An array of hashes describing the resources in this collection.



26
27
28
29
30
31
32
33
34
# File 'lib/contextio/api/resource_collection.rb', line 26

def initialize(api, options={})
  @api = api
  @where_constraints = options[:where] || {}
  @attribute_hashes = options[:attribute_hashes]

  self.class.associations.each do |association_name|
    instance_variable_set("@#{association_name}", options[association_name.to_sym])
  end
end

#sizeObject Also known as: length, count

Note:

Calling this method will load the collection if not already loaded.

Returns the number of elements in self. May be zero.



57
58
59
# File 'lib/contextio/api/resource_collection.rb', line 57

def size
  attribute_hashes.size
end

#where(constraints) ⇒ Object

Specify one or more constraints for limiting resources in this collection. See individual classes in the [Context.IO docs](context.io/docs/2.0/) for the list of valid constraints. Not all collections have valid where constraints at all.

This can be chained at need and doesn’t actually cause the API to get hit until some iterator is called like ‘#each`.

Examples:

accounts = contextio.accounts
accounts = accounts.where(email: '[email protected]')
accounts = accounts.where(status: 'OK')

accounts.each do ||
  # API gets hit for this call
end

Parameters:

  • constraints (Hash{String, Symbol => String, Integer})

    A Hash mapping keys to the desired limiting values.



89
90
91
92
# File 'lib/contextio/api/resource_collection.rb', line 89

def where(constraints)
  constraints.each{|i,c| constraints[i] = (c ? 1 : 0) if c == !!c }
  self.class.new(api, associations_hash.merge(where: where_constraints.merge(constraints)))
end