Class: Travis::Client::Collection

Inherits:
Entity
  • Object
show all
Includes:
Enumerable
Defined in:
lib/travis/client/collection.rb

Instance Attribute Summary

Attributes inherited from Entity

#session

Instance Method Summary collapse

Methods inherited from Entity

#[], add_action, add_attribute, add_related_action, for_session, #initialize, #inspect, #merge!, #permission?, #to_entity, #to_h

Constructor Details

This class inherits a constructor from Travis::Client::Entity

Instance Method Details

#collection_keyObject



26
27
28
29
30
31
# File 'lib/travis/client/collection.rb', line 26

def collection_key
  @collection_key ||= begin
    session.fetch(@href) if @payload.keys.all? { |k| k.start_with? '@' }
    @payload.keys.detect { |k| !k.start_with?('@') and self[k].is_a? Array }
  end
end

#each(&block) ⇒ Object



55
56
57
58
59
60
# File 'lib/travis/client/collection.rb', line 55

def each(&block)
  return enum_for(:each) unless block
  on_page.each(&block)
  next_page&.each(&block)
  self
end

#first_pageObject



45
46
47
48
# File 'lib/travis/client/collection.rb', line 45

def first_page
  return self if first_page?
  pagination_info['first']&.fetch
end

#first_page?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/travis/client/collection.rb', line 22

def first_page?
  pagination_info.fetch('is_first', true)
end

#last_pageObject



50
51
52
53
# File 'lib/travis/client/collection.rb', line 50

def last_page
  return self if last_page?
  pagination_info['last']&.fetch
end

#last_page?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/travis/client/collection.rb', line 18

def last_page?
  pagination_info.fetch('is_last', true)
end

#next_pageObject



37
38
39
# File 'lib/travis/client/collection.rb', line 37

def next_page
  pagination_info['next']&.fetch
end

#offsetObject



10
11
12
# File 'lib/travis/client/collection.rb', line 10

def offset
  pagination_info['offset'] || 0
end

#on_pageObject



14
15
16
# File 'lib/travis/client/collection.rb', line 14

def on_page
  @on_page ||= Array(@payload[collection_key])
end

#paginates?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/travis/client/collection.rb', line 6

def paginates?
  !!@payload['@pagination']
end

#pagination_infoObject



62
63
64
# File 'lib/travis/client/collection.rb', line 62

def pagination_info
  @pagination_info ||= @payload['@pagination'] || {}
end

#previous_pageObject



41
42
43
# File 'lib/travis/client/collection.rb', line 41

def previous_page
  pagination_info['prev']&.fetch
end

#sizeObject



33
34
35
# File 'lib/travis/client/collection.rb', line 33

def size
  pagination_info['count'] || on_page.size
end