class Netdata::Client::Helper::Network

Perform HTTP requests

Attributes

config[RW]

Access the configuration object instance externally

Public Instance Methods

get(endpoint, url, args, version = 'v1') click to toggle source

Perform a GET request to a specified URL Params:

url

The URL you want to hit

key

The authentication key to pass via headers to the URL

# File lib/netdata/client/helper/network.rb, line 13
def get(endpoint, url, args, version = 'v1')
  qs = build_qs(args)
  req_url = "#{url}/api/#{version}/#{endpoint}?#{qs}"

  request(req_url, :GET)
end
post(endpoint, url, args, version = 'v1') click to toggle source

Perform a POST request to a specified URL Params:

url

The URL you want to hit

key

The authentication key to pass via headers to the URL

# File lib/netdata/client/helper/network.rb, line 24
def post(endpoint, url, args, version = 'v1')
  qs = build_qs(args)
  req_url = "#{url}/api/#{version}/#{endpoint}?#{qs}"

  request(req_url, :POST)
end

Private Instance Methods

build_qs(args) click to toggle source

Create a query string from a hash Params:

args

The hash

# File lib/netdata/client/helper/network.rb, line 59
def build_qs(args)
  args.map { |k, v| "#{k}=#{v}" }.join('&')
end
request(url, type) click to toggle source

Create and send the HTTP request Params:

url

The URL you want to hit

type

The HTTP method to send

key

The authentication key to pass via headers to the URL

# File lib/netdata/client/helper/network.rb, line 38
def request(url, type)
  url = URI(URI.encode(url))
  type ||= :GET
  req_path = "#{url.path}?#{url.query}"

  if type == :GET
    req = Net::HTTP::Get.new(req_path)
  elsif type == :POST
    req = Net::HTTP::Post.new(req_path)
  end

  res = Net::HTTP.new(url.host, url.port).start do |http|
    http.request(req)
  end

  res
end