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.

[View source]

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.to_sym
  @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

[View source]

110
111
112
113
114
115
116
117
# File 'lib/rtx/api/collection.rb', line 110

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

[View source]

120
121
122
123
124
125
126
# File 'lib/rtx/api/collection.rb', line 120

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

[View source]

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

[View source]

58
59
60
61
62
# File 'lib/rtx/api/collection.rb', line 58

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

#delete!(attrs = {}) ⇒ Object

Deletes a resource via DELETE and returns true on success

[View source]

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

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

#detail!(attrs = {}) ⇒ Object

Gets an individual resource returns an object instead of an array

[View source]

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

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

#firstObject

For moving to the first page of the collection

[View source]

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

def first
  page(1)
  self
end

#has_next?Boolean

Responds true if the collection has another page ahead of it

Returns:

  • (Boolean)
[View source]

100
101
102
# File 'lib/rtx/api/collection.rb', line 100

def has_next?
  current_page < meta[:_total_pages]
end

#has_previous?Boolean

Responds true if the collection has a previous one

Returns:

  • (Boolean)
[View source]

105
106
107
# File 'lib/rtx/api/collection.rb', line 105

def has_previous?
  current_page > 1
end

#lastObject

For moving to the last page of the collection

[View source]

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

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

#metaObject

Returns the metadata about the current response

[View source]

65
66
67
68
69
# File 'lib/rtx/api/collection.rb', line 65

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

#nextObject

For moving forward one page with the collection

[View source]

72
73
74
75
76
77
# File 'lib/rtx/api/collection.rb', line 72

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

[View source]

51
52
53
54
55
# File 'lib/rtx/api/collection.rb', line 51

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

#patch!(attrs = {}) ⇒ Object

Updates a resource via PATCH and returns it on success

[View source]

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

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

#per_page(num) ⇒ Object

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

[View source]

44
45
46
47
48
# File 'lib/rtx/api/collection.rb', line 44

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

#prevObject

For moving backward one page with the collection

[View source]

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

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

[View source]

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

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