Class: Ecoportal::API::Common::BatchOperation
- Inherits:
-
Object
- Object
- Ecoportal::API::Common::BatchOperation
show all
- Includes:
- DocHelpers
- Defined in:
- lib/ecoportal/api/common/batch_operation.rb
Instance Method Summary
collapse
Methods included from DocHelpers
#get_body, #get_external_id, #get_id
Constructor Details
#initialize(base_path, wrapper) ⇒ BatchOperation
Returns a new instance of BatchOperation.
8
9
10
11
12
|
# File 'lib/ecoportal/api/common/batch_operation.rb', line 8
def initialize(base_path, wrapper)
@base_path = base_path
@wrapper = wrapper
@operations = []
end
|
Instance Method Details
#as_json ⇒ Object
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
83
84
85
86
87
88
89
90
91
|
# File 'lib/ecoportal/api/common/batch_operation.rb', line 83
def create(doc)
body = get_body(doc)
@operations << {
path: @base_path,
method: "POST",
body: body,
callback: block_given? && Proc.new
}
end
|
#delete(doc) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/ecoportal/api/common/batch_operation.rb', line 74
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
43
44
45
46
47
48
49
50
|
# File 'lib/ecoportal/api/common/batch_operation.rb', line 43
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
|
# File 'lib/ecoportal/api/common/batch_operation.rb', line 22
def process_response(response)
if response.success?
response.body.each.with_index do |subresponse, idx|
next unless callback = @operations[idx][:callback]
status = subresponse["status"]
body = subresponse["response"]
method = @operations[idx][:method]
batch_response = BatchResponse.new(status, body)
if batch_response.success? && method == "GET"
callback.call batch_response, @wrapper.new(body)
else
callback.call batch_response
end
end
else
raise "Total failure in batch operation"
end
end
|
#update(doc) ⇒ Object
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/ecoportal/api/common/batch_operation.rb', line 52
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
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/ecoportal/api/common/batch_operation.rb', line 63
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
|