Class: Intercom::ScrollCollectionProxy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ScrollCollectionProxy.


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

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

end

Instance Attribute Details

#recordsObject (readonly)

Returns the value of attribute records


6
7
8
# File 'lib/intercom/scroll_collection_proxy.rb', line 6

def records
  @records
end

#resource_classObject (readonly)

Returns the value of attribute resource_class


6
7
8
# File 'lib/intercom/scroll_collection_proxy.rb', line 6

def resource_class
  @resource_class
end

#resource_nameObject (readonly)

Returns the value of attribute resource_name


6
7
8
# File 'lib/intercom/scroll_collection_proxy.rb', line 6

def resource_name
  @resource_name
end

#scroll_paramObject (readonly)

Returns the value of attribute scroll_param


6
7
8
# File 'lib/intercom/scroll_collection_proxy.rb', line 6

def scroll_param
  @scroll_param
end

#scroll_urlObject (readonly)

Returns the value of attribute scroll_url


6
7
8
# File 'lib/intercom/scroll_collection_proxy.rb', line 6

def scroll_url
  @scroll_url
end

Instance Method Details

#[](target_index) ⇒ Object


53
54
55
56
57
58
# File 'lib/intercom/scroll_collection_proxy.rb', line 53

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

#each(&block) ⇒ Object


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

def each(&block)
  scroll_param = nil
  loop do
    if not scroll_param
      response_hash = @client.get(@scroll_url, '')
    else
      response_hash = @client.get(@scroll_url, scroll_param: scroll_param)
    end
    raise Intercom::HttpError.new('Http Error - No response entity returned') unless response_hash
    response_hash[deserialize_response_hash(response_hash)].each do |object_json|
      block.call Lib::TypedJsonDeserializer.new(object_json).deserialize
    end
    scroll_param = extract_scroll_param(response_hash)
    break if not records_present?(response_hash)
  end
  self
end

#next(scroll_parameter = nil) ⇒ Object


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

def next(scroll_parameter=nil)
  @records = []
  if not scroll_parameter    #First time so do initial get without scroll_param

    response_hash = @client.get(@scroll_url, '')
  else
    #Not first call so use get next page
    response_hash = @client.get(@scroll_url, scroll_param: scroll_parameter)
  end
  raise Intercom::HttpError.new('Http Error - No response entity returned') unless response_hash
  @scroll_param = extract_scroll_param(response_hash)
  top_level_entity_key = deserialize_response_hash(response_hash)
  response_hash[top_level_entity_key] = response_hash[top_level_entity_key].map do |object_json|
    Lib::TypedJsonDeserializer.new(object_json).deserialize
  end
  @records = response_hash[@resource_name]
  self
end