Class: Contentful::Array

Inherits:
BaseResource show all
Includes:
ArrayLike
Defined in:
lib/contentful/array.rb

Overview

Note:

It also provides an #each method and includes Ruby's Enumerable module (gives you methods like #min, #first, etc)

Resource Class for Arrays (e.g. search results)

See Also:

  • https://www.contentful.com/developers/documentation/content-delivery-api/#arrays

Instance Attribute Summary collapse

Attributes inherited from BaseResource

#default_locale, #raw, #sys

Instance Method Summary collapse

Methods included from ArrayLike

#[], #array?, #each_item, #empty?, #last, #size

Methods inherited from BaseResource

#==, #reload

Constructor Details

#initialize(item = nil, configuration = { default_locale: Contentful::Client::DEFAULT_CONFIGURATION[:default_locale] }, endpoint = '') ⇒ Array

Returns a new instance of Array



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/contentful/array.rb', line 16

def initialize(item = nil,
               configuration = {
                 default_locale: Contentful::Client::DEFAULT_CONFIGURATION[:default_locale]
               },
               endpoint = '', *)
  super(item, configuration)

  @endpoint = endpoint
  @total = item.fetch('total', nil)
  @limit = item.fetch('limit', nil)
  @skip = item.fetch('skip', nil)
  @items = item.fetch('items', [])
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint



14
15
16
# File 'lib/contentful/array.rb', line 14

def endpoint
  @endpoint
end

#itemsObject (readonly)

Returns the value of attribute items



14
15
16
# File 'lib/contentful/array.rb', line 14

def items
  @items
end

#limitObject (readonly)

Returns the value of attribute limit



14
15
16
# File 'lib/contentful/array.rb', line 14

def limit
  @limit
end

#skipObject (readonly)

Returns the value of attribute skip



14
15
16
# File 'lib/contentful/array.rb', line 14

def skip
  @skip
end

#totalObject (readonly)

Returns the value of attribute total



14
15
16
# File 'lib/contentful/array.rb', line 14

def total
  @total
end

Instance Method Details

#next_page(client = nil) ⇒ Contentful::Array, false

Simplifies pagination

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/contentful/array.rb', line 60

def next_page(client = nil)
  return false if client.nil?
  return false if items.first.nil?

  new_skip = (skip || 0) + (limit || DEFAULT_LIMIT)

  plurals = {
    'Space' => 'spaces',
    'ContentType' => 'content_types',
    'Entry' => 'entries',
    'Asset' => 'assets',
    'Locale' => 'locales'
  }
  client.public_send(plurals[items.first.type], limit: limit, skip: new_skip)
end