class RTX::API::CollectionV2

Attributes

client[RW]
options[RW]
resource_name[RW]
response[RW]

Public Class Methods

new(client, resource_name, attrs = {}) click to toggle source
# 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

Public Instance Methods

all_pages(&block) click to toggle source

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

# 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) click to toggle source

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

# 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
data() click to toggle source

Returns all data associated with the existing response

# 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?() click to toggle source

Responds true if the collection has another page ahead of it

# File lib/rtx/api/collection_v2.rb, line 50
def has_next?
  !after_token.nil?
end
has_previous?() click to toggle source

Responds true if the collection has a previous one

# File lib/rtx/api/collection_v2.rb, line 55
def has_previous?
  !before_token.nil?
end
next() click to toggle source

For moving forward one page with the collection

# File lib/rtx/api/collection_v2.rb, line 38
def next
  next_page(after_token) if has_next?
  self
end
paging() click to toggle source

Returns the paging information about the current response

# 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) click to toggle source

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

# File lib/rtx/api/collection_v2.rb, line 17
def per_page(num)
  clear if !num.nil?
  @options[:per_page] = num
  self
end
prev() click to toggle source

For moving backward one page with the collection

# File lib/rtx/api/collection_v2.rb, line 44
def prev
  previous_page(before_token) if has_previous?
  self
end

Protected Instance Methods

after_token() click to toggle source
# File lib/rtx/api/collection_v2.rb, line 99
def after_token
  return nil if paging.nil?
  return nil if paging[:cursors].nil?

  paging[:cursors][:after]
end
before_token() click to toggle source
# File lib/rtx/api/collection_v2.rb, line 106
def before_token
  return nil if paging.nil?
  return nil if paging[:cursors].nil?

  paging[:cursors][:before]
end
clear() click to toggle source
# File lib/rtx/api/collection_v2.rb, line 113
def clear
  @response = {}
end
collection() click to toggle source
# File lib/rtx/api/collection_v2.rb, line 117
def collection
  @response = client.collection(resource_name, options)
end
has_response?() click to toggle source
# File lib/rtx/api/collection_v2.rb, line 121
def has_response?
  response != {}
end
next_page(after_token) click to toggle source

Chainable method that allows you to get the next page for a given after token

# File lib/rtx/api/collection_v2.rb, line 86
def next_page(after_token)
  clear
  @options[:after] = after_token
  self
end
previous_page(before_token) click to toggle source

Chainable method that allows you to get the previous page for a given before token

# File lib/rtx/api/collection_v2.rb, line 93
def previous_page(before_token)
  clear
  @options[:before] = before_token
  self
end
symbolize_hash(hash) click to toggle source
# File lib/rtx/api/collection_v2.rb, line 125
def symbolize_hash(hash)
  Hash[hash.map { |(key, value)| [key.to_sym, value] }]
end