class RTX::API::Collection
Attributes
Public Class Methods
# 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
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
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
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
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
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
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
For moving to the first page of the collection
# File lib/rtx/api/collection.rb, line 98 def first page(1) self end
# 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
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
Responds true if the collection has a previous one
# File lib/rtx/api/collection.rb, line 109 def has_previous? current_page > 1 end
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
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
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
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
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
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
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
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
# File lib/rtx/api/collection.rb, line 135 def clear @response = {} end
# File lib/rtx/api/collection.rb, line 144 def collection @response = client.collection(resource_name, options) end
# File lib/rtx/api/collection.rb, line 139 def current_page @options[:page] = options[:page].nil? ? meta[:_page] : options[:page] options[:page] end
# File lib/rtx/api/collection.rb, line 167 def delete(attrs = {}) resource_id = attrs[:id] client.delete(resource_name, resource_id) end
# File lib/rtx/api/collection.rb, line 148 def detail(attrs = {}) resource_id = attrs[:id] client.detail(resource_name, resource_id, attrs) end
# File lib/rtx/api/collection.rb, line 172 def get(attrs = {}, file_type = 'json') client.get(resource_name, attrs, file_type) end
# File lib/rtx/api/collection.rb, line 176 def has_response? response != {} end
# File lib/rtx/api/collection.rb, line 162 def patch(attrs = {}) resource_id = attrs[:id] client.patch(resource_name, resource_id, attrs) end
# File lib/rtx/api/collection.rb, line 153 def post(attrs = {}) client.post(resource_name, attrs) end
# File lib/rtx/api/collection.rb, line 157 def put(attrs = {}) resource_id = attrs[:id] client.put(resource_name, resource_id, attrs) end
# File lib/rtx/api/collection.rb, line 180 def symbolize_hash(hash) Hash[hash.map { |(key, value)| [key.to_sym, value] }] end