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

Constant Summary collapse

DEFAULT_LIMIT =
100

Instance Attribute Summary collapse

Attributes inherited from BaseResource

#_metadata, #default_locale, #raw, #sys

Instance Method Summary collapse

Methods included from ArrayLike

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

Methods inherited from BaseResource

#==, #reload

Constructor Details

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

Returns a new instance of Array.



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

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

  @endpoint = endpoint
  @total = item.fetch('total', nil)
  @limit = item.fetch('limit', nil)
  @skip = item.fetch('skip', nil)
  @items = item.fetch('items', [])
  @query = query
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

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
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

#inspectObject



59
60
61
# File 'lib/contentful/array.rb', line 59

def inspect
  "<#{repr_name} total=#{total} skip=#{skip} limit=#{limit}>"
end

#marshal_dumpObject



34
35
36
# File 'lib/contentful/array.rb', line 34

def marshal_dump
  super.merge(endpoint: endpoint, query: query)
end

#marshal_load(raw_object) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/contentful/array.rb', line 39

def marshal_load(raw_object)
  super
  @endpoint = raw_object[:endpoint]
  @total = raw.fetch('total', nil)
  @limit = raw.fetch('limit', nil)
  @skip = raw.fetch('skip', nil)
  @query = raw_object[:query]
  @items = raw.fetch('items', []).map do |item|
    require_relative 'resource_builder'
    ResourceBuilder.new(
      item.raw,
      raw_object[:configuration].merge(includes_for_single: Support.includes_from_response(raw, false)),
      item.respond_to?(:localized) ? item.localized : false,
      0,
      raw_object[:configuration][:errors] || []
    ).run
  end
end

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

Simplifies pagination

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/contentful/array.rb', line 66

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], query.merge(limit: limit, skip: new_skip))
end