Class: Pinata::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/pinata/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, next_page_token:) ⇒ Collection

Returns a new instance of Collection.



15
16
17
18
# File 'lib/pinata/collection.rb', line 15

def initialize(data:, next_page_token:)
  @data = data
  @next_page_token = (next_page_token.nil? || next_page_token.empty?) ? nil : next_page_token
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/pinata/collection.rb', line 5

def data
  @data
end

#next_page_tokenObject (readonly)

Returns the value of attribute next_page_token.



5
6
7
# File 'lib/pinata/collection.rb', line 5

def next_page_token
  @next_page_token
end

Class Method Details

.from_response(response, key:, type:) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/pinata/collection.rb', line 7

def self.from_response(response, key:, type:)
  body = response.body
  new(
    data: body["data"][key].map { |attrs| type.new(attrs) },
    next_page_token: body.dig("data", "next_page_token")
  )
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/pinata/collection.rb', line 20

def empty?
  data.empty?
end

#sampleObject



24
25
26
# File 'lib/pinata/collection.rb', line 24

def sample
  data.sample
end