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.



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

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.



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

def endpoint
  @endpoint
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

#queryObject (readonly)

Returns the value of attribute query.



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

def query
  @query
end

#skipObject (readonly)

Returns the value of attribute skip.



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

def skip
  @skip
end

#totalObject (readonly)

Returns the value of attribute total.



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

def total
  @total
end

Instance Method Details

#inspectObject



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

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

#marshal_dumpObject



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

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

#marshal_load(raw_object) ⇒ Object



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

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



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

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