Module: ContextIO::API::ResourceCollection

Overview

When included 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

#resource_urlString (readonly)

Returns The URL that will fetch attributes from the API.

Returns:

  • (String)

    The URL that will fetch attributes from the API.



40
41
42
# File 'lib/contextio/api/resource_collection.rb', line 40

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

#where_constraintsObject (readonly)

Returns the value of attribute where_constraints.



15
16
17
# File 'lib/contextio/api/resource_collection.rb', line 15

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.



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

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


50
51
52
53
54
# File 'lib/contextio/api/resource_collection.rb', line 50

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)


68
69
70
# File 'lib/contextio/api/resource_collection.rb', line 68

def empty?
  size == 0
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.



59
60
61
# File 'lib/contextio/api/resource_collection.rb', line 59

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 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.



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

def where(constraints)
  self.class.new(api, associations_hash.merge(where: where_constraints.merge(constraints)))
end