Module: Eson::HTTP::Bulk

Extended by:
API
Includes:
Shared::Bulk
Defined in:
lib/eson/http/core/bulk.rb

Constant Summary collapse

FIELD_MAPPING =
[:index, :type, :id, :parent, :ttl, :timestamp, :routing].map do |field|
  [field, "_#{field}"]
end

Instance Method Summary collapse

Methods included from API

request_method

Instance Method Details

#pathObject



13
14
15
# File 'lib/eson/http/core/bulk.rb', line 13

def path
  '/_bulk'
end

#serialize_request(request) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/eson/http/core/bulk.rb', line 21

def serialize_request(request)
  case request
  when Eson::HTTP::Index
    MultiJson.encode({ "index" => to_params_hash(request) }) << "\n" << request.source << "\n"
  when Eson::HTTP::Delete
    MultiJson.encode({ "delete" => to_params_hash(request) }) << "\n"
  else
    warn("Unserializable request #{request.inspect}")
  end
end

#sourceObject



17
18
19
# File 'lib/eson/http/core/bulk.rb', line 17

def source
  bulk.map {|r| serialize_request(r)}.join
end

#to_params_hash(r) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/eson/http/core/bulk.rb', line 32

def to_params_hash(r)
  FIELD_MAPPING.inject({}) do |params, (field, serialized_field)|
    if r.respond_to?(field) && val = r.send(field)
      params[serialized_field] = val
    end
    params
  end
end