Class: RTX::API::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, resource_name, attrs = {}) ⇒ Collection

Returns a new instance of Collection.


6
7
8
9
10
11
# File 'lib/rtx/api/collection.rb', line 6

def initialize(client, resource_name, attrs = {})
  @client = client
  @resource_name = resource_name
  @options = symbolize_hash(attrs)
  @response = {}
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.


4
5
6
# File 'lib/rtx/api/collection.rb', line 4

def client
  @client
end

#optionsObject

Returns the value of attribute options.


4
5
6
# File 'lib/rtx/api/collection.rb', line 4

def options
  @options
end

#resource_nameObject

Returns the value of attribute resource_name.


4
5
6
# File 'lib/rtx/api/collection.rb', line 4

def resource_name
  @resource_name
end

#responseObject

Returns the value of attribute response.


4
5
6
# File 'lib/rtx/api/collection.rb', line 4

def response
  @response
end

Instance Method Details

#all_pages(initial_page = 1, &block) ⇒ Object

Allows you to loop through all of the pages and retrieve the records


98
99
100
101
102
103
104
105
# File 'lib/rtx/api/collection.rb', line 98

def all_pages(initial_page = 1, &block)
  page(initial_page)

  pages = (current_page..meta["_total_pages"]).to_a
  pages.each do |page_num|
    block.call(page(page_num).data)
  end
end

#all_resources(initial_page = 1, &block) ⇒ Object

Allows you to loop through all of the resources within the pages specified and retrieve the records


108
109
110
111
112
113
114
# File 'lib/rtx/api/collection.rb', line 108

def all_resources(initial_page = 1, &block)
  all_pages(initial_page) do |page|
    page.each do |resource|
      block.call(resource)
    end
  end
end

#create!(attrs = {}) ⇒ Object

Creates a new resource via POST and returns it on success


14
15
16
17
# File 'lib/rtx/api/collection.rb', line 14

def create!(attrs = {})
  client.authenticate if !client.authenticated?
  post(symbolize_hash(attrs))
end

#dataObject

Returns all data associated with the existing response


46
47
48
49
50
# File 'lib/rtx/api/collection.rb', line 46

def data
  client.authenticate if !client.authenticated?
  get if !has_response?
  response["_embedded"][resource_name]
end

#delete!(attrs = {}) ⇒ Object

Deletes a resource via DELETE and returns true on success


26
27
28
29
# File 'lib/rtx/api/collection.rb', line 26

def delete!(attrs = {})
  client.authenticate if !client.authenticated?
  delete(symbolize_hash(attrs))
end

#firstObject

For moving to the first page of the collection


82
83
84
85
# File 'lib/rtx/api/collection.rb', line 82

def first
  page(1)
  self
end

#has_next?Boolean

Responds true if the collection has another page ahead of it

Returns:

  • (Boolean)

88
89
90
# File 'lib/rtx/api/collection.rb', line 88

def has_next?
  current_page < meta["_total_pages"]
end

#has_previous?Boolean

Responds true if the collection has a previous one

Returns:

  • (Boolean)

93
94
95
# File 'lib/rtx/api/collection.rb', line 93

def has_previous?
  current_page > 1
end

#lastObject

For moving to the last page of the collection


76
77
78
79
# File 'lib/rtx/api/collection.rb', line 76

def last
  page(meta["_total_pages"])
  self
end

#metaObject

Returns the metadata about the current response


53
54
55
56
57
# File 'lib/rtx/api/collection.rb', line 53

def meta
  client.authenticate if !client.authenticated?
  get if !has_response?
  response.reject {|key,_| key == "_embedded"}
end

#nextObject

For moving forward one page with the collection


60
61
62
63
64
65
# File 'lib/rtx/api/collection.rb', line 60

def next
  if has_next?
    page(options[:page] += 1)
  end
  self
end

#page(num) ⇒ Object

Chainable method that allows you to set the page number of the collection for your request


39
40
41
42
43
# File 'lib/rtx/api/collection.rb', line 39

def page(num)
  clear if !num.nil?
  @options[:page] = num
  self
end

#per_page(num) ⇒ Object

Chainable method that allows you to set the per page number of the collection for your request


32
33
34
35
36
# File 'lib/rtx/api/collection.rb', line 32

def per_page(num)
  clear if !num.nil?
  @options[:per_page] = num
  self
end

#prevObject

For moving backward one page with the collection


68
69
70
71
72
73
# File 'lib/rtx/api/collection.rb', line 68

def prev
  if has_previous?
    page(options[:page] -= 1)
  end
  self
end

#update!(attrs = {}) ⇒ Object

Updates a resource via PUT and returns it on success


20
21
22
23
# File 'lib/rtx/api/collection.rb', line 20

def update!(attrs = {})
  client.authenticate if !client.authenticated?
  put(symbolize_hash(attrs))
end