Class: Intercom::ScrollCollectionProxy

Inherits:
BaseCollectionProxy show all
Defined in:
lib/intercom/scroll_collection_proxy.rb

Instance Attribute Summary collapse

Attributes inherited from BaseCollectionProxy

#resource_class, #resource_name, #url

Instance Method Summary collapse

Methods inherited from BaseCollectionProxy

#[]

Constructor Details

#initialize(resource_name, resource_class, details: {}, client:) ⇒ ScrollCollectionProxy

Returns a new instance of ScrollCollectionProxy.



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

def initialize(resource_name, resource_class, details: {}, client:)
  @resource_name = resource_name
  @resource_class = resource_class
  @scroll_url = (details[:url] || "/#{@resource_name}") + '/scroll'
  @client = client
end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records.



8
9
10
# File 'lib/intercom/scroll_collection_proxy.rb', line 8

def records
  @records
end

#scroll_paramObject (readonly)

Returns the value of attribute scroll_param.



8
9
10
# File 'lib/intercom/scroll_collection_proxy.rb', line 8

def scroll_param
  @scroll_param
end

#scroll_urlObject (readonly)

Returns the value of attribute scroll_url.



8
9
10
# File 'lib/intercom/scroll_collection_proxy.rb', line 8

def scroll_url
  @scroll_url
end

Instance Method Details

#each(&block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/intercom/scroll_collection_proxy.rb', line 37

def each(&block)
  scroll_param = nil
  loop do
    response_hash = if !scroll_param
                      @client.get(@scroll_url, '')
                    else
                      @client.get(@scroll_url, scroll_param: scroll_param)
                    end
    raise Intercom::HttpError, 'Http Error - No response entity returned' unless response_hash

    top_level_entity_key = entity_key_from_response(response_hash)
    response_hash[top_level_entity_key].each do |object_json|
      block.call Lib::TypedJsonDeserializer.new(object_json, @client).deserialize
    end
    scroll_param = extract_scroll_param(response_hash)
    break unless records_present?(response_hash)
  end
  self
end

#next(scroll_parameter = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/intercom/scroll_collection_proxy.rb', line 17

def next(scroll_parameter = nil)
  @records = []
  response_hash = if !scroll_parameter
                    # First time so do initial get without scroll_param
                    @client.get(@scroll_url, '')
                  else
                    # Not first call so use get next page
                    @client.get(@scroll_url, scroll_param: scroll_parameter)
                  end
  raise Intercom::HttpError, 'Http Error - No response entity returned' unless response_hash

  @scroll_param = extract_scroll_param(response_hash)
  top_level_entity_key = entity_key_from_response(response_hash)
  response_hash[top_level_entity_key] = response_hash[top_level_entity_key].map do |object_json|
    Lib::TypedJsonDeserializer.new(object_json, @client).deserialize
  end
  @records = response_hash[@resource_name]
  self
end