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.



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

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.



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

def client
  @client
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#resource_nameObject

Returns the value of attribute resource_name.



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

def resource_name
  @resource_name
end

#responseObject

Returns the value of attribute response.



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

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



114
115
116
117
118
119
120
121
# File 'lib/rtx/api/collection.rb', line 114

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



125
126
127
128
129
130
131
# File 'lib/rtx/api/collection.rb', line 125

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



16
17
18
19
# File 'lib/rtx/api/collection.rb', line 16

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

#dataObject

Returns all data associated with the existing response



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

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



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

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



22
23
24
25
# File 'lib/rtx/api/collection.rb', line 22

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

#firstObject

For moving to the first page of the collection



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

def first
  page(1)
  self
end

#get!(attrs = {}, file_type = 'json') ⇒ Object



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

def get!(attrs = {}, file_type = 'json')
  client.authenticate if !client.authenticated?
  get(symbolize_hash(attrs), file_type)
end

#has_next?Boolean

Responds true if the collection has another page ahead of it

Returns:

  • (Boolean)


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

def has_next?
  current_page < meta[:_total_pages]
end

#has_previous?Boolean

Responds true if the collection has a previous one

Returns:

  • (Boolean)


109
110
111
# File 'lib/rtx/api/collection.rb', line 109

def has_previous?
  current_page > 1
end

#lastObject

For moving to the last page of the collection



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

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

#metaObject

Returns the metadata about the current response



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

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



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

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

#page(num) ⇒ Object

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



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

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



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

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



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

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

#prevObject

For moving backward one page with the collection



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

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

#update!(attrs = {}) ⇒ Object

Updates a resource via PUT and returns it on success



28
29
30
31
# File 'lib/rtx/api/collection.rb', line 28

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