Class: Kentaa::Api::Resources::List

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kentaa/api/resources/list.rb

Direct Known Subclasses

Users

Instance Attribute Summary

Attributes inherited from Base

#config, #endpoint_path, #options, #resource_class

Instance Method Summary collapse

Methods inherited from Base

attribute_key, #initialize, #load, #loaded?

Constructor Details

This class inherits a constructor from Kentaa::Api::Resources::Base

Instance Method Details

#[](index) ⇒ Object



9
10
11
# File 'lib/kentaa/api/resources/list.rb', line 9

def [](index)
  resources[index]
end

#allObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kentaa/api/resources/list.rb', line 69

def all
  enumerator = Enumerator.new do |yielder|
    page = 1

    loop do
      response = self.class.new(config, options.merge(resource_class: resource_class, endpoint_path: endpoint_path, page: page))
      response.each { |item| yielder.yield item }

      raise StopIteration unless response.next_page?

      page = response.next_page
    end
  end

  enumerator.lazy
end

#create(attributes, options = {}) ⇒ Object



91
92
93
94
# File 'lib/kentaa/api/resources/list.rb', line 91

def create(attributes, options = {})
  resource = resource_class.new(config, options: options.merge(endpoint_path: endpoint_path))
  resource.save(attributes)
end

#current_pageObject



41
42
43
# File 'lib/kentaa/api/resources/list.rb', line 41

def current_page
  body[:current_page]
end

#delete(id, options = {}) ⇒ Object



101
102
103
104
# File 'lib/kentaa/api/resources/list.rb', line 101

def delete(id, options = {})
  resource = resource_class.new(config, id: id, options: options.merge(endpoint_path: endpoint_path))
  resource.delete
end

#each(&block) ⇒ Object



17
18
19
# File 'lib/kentaa/api/resources/list.rb', line 17

def each(&block)
  resources.each(&block)
end

#get(id, options = {}) ⇒ Object



86
87
88
89
# File 'lib/kentaa/api/resources/list.rb', line 86

def get(id, options = {})
  resource = resource_class.new(config, id: id, options: options.merge(endpoint_path: endpoint_path))
  resource.load
end


21
22
23
# File 'lib/kentaa/api/resources/list.rb', line 21

def links
  body[:links]
end

#nextObject



61
62
63
# File 'lib/kentaa/api/resources/list.rb', line 61

def next
  self.class.new(config, options.merge(resource_class: resource_class, endpoint_path: endpoint_path, page: next_page)) if next_page?
end

#next_pageObject



45
46
47
# File 'lib/kentaa/api/resources/list.rb', line 45

def next_page
  current_page + 1 if next_page?
end

#next_page?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/kentaa/api/resources/list.rb', line 49

def next_page?
  current_page && current_page < total_pages
end

#pagesObject



25
26
27
# File 'lib/kentaa/api/resources/list.rb', line 25

def pages
  links[:pages] if links
end

#per_pageObject



37
38
39
# File 'lib/kentaa/api/resources/list.rb', line 37

def per_page
  body[:per_page]
end

#previousObject



65
66
67
# File 'lib/kentaa/api/resources/list.rb', line 65

def previous
  self.class.new(config, options.merge(resource_class: resource_class, endpoint_path: endpoint_path, page: previous_page)) if previous_page?
end

#previous_pageObject



53
54
55
# File 'lib/kentaa/api/resources/list.rb', line 53

def previous_page
  current_page - 1 if previous_page?
end

#previous_page?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/kentaa/api/resources/list.rb', line 57

def previous_page?
  current_page && current_page > 1
end

#sizeObject



13
14
15
# File 'lib/kentaa/api/resources/list.rb', line 13

def size
  resources.size
end

#total_entriesObject



29
30
31
# File 'lib/kentaa/api/resources/list.rb', line 29

def total_entries
  body[:total_entries]
end

#total_pagesObject



33
34
35
# File 'lib/kentaa/api/resources/list.rb', line 33

def total_pages
  body[:total_pages]
end

#update(id, attributes, options = {}) ⇒ Object



96
97
98
99
# File 'lib/kentaa/api/resources/list.rb', line 96

def update(id, attributes, options = {})
  resource = resource_class.new(config, id: id, options: options.merge(endpoint_path: endpoint_path))
  resource.save(attributes)
end