Class: Microsoft::Graph::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/microsoft/graph/batch.rb

Defined Under Namespace

Classes: Request, Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(graph, token:, size: 20) ⇒ Batch

Returns a new instance of Batch.



8
9
10
11
12
13
# File 'lib/microsoft/graph/batch.rb', line 8

def initialize(graph, token:, size: 20)
  @graph = graph
  @requests = []
  @token = token
  @size = size
end

Instance Attribute Details

#requestsObject (readonly)

Returns the value of attribute requests.



6
7
8
# File 'lib/microsoft/graph/batch.rb', line 6

def requests
  @requests
end

Instance Method Details

#add(endpoint, id: SecureRandom.uuid, method: "GET", headers: {}, params: nil, body: nil, depends_on: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/microsoft/graph/batch.rb', line 15

def add(endpoint, id: SecureRandom.uuid, method: "GET", headers: {}, params: nil, body: nil, depends_on: nil)
  @requests << Request.new(
    endpoint,
    id: id,
    method: method,
    headers: headers,
    params: params,
    body: body,
    depends_on: depends_on
  )
end

#callObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/microsoft/graph/batch.rb', line 27

def call
  @requests.each_slice(@size).flat_map do |group|
    requests_by_id = group.group_by(&:id).transform_values(&:first)
    group.first.depends_on = nil

    response = @graph.call("/$batch", method: "POST", token: @token, body: { requests: group.map(&:to_h) })
    response.responses.map do |current|
      Result.new(request: requests_by_id[current.id], response: current)
    end
  end
end