Module: LogStash::Api::AppHelpers

Defined in:
lib/logstash/api/app_helpers.rb

Instance Method Summary collapse

Instance Method Details

#respond_with(data, options = {}) ⇒ Object

This method handle both of the normal flow happy path and the display or errors, if more custom logic is added here it will make sense to separate them.

See #error method in the LogStash::Api::Module::Base



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/logstash/api/app_helpers.rb', line 11

def respond_with(data, options={})
  as     = options.fetch(:as, :json)
  filter = options.fetch(:filter, "")

  status data.respond_to?(:status_code) ? data.status_code : 200

  if as == :json
    if api_error?(data)
      data = generate_error_hash(data)
    else
      selected_fields = extract_fields(filter.to_s.strip)
      data.select! { |k,v| selected_fields.include?(k) } unless selected_fields.empty?
      unless options.include?(:exclude_default_metadata)
        data = data.to_hash
        if data.values.size == 0 && selected_fields.size > 0
          raise LogStash::Api::NotFoundError
        end
        data = .merge(data)
      end
    end

    content_type "application/json"
    LogStash::Json.dump(data, {:pretty => pretty?})
  else
    content_type "text/plain"
    data.to_s
  end
end