class A2::Client

Public Class Methods

new(opts = {}) click to toggle source
# File lib/a2/client.rb, line 15
def initialize(opts = {})
  @automate_url = opts[:automate_url] || ENV['AUTOMATE_URL']
  @automate_token = opts[:automate_token] || ENV['AUTOMATE_TOKEN']
  @ssl_no_verify = opts[:ssl_no_verify] || ENV['AUTOMATE_SSL_NO_VERIFY'] || false

  raise A2::Error, "Must provide the URL for Chef Automate" if @automate_url.nil?
  raise A2::Error, "Must provide a token for Chef Automate" if @automate_token.nil?
end

Public Instance Methods

delete(path) click to toggle source
# File lib/a2/client.rb, line 62
def delete(path)
  response = HTTParty.delete(File.join(@automate_url, path).to_s, {
    verify: !@ssl_no_verify,
    headers: {"api-token" => @automate_token},
  })
  JSON.parse(response.body)
end
download_file(path, json, output_file) click to toggle source
# File lib/a2/client.rb, line 50
def download_file(path, json, output_file)
  File.open(output_file, "w") do |file|
    HTTParty.post(File.join(@automate_url, path).to_s, {
      verify: !@ssl_no_verify,
      headers: {"api-token" => @automate_token},
      body: json
    }) do |fragment|
      file.write(fragment)
    end
  end
end
get(path) click to toggle source
# File lib/a2/client.rb, line 24
def get(path)
  response = HTTParty.get(File.join(@automate_url, path).to_s, {
    verify: !@ssl_no_verify,
    headers: {"api-token" => @automate_token},
  })
  JSON.parse(response.body)
end
post(path, json) click to toggle source
# File lib/a2/client.rb, line 41
def post(path, json)
  response = HTTParty.post(File.join(@automate_url, path).to_s, {
    verify: !@ssl_no_verify,
    headers: {"api-token" => @automate_token},
    body: json
  })
  JSON.parse(response.body)
end
put(path, json) click to toggle source
# File lib/a2/client.rb, line 32
def put(path, json)
  response = HTTParty.put(File.join(@automate_url, path).to_s, {
    verify: !@ssl_no_verify,
    headers: {"api-token" => @automate_token},
    body: json
  })
  JSON.parse(response.body)
end