Sha256: a5d32d6695333a587c87c46c6743d712eed2274f49a033a94f8d8ba639491c77

Contents?: true

Size: 1.3 KB

Versions: 15

Compression:

Stored size: 1.3 KB

Contents

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] = 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

end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
oxidized-0.16.3 lib/oxidized/source/http.rb
oxidized-0.16.2 lib/oxidized/source/http.rb
oxidized-0.16.1 lib/oxidized/source/http.rb
oxidized-0.16.0 lib/oxidized/source/http.rb
oxidized-0.15.0 lib/oxidized/source/http.rb
oxidized-0.14.3 lib/oxidized/source/http.rb
oxidized-0.14.2 lib/oxidized/source/http.rb
oxidized-0.14.1 lib/oxidized/source/http.rb
oxidized-0.14.0 lib/oxidized/source/http.rb
oxidized-0.13.1 lib/oxidized/source/http.rb
oxidized-0.13.0 lib/oxidized/source/http.rb
oxidized-0.12.2 lib/oxidized/source/http.rb
oxidized-0.12.1 lib/oxidized/source/http.rb
oxidized-0.12.0 lib/oxidized/source/http.rb
oxidized-0.11.0 lib/oxidized/source/http.rb