Class: Whatsapp::MessageTemplates::Response::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ruby/whatsapp/message_templates/response/collection.rb

Overview

A page of templates: {data, paging, summary}.

Enumerable over its templates, so list.select(&:approved?) reads naturally without reaching for .data first.

Paging is exposed rather than followed automatically: the caller decides whether to walk the whole account, and passing #next_cursor back as after: is one line. Auto-pagination would hide an unbounded number of requests behind a single innocuous-looking call. Source: https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account/message_templates/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data: [], paging: nil, summary: nil) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • data (Array<Node>) (defaults to: [])
  • paging (Paging, nil) (defaults to: nil)
  • summary (Summary, nil) (defaults to: nil)


34
35
36
37
38
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 34

def initialize(data: [], paging: nil, summary: nil)
  @data = data
  @paging = paging
  @summary = summary
end

Instance Attribute Details

#dataArray<Node>

Returns:



21
22
23
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 21

def data
  @data
end

#pagingPaging?

Returns:



25
26
27
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 25

def paging
  @paging
end

#summarySummary?

Returns:



29
30
31
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 29

def summary
  @summary
end

Class Method Details

.deserialize(response) ⇒ Collection

Parameters:

  • response (Hash, nil)

    The parsed response body.

Returns:



43
44
45
46
47
48
49
50
51
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 43

def deserialize(response)
  response ||= {}

  new(
    data: Array(response["data"]).map { |node| Node.deserialize(node) },
    paging: response["paging"] && Paging.deserialize(response["paging"]),
    summary: response["summary"] && Summary.deserialize(response["summary"])
  )
end

Instance Method Details

#each {|template| ... } ⇒ Enumerator

Yield Parameters:

Returns:

  • (Enumerator)


56
57
58
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 56

def each(&)
  data.each(&)
end

#next_cursorString?

The cursor to pass as after: to fetch the next page.

Returns:

  • (String, nil)


62
63
64
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 62

def next_cursor
  paging&.after
end

#remainingInteger?

How many more templates the account can hold.

Returns:

  • (Integer, nil)


68
69
70
# File 'lib/ruby/whatsapp/message_templates/response/collection.rb', line 68

def remaining
  summary&.remaining
end