Class: Ecoportal::API::Common::BatchOperation

Inherits:
Object
  • Object
show all
Includes:
DocHelpers
Defined in:
lib/ecoportal/api/common/batch_operation.rb

Instance Method Summary collapse

Methods included from DocHelpers

#get_body, #get_id

Constructor Details

#initialize(base_path, wrapper, logger: nil) ⇒ BatchOperation

Returns a new instance of BatchOperation.



7
8
9
10
11
12
# File 'lib/ecoportal/api/common/batch_operation.rb', line 7

def initialize(base_path, wrapper, logger: nil)
  @base_path  = base_path
  @wrapper    = wrapper
  @operations = []
  @logger     = logger
end

Instance Method Details

#as_jsonObject



14
15
16
17
18
19
20
# File 'lib/ecoportal/api/common/batch_operation.rb', line 14

def as_json
  {
    actions: @operations.map do |operation|
      operation.slice(:path, :method, :body)
    end
  }
end

#create(doc, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/ecoportal/api/common/batch_operation.rb', line 90

def create(doc, &block)
  body = get_body(doc)
  @operations << {
    path:     @base_path,
    method:   "POST",
    body:     body,
    callback: block
  }
end

#delete(doc, &block) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/ecoportal/api/common/batch_operation.rb', line 81

def delete(doc, &block)
  id = get_id(doc)
  @operations << {
    path:     "#{@base_path}/#{CGI.escape(id)}",
    method:   "DELETE",
    callback: block
  }
end

#get(doc, &block) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/ecoportal/api/common/batch_operation.rb', line 50

def get(doc, &block)
  id = get_id(doc)
  @operations << {
    path:     "#{@base_path}/#{CGI.escape(id)}",
    method:   "GET",
    callback: block
  }
end

#process_response(response) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ecoportal/api/common/batch_operation.rb', line 22

def process_response(response)
  unless response.success?
    log(:error) { "Total failure in batch operation." }
    raise "Total failure in batch operation"
  end

  log(:info) { "Processing batch responses" }

  body_data(response.body).each.with_index do |subresponse, idx|
    status   = subresponse["status"]
    method   = @operations[idx][:method]
    body     = subresponse["response"]
    callback = @operations[idx][:callback]

    if status == 200 && method == "GET"
      batch_response = BatchResponse.new(status, body, @wrapper.new(body))
      log_batch_response(@operations[idx], batch_response)

      callback&.call(batch_response, batch_response.result)
    else
      batch_response = BatchResponse.new(status, body)
      log_batch_response(@operations[idx], batch_response)

      callback&.call(batch_response)
    end
  end
end

#update(doc, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/ecoportal/api/common/batch_operation.rb', line 59

def update(doc, &block)
  id   = get_id(doc)
  body = get_body(doc)
  @operations << {
    path:     "#{@base_path}/#{CGI.escape(id)}",
    method:   "PATCH",
    body:     body,
    callback: block
  }
end

#upsert(doc, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/ecoportal/api/common/batch_operation.rb', line 70

def upsert(doc, &block)
  id   = get_id(doc)
  body = get_body(doc)
  @operations << {
    path:     "#{@base_path}/#{CGI.escape(id)}",
    method:   "POST",
    body:     body,
    callback: block
  }
end