Class: PhobosPrometheus::ExporterHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/phobos_prometheus/exporter_helper.rb

Overview

Helper for Exporter

Class Method Summary collapse

Class Method Details

.build_dictionary(formats, fallback) ⇒ Object



34
35
36
37
38
39
# File 'lib/phobos_prometheus/exporter_helper.rb', line 34

def build_dictionary(formats, fallback)
  formats.each_with_object('*/*' => fallback) do |format, memo|
    memo[format::CONTENT_TYPE] = format
    memo[format::MEDIA_TYPE] = format
  end
end

.extract_quality(attributes, default = 1.0) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/phobos_prometheus/exporter_helper.rb', line 24

def extract_quality(attributes, default = 1.0)
  quality = default

  attributes.delete_if do |attr|
    quality = attr.split('q=').last.to_f if attr.start_with?('q=')
  end

  quality
end

.negotiate(env, formats) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/phobos_prometheus/exporter_helper.rb', line 7

def negotiate(env, formats)
  parse(env.fetch('HTTP_ACCEPT', '*/*')).each_entry do |content_type, _|
    return formats[content_type] if formats.key?(content_type)
  end

  nil
end

.parse(header) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/phobos_prometheus/exporter_helper.rb', line 15

def parse(header)
  header.split(/\s*,\s*/).map do |type|
    attributes = type.split(/\s*;\s*/)
    quality = extract_quality(attributes)

    [attributes.join('; '), quality]
  end.sort_by(&:last).reverse
end