Class: Intercom::BaseCollectionProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/intercom/base_collection_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_name, resource_class, details: {}, client:, method: 'get') ⇒ BaseCollectionProxy

Returns a new instance of BaseCollectionProxy.



9
10
11
12
13
14
15
16
# File 'lib/intercom/base_collection_proxy.rb', line 9

def initialize(resource_name, resource_class, details: {}, client:, method: 'get')
  @resource_name = resource_name
  @resource_class = resource_class
  @url = (details[:url] || "/#{@resource_name}")
  @params = (details[:params] || {})
  @client = client
  @method = method
end

Instance Attribute Details

#resource_classObject (readonly)

Returns the value of attribute resource_class.



7
8
9
# File 'lib/intercom/base_collection_proxy.rb', line 7

def resource_class
  @resource_class
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name.



7
8
9
# File 'lib/intercom/base_collection_proxy.rb', line 7

def resource_name
  @resource_name
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/intercom/base_collection_proxy.rb', line 7

def url
  @url
end

Instance Method Details

#[](target_index) ⇒ Object



29
30
31
32
33
34
# File 'lib/intercom/base_collection_proxy.rb', line 29

def [](target_index)
  each_with_index do |item, index|
    return item if index == target_index
  end
  nil
end

#each(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/intercom/base_collection_proxy.rb', line 18

def each(&block)
  loop do
    response_hash = @client.public_send(@method, @url, payload)
    raise Intercom::HttpError, 'Http Error - No response entity returned' unless response_hash

    deserialize_response_hash(response_hash, block)
    break unless has_next_link?(response_hash)
  end
  self
end