summaryrefslogtreecommitdiff
path: root/lib/oxidized/source/http.rb
blob: dc1c0b76a45fe8dcb6823c31288b15ef81b7e470 (plain)
1
2
3
4
5
6
7
8
9
10
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module Oxidized
class HTTP < Source
  def initialize
    @cfg = Oxidized.config.source.http
    super
  end

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

  require "net/http"
  require "uri"
  require "json"

  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)
    if (@cfg.user && @cfg.pass)
        request.basic_auth(@cfg.user,@cfg.pass)
    end

    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] = node_var_interpolate line[position]
      end
      keys[:model] = map_model keys[:model] if keys.key? :model

      # map node specific vars
      vars = {}
      @cfg.vars_map.each do |key, position|
        vars[key.to_sym] = node_var_interpolate line[position]
      end
      keys[:vars] = vars unless vars.empty?

      nodes << keys
    end
    nodes
  end

end
end