Class: Intercom::CollectionProxy
- Inherits:
-
Object
- Object
- Intercom::CollectionProxy
- Includes:
- Enumerable
- Defined in:
- lib/intercom/collection_proxy.rb
Instance Attribute Summary collapse
-
#resource_name ⇒ Object
readonly
Returns the value of attribute resource_name.
Instance Method Summary collapse
- #[](target_index) ⇒ Object
- #count ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(resource_name, finder_details = {}) ⇒ CollectionProxy
constructor
A new instance of CollectionProxy.
Constructor Details
#initialize(resource_name, finder_details = {}) ⇒ CollectionProxy
Returns a new instance of CollectionProxy.
9 10 11 12 13 14 |
# File 'lib/intercom/collection_proxy.rb', line 9 def initialize(resource_name, finder_details = {}) @resource_name = resource_name @resource_class = Utils.constantize_resource_name(resource_name) @finder_url = (finder_details[:url] || "/#{@resource_name}") @finder_params = (finder_details[:params] || {}) end |
Instance Attribute Details
#resource_name ⇒ Object (readonly)
Returns the value of attribute resource_name.
7 8 9 |
# File 'lib/intercom/collection_proxy.rb', line 7 def resource_name @resource_name end |
Instance Method Details
#[](target_index) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/intercom/collection_proxy.rb', line 32 def [](target_index) self.each_with_index do |item, index| return item if index == target_index end nil end |
#count ⇒ Object
41 42 43 |
# File 'lib/intercom/collection_proxy.rb', line 41 def count raise NoMethodError, "undefined method `count' for #{self.class}. Consider using the dedicated Intercom::Count interface if suitable" end |
#each(&block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/intercom/collection_proxy.rb', line 16 def each(&block) next_page = nil loop do if next_page response_hash = Intercom.get(next_page, {}) else response_hash = Intercom.get(@finder_url, @finder_params) end raise Intercom::HttpError.new('Http Error - No response entity returned') unless response_hash deserialize_response_hash(response_hash, block) next_page = extract_next_link(response_hash) break if next_page.nil? end self end |