Class: RTX::API::Collection
- Inherits:
-
Object
- Object
- RTX::API::Collection
- Defined in:
- lib/rtx/api/collection.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
-
#options ⇒ Object
Returns the value of attribute options.
-
#resource_name ⇒ Object
Returns the value of attribute resource_name.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
-
#all_pages(initial_page = 1, &block) ⇒ Object
Allows you to loop through all of the pages and retrieve the records.
-
#all_resources(initial_page = 1, &block) ⇒ Object
Allows you to loop through all of the resources within the pages specified and retrieve the records.
-
#create!(attrs = {}) ⇒ Object
Creates a new resource via POST and returns it on success.
-
#data ⇒ Object
Returns all data associated with the existing response.
-
#delete!(attrs = {}) ⇒ Object
Deletes a resource via DELETE and returns true on success.
-
#detail!(attrs = {}) ⇒ Object
Gets an individual resource returns an object instead of an array.
-
#first ⇒ Object
For moving to the first page of the collection.
- #get!(attrs = {}, file_type = 'json') ⇒ Object
-
#has_next? ⇒ Boolean
Responds true if the collection has another page ahead of it.
-
#has_previous? ⇒ Boolean
Responds true if the collection has a previous one.
-
#initialize(client, resource_name, attrs = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#last ⇒ Object
For moving to the last page of the collection.
-
#meta ⇒ Object
Returns the metadata about the current response.
-
#next ⇒ Object
For moving forward one page with the collection.
-
#page(num) ⇒ Object
Chainable method that allows you to set the page number of the collection for your request.
-
#patch!(attrs = {}) ⇒ Object
Updates a resource via PATCH and returns it on success.
-
#per_page(num) ⇒ Object
Chainable method that allows you to set the per page number of the collection for your request.
-
#prev ⇒ Object
For moving backward one page with the collection.
-
#update!(attrs = {}) ⇒ Object
Updates a resource via PUT and returns it on success.
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
#client ⇒ Object
Returns the value of attribute client.
6 7 8 |
# File 'lib/rtx/api/collection.rb', line 6 def client @client end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/rtx/api/collection.rb', line 6 def @options end |
#resource_name ⇒ Object
Returns the value of attribute resource_name.
6 7 8 |
# File 'lib/rtx/api/collection.rb', line 6 def resource_name @resource_name end |
#response ⇒ Object
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..[:_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 |
#data ⇒ Object
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) || response 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 |
#first ⇒ Object
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
104 105 106 |
# File 'lib/rtx/api/collection.rb', line 104 def has_next? current_page < [:_total_pages] end |
#has_previous? ⇒ Boolean
Responds true if the collection has a previous one
109 110 111 |
# File 'lib/rtx/api/collection.rb', line 109 def has_previous? current_page > 1 end |
#last ⇒ Object
For moving to the last page of the collection
92 93 94 95 |
# File 'lib/rtx/api/collection.rb', line 92 def last page([:_total_pages]) self end |
#meta ⇒ Object
Returns the metadata about the current response
73 74 75 76 77 |
# File 'lib/rtx/api/collection.rb', line 73 def client.authenticate if !client.authenticated? collection if !has_response? response.reject { |key, _| key == :_embedded } end |
#next ⇒ Object
For moving forward one page with the collection
80 81 82 83 |
# File 'lib/rtx/api/collection.rb', line 80 def next page([: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 |
#prev ⇒ Object
For moving backward one page with the collection
86 87 88 89 |
# File 'lib/rtx/api/collection.rb', line 86 def prev page([: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 |