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.
-
#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
permalink #initialize(client, resource_name, attrs = {}) ⇒ Collection
Returns a new instance of Collection.
6 7 8 9 10 11 |
# File 'lib/rtx/api/collection.rb', line 6 def initialize(client, resource_name, attrs = {}) @client = client @resource_name = resource_name.to_sym @options = symbolize_hash(attrs) @response = {} end |
Instance Attribute Details
permalink #client ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/rtx/api/collection.rb', line 4 def client @client end |
permalink #options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/rtx/api/collection.rb', line 4 def @options end |
permalink #resource_name ⇒ Object
Returns the value of attribute resource_name.
4 5 6 |
# File 'lib/rtx/api/collection.rb', line 4 def resource_name @resource_name end |
permalink #response ⇒ Object
Returns the value of attribute response.
4 5 6 |
# File 'lib/rtx/api/collection.rb', line 4 def response @response end |
Instance Method Details
permalink #all_pages(initial_page = 1, &block) ⇒ Object
Allows you to loop through all of the pages and retrieve the records
110 111 112 113 114 115 116 117 |
# File 'lib/rtx/api/collection.rb', line 110 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 |
permalink #all_resources(initial_page = 1, &block) ⇒ Object
Allows you to loop through all of the resources within the pages specified and retrieve the records
120 121 122 123 124 125 126 |
# File 'lib/rtx/api/collection.rb', line 120 def all_resources(initial_page = 1, &block) all_pages(initial_page) do |page| page.each do |resource| block.call(resource) end end end |
permalink #create!(attrs = {}) ⇒ Object
Creates a new resource via POST and returns it on success
14 15 16 17 |
# File 'lib/rtx/api/collection.rb', line 14 def create!(attrs = {}) client.authenticate if !client.authenticated? post(symbolize_hash(attrs)) end |
permalink #data ⇒ Object
Returns all data associated with the existing response
58 59 60 61 62 |
# File 'lib/rtx/api/collection.rb', line 58 def data client.authenticate if !client.authenticated? collection if !has_response? response[:_embedded][resource_name] end |
permalink #delete!(attrs = {}) ⇒ Object
Deletes a resource via DELETE and returns true on success
38 39 40 41 |
# File 'lib/rtx/api/collection.rb', line 38 def delete!(attrs = {}) client.authenticate if !client.authenticated? delete(symbolize_hash(attrs)) end |
permalink #detail!(attrs = {}) ⇒ Object
Gets an individual resource returns an object instead of an array
20 21 22 23 |
# File 'lib/rtx/api/collection.rb', line 20 def detail!(attrs = {}) client.authenticate if !client.authenticated? detail(symbolize_hash(attrs)) end |
permalink #first ⇒ Object
For moving to the first page of the collection
94 95 96 97 |
# File 'lib/rtx/api/collection.rb', line 94 def first page(1) self end |
permalink #has_next? ⇒ Boolean
Responds true if the collection has another page ahead of it
100 101 102 |
# File 'lib/rtx/api/collection.rb', line 100 def has_next? current_page < [:_total_pages] end |
permalink #has_previous? ⇒ Boolean
Responds true if the collection has a previous one
105 106 107 |
# File 'lib/rtx/api/collection.rb', line 105 def has_previous? current_page > 1 end |
permalink #last ⇒ Object
For moving to the last page of the collection
88 89 90 91 |
# File 'lib/rtx/api/collection.rb', line 88 def last page([:_total_pages]) self end |
permalink #meta ⇒ Object
Returns the metadata about the current response
65 66 67 68 69 |
# File 'lib/rtx/api/collection.rb', line 65 def client.authenticate if !client.authenticated? collection if !has_response? response.reject {|key,_| key == :_embedded} end |
permalink #next ⇒ Object
For moving forward one page with the collection
72 73 74 75 76 77 |
# File 'lib/rtx/api/collection.rb', line 72 def next if has_next? page([:page] += 1) end self end |
permalink #page(num) ⇒ Object
Chainable method that allows you to set the page number of the collection for your request
51 52 53 54 55 |
# File 'lib/rtx/api/collection.rb', line 51 def page(num) clear if !num.nil? @options[:page] = num self end |
permalink #patch!(attrs = {}) ⇒ Object
Updates a resource via PATCH and returns it on success
32 33 34 35 |
# File 'lib/rtx/api/collection.rb', line 32 def patch!(attrs = {}) client.authenticate if !client.authenticated? patch(symbolize_hash(attrs)) end |
permalink #per_page(num) ⇒ Object
Chainable method that allows you to set the per page number of the collection for your request
44 45 46 47 48 |
# File 'lib/rtx/api/collection.rb', line 44 def per_page(num) clear if !num.nil? @options[:per_page] = num self end |
permalink #prev ⇒ Object
For moving backward one page with the collection
80 81 82 83 84 85 |
# File 'lib/rtx/api/collection.rb', line 80 def prev if has_previous? page([:page] -= 1) end self end |
permalink #update!(attrs = {}) ⇒ Object
Updates a resource via PUT and returns it on success
26 27 28 29 |
# File 'lib/rtx/api/collection.rb', line 26 def update!(attrs = {}) client.authenticate if !client.authenticated? put(symbolize_hash(attrs)) end |