Class: Dyte::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/dyte/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, total_count: nil, start_offset: nil, end_offset: nil) ⇒ Collection

Returns a new instance of Collection.



25
26
27
28
29
30
# File 'lib/dyte/collection.rb', line 25

def initialize(data:, total_count: nil, start_offset: nil, end_offset: nil)
  @data = data
  @total_count = total_count
  @start_offset = start_offset
  @end_offset = end_offset
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/dyte/collection.rb', line 3

def data
  @data
end

#end_offsetObject (readonly)

Returns the value of attribute end_offset.



3
4
5
# File 'lib/dyte/collection.rb', line 3

def end_offset
  @end_offset
end

#start_offsetObject (readonly)

Returns the value of attribute start_offset.



3
4
5
# File 'lib/dyte/collection.rb', line 3

def start_offset
  @start_offset
end

#total_countObject (readonly)

Returns the value of attribute total_count.



3
4
5
# File 'lib/dyte/collection.rb', line 3

def total_count
  @total_count
end

Class Method Details

.from_response(response, type:, key: nil) ⇒ Object

from_response(response, type: Meeting)



6
7
8
9
10
11
12
13
14
# File 'lib/dyte/collection.rb', line 6

def self.from_response(response, type:, key: nil)
  body = response.body
  new(
    data: parse_body(body: body, type: type, key: key),
    total_count: body.dig("paging", "total_count"),
    start_offset: body.dig("paging", "start_offset"),
    end_offset: body.dig("paging", "end_offset")
  )
end

.from_sessions_response(response, type:) ⇒ Object

As of now the session response differs from the other responses. It’s missing pagination data and also the data is nested under ‘sessions’.



18
19
20
21
22
23
# File 'lib/dyte/collection.rb', line 18

def self.from_sessions_response(response, type:)
  body = response.body
  new(
    data: body.dig("data", "sessions").map { |attrs| type.new(attrs) }
  )
end

.parse_body(body:, type:, key: nil) ⇒ Object

The Dyte API does not have a consistent response structure. This method accounts for that.



33
34
35
36
37
38
39
# File 'lib/dyte/collection.rb', line 33

def self.parse_body(body:, type:, key: nil)
  if body["data"].is_a?(Array)
    body["data"].map { |attrs| type.new(attrs) }
  else
    body.dig("data", key).map { |attrs| type.new(attrs) }
  end
end