Class: Arango::Batch

Inherits:
Object
  • Object
show all
Includes:
Helper_Error, Helper_Return, Server_Return
Defined in:
lib/Batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Server_Return

#server=

Methods included from Helper_Return

#return_delete, #return_directly?, #return_element

Methods included from Helper_Error

#satisfy_category?, #satisfy_class?, #warning_deprecated

Constructor Details

#initialize(server:, boundary: "XboundaryX", queries: []) ⇒ Batch

Returns a new instance of Batch.



7
8
9
10
11
12
13
14
15
16
# File 'lib/Batch.rb', line 7

def initialize(server:, boundary: "XboundaryX", queries: [])
  @id = 1
  assign_server(server)
  assign_queries(queries)
  @headers = {
    "Content-Type": "multipart/form-data",
    "boundary":     boundary
  }
  @boundary = boundary
end

Instance Attribute Details

#boundaryObject

DEFINE ===



20
21
22
# File 'lib/Batch.rb', line 20

def boundary
  @boundary
end

#queriesObject

DEFINE ===



20
21
22
# File 'lib/Batch.rb', line 20

def queries
  @queries
end

#serverObject (readonly)

DEFINE ===



20
21
22
# File 'lib/Batch.rb', line 20

def server
  @server
end

Instance Method Details

#addQuery(id: @id, method:, address:, body: nil) ⇒ Object Also known as: modifyQuery

QUERY ===



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/Batch.rb', line 62

def addQuery(id: @id, method:, address:, body: nil)
  id = id.to_s
  @queries[id] = {
    "id":      id,
    "method":  method,
    "address": address,
    "body":    body
  }.delete_if{|k,v| v.nil?}
  @id += 1
  return @queries
end

#executeObject

EXECUTE ===



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/Batch.rb', line 82

def execute
  body = ""
  @queries.each do |id, query|
    body += "--#{@boundary}\n"
    body += "Content-Type: application/x-arango-batchpart\n"
    body += "Content-Id: #{query[:id]}\n\n"
    body += "#{query[:method]} "
    body += "#{query[:address]} HTTP/1.1\n"
    body += "\n#{Oj.dump(query[:body])}\n" unless query[:body].nil?
  end
  body += "--#{@boundary}--\n" if @queries.length > 0
  @server.request("POST", "_api/batch", body: body, skip_to_json: true,
    headers: @headers, skip_parsing: true)
end

#removeQuery(id:) ⇒ Object



75
76
77
78
# File 'lib/Batch.rb', line 75

def removeQuery(id:)
  @queries.delete(id)
  return @queries
end

#to_hObject

TO HASH ===



52
53
54
55
56
57
58
# File 'lib/Batch.rb', line 52

def to_h
  {
    "boundary": @boundary,
    "queries":  @queries,
    "database": @database.name
  }.delete_if{|k,v| v.nil?}
end