Class: RTX::API::CollectionV2

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CollectionV2.



8
9
10
11
12
13
# File 'lib/rtx/api/collection_v2.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_v2.rb', line 6

def client
  @client
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#resource_nameObject

Returns the value of attribute resource_name.



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

def resource_name
  @resource_name
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

Instance Method Details

#all_pages(&block) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rtx/api/collection_v2.rb', line 60

def all_pages(&block)
  loop do
    # Return first page
    block.call(data)

    # No need to continue if all data is retrieved
    break unless has_next?

    # Navigate to the next page
    self.next
  end
end

#all_resources(&block) ⇒ Object

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



75
76
77
78
79
80
81
# File 'lib/rtx/api/collection_v2.rb', line 75

def all_resources(&block)
  all_pages do |page|
    page.each do |resource|
      block.call(resource)
    end
  end
end

#dataObject

Returns all data associated with the existing response



24
25
26
27
28
# File 'lib/rtx/api/collection_v2.rb', line 24

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

#has_next?Boolean

Responds true if the collection has another page ahead of it

Returns:

  • (Boolean)


50
51
52
# File 'lib/rtx/api/collection_v2.rb', line 50

def has_next?
  !after_token.nil?
end

#has_previous?Boolean

Responds true if the collection has a previous one

Returns:

  • (Boolean)


55
56
57
# File 'lib/rtx/api/collection_v2.rb', line 55

def has_previous?
  !before_token.nil?
end

#nextObject

For moving forward one page with the collection



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

def next
  next_page(after_token) if has_next?
  self
end

#pagingObject

Returns the paging information about the current response



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

def paging
  client.authenticate if !client.authenticated?
  collection if !has_response?
  response[:paging]
end

#per_page(num) ⇒ Object

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



17
18
19
20
21
# File 'lib/rtx/api/collection_v2.rb', line 17

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

#prevObject

For moving backward one page with the collection



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

def prev
  previous_page(before_token) if has_previous?
  self
end