Class: Oxidized::HTTP

Inherits:
Source
  • Object
show all
Defined in:
lib/oxidized/source/http.rb

Instance Method Summary collapse

Methods inherited from Source

#map_model

Constructor Details

#initializeHTTP

Returns a new instance of HTTP.



3
4
5
6
# File 'lib/oxidized/source/http.rb', line 3

def initialize
  @cfg = CFG.source.http
  super
end

Instance Method Details

#loadObject



18
19
20
21
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
47
48
49
50
51
# File 'lib/oxidized/source/http.rb', line 18

def load
  nodes = []
  uri = URI.parse(@cfg.url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme == 'https'

  # map headers
  headers = {}
  @cfg.headers.each do |header, value|
    headers[header] = value
  end

  request = Net::HTTP::Get.new(uri.request_uri, headers)

  response = http.request(request)
  data = JSON.parse(response.body)
  data.each do |line|
    next if line.empty?
    # map node parameters
    keys = {}
    @cfg.map.each do |key, position|
      keys[key.to_sym] = line[position]
    end
    keys[:model] = map_model keys[:model] if keys.key? :model

    # map node specific vars, empty value is considered as nil
    vars = {}
    @cfg.vars_map.each { |key, position| vars[key.to_sym] = line[position].to_s.empty? ? nil : line[position] }
    keys[:vars] = vars unless vars.empty?

    nodes << keys
  end
  nodes
end

#setupObject



8
9
10
11
12
# File 'lib/oxidized/source/http.rb', line 8

def setup
  if @cfg.url.empty?
    raise NoConfig, 'no source http url config, edit ~/.config/oxidized/config'
  end
end