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) ⇒ Object



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

def create(doc)
  body = get_body(doc)
  @operations << {
    path:     @base_path,
    method:   "POST",
    body:     body,
    callback: block_given? && Proc.new
  }
end

#delete(doc) ⇒ Object



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

def delete(doc)
  id = get_id(doc)
  @operations << {
    path:     @base_path + "/" + CGI::escape(id),
    method:   "DELETE",
    callback: block_given? && Proc.new
  }
end

#get(doc) ⇒ Object



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

def get(doc)
  id = get_id(doc)
  @operations << {
    path:     @base_path + "/" + CGI::escape(id),
    method:   "GET",
    callback: block_given? && Proc.new
  }
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
# 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|
    callback = @operations[idx][:callback]
    status   = subresponse["status"]
    body     = subresponse["response"]
    method   = @operations[idx][:method]

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

#update(doc) ⇒ Object



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

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

#upsert(doc) ⇒ Object



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

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