class RTX::API::Collection

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.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(initial_page = 1, &block) click to toggle source

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

# 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) 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.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 = {}) click to toggle source

Creates a new resource via POST and returns it on success

# File lib/rtx/api/collection.rb, line 16
def create!(attrs = {})
  client.authenticate if !client.authenticated?
  post(symbolize_hash(attrs))
end
data() click to toggle source

Returns all data associated with the existing response

# File lib/rtx/api/collection.rb, line 66
def data
  client.authenticate if !client.authenticated?
  collection if !has_response?
  response[:_embedded]&.[](resource_name) || response
end
delete!(attrs = {}) click to toggle source

Deletes a resource via DELETE and returns true on success

# File lib/rtx/api/collection.rb, line 40
def delete!(attrs = {})
  client.authenticate if !client.authenticated?
  delete(symbolize_hash(attrs))
end
detail!(attrs = {}) click to toggle source

Gets an individual resource returns an object instead of an array

# File lib/rtx/api/collection.rb, line 22
def detail!(attrs = {})
  client.authenticate if !client.authenticated?
  detail(symbolize_hash(attrs))
end
first() click to toggle source

For moving to the first page of the collection

# File lib/rtx/api/collection.rb, line 98
def first
  page(1)
  self
end
get!(attrs = {}, file_type = 'json') click to toggle source
# 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?() click to toggle source

Responds true if the collection has another page ahead of it

# File lib/rtx/api/collection.rb, line 104
def has_next?
  current_page < meta[:_total_pages]
end
has_previous?() click to toggle source

Responds true if the collection has a previous one

# File lib/rtx/api/collection.rb, line 109
def has_previous?
  current_page > 1
end
last() click to toggle source

For moving to the last page of the collection

# File lib/rtx/api/collection.rb, line 92
def last
  page(meta[:_total_pages])
  self
end
meta() click to toggle source

Returns the metadata about the current response

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

For moving forward one page with the collection

# File lib/rtx/api/collection.rb, line 80
def next
  page(options[:page] += 1) if has_next?
  self
end
page(num) click to toggle source

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

# File lib/rtx/api/collection.rb, line 59
def page(num)
  clear if !num.nil?
  @options[:page] = num
  self
end
patch!(attrs = {}) click to toggle source

Updates a resource via PATCH and returns it on success

# File lib/rtx/api/collection.rb, line 34
def patch!(attrs = {})
  client.authenticate if !client.authenticated?
  patch(symbolize_hash(attrs))
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.rb, line 52
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.rb, line 86
def prev
  page(options[:page] -= 1) if has_previous?
  self
end
update!(attrs = {}) click to toggle source

Updates a resource via PUT and returns it on success

# File lib/rtx/api/collection.rb, line 28
def update!(attrs = {})
  client.authenticate if !client.authenticated?
  put(symbolize_hash(attrs))
end

Protected Instance Methods

clear() click to toggle source
# File lib/rtx/api/collection.rb, line 135
def clear
  @response = {}
end
collection() click to toggle source
# File lib/rtx/api/collection.rb, line 144
def collection
  @response = client.collection(resource_name, options)
end
current_page() click to toggle source
# File lib/rtx/api/collection.rb, line 139
def current_page
  @options[:page] = options[:page].nil? ? meta[:_page] : options[:page]
  options[:page]
end
delete(attrs = {}) click to toggle source
# File lib/rtx/api/collection.rb, line 167
def delete(attrs = {})
  resource_id = attrs[:id]
  client.delete(resource_name, resource_id)
end
detail(attrs = {}) click to toggle source
# File lib/rtx/api/collection.rb, line 148
def detail(attrs = {})
  resource_id = attrs[:id]
  client.detail(resource_name, resource_id, attrs)
end
get(attrs = {}, file_type = 'json') click to toggle source
# File lib/rtx/api/collection.rb, line 172
def get(attrs = {}, file_type = 'json')
  client.get(resource_name, attrs, file_type)
end
has_response?() click to toggle source
# File lib/rtx/api/collection.rb, line 176
def has_response?
  response != {}
end
patch(attrs = {}) click to toggle source
# File lib/rtx/api/collection.rb, line 162
def patch(attrs = {})
  resource_id = attrs[:id]
  client.patch(resource_name, resource_id, attrs)
end
post(attrs = {}) click to toggle source
# File lib/rtx/api/collection.rb, line 153
def post(attrs = {})
  client.post(resource_name, attrs)
end
put(attrs = {}) click to toggle source
# File lib/rtx/api/collection.rb, line 157
def put(attrs = {})
  resource_id = attrs[:id]
  client.put(resource_name, resource_id, attrs)
end
symbolize_hash(hash) click to toggle source
# File lib/rtx/api/collection.rb, line 180
def symbolize_hash(hash)
  Hash[hash.map { |(key, value)| [key.to_sym, value] }]
end