class Checkson::APIClient

Attributes

apiaddr[R]
outfile[R]

Public Class Methods

new(apiaddr, outfile) click to toggle source
# File lib/checkson/apiclient.rb, line 15
def initialize(apiaddr, outfile)
  @apiaddr = apiaddr
  @outfile = outfile
end

Public Instance Methods

getchecks() click to toggle source
# File lib/checkson/apiclient.rb, line 20
    def getchecks
      template = <<-TEMPLATE
        <%@checks.each do |check| %>
        check '<%=check["description"]%>' do
          using <%=check["check"]%>
          <%check["sets"].each do |key,value| %>
          set :<%=key%>, "<%=value%>"
          <%end%>
        <%unless check["helps"].eql?(nil)%>
          <%check["helps"].each do |help| %>
          help '<%= help %>'
          <%end%>
        <%end%>
        end
        <% end %>
      TEMPLATE
      querydata = get
      @checks = querydata['checks'] if querydata['status'].eql? 'ok'
      begin
        remove_empty_lines(ERB.new(template).result(binding))
      rescue StandardError
        die querydata['error']
      end
    end
writechecks() click to toggle source
# File lib/checkson/apiclient.rb, line 45
def writechecks
  File.open(@outfile, 'w') do |f|
    f.write(getchecks)
  end
end

Private Instance Methods

die(msg) click to toggle source
# File lib/checkson/apiclient.rb, line 75
def die(msg)
  warn(msg)
  exit 1
end
get() click to toggle source
# File lib/checkson/apiclient.rb, line 58
def get
  token = ENV['CHECKSON_API_KEY']
  token ||= 'empty'
  hostname = Socket.gethostname
  query("#{keyhash(token)}/get/#{hostname}")
end
keyhash(token) click to toggle source
# File lib/checkson/apiclient.rb, line 71
def keyhash(token)
  Digest::SHA512.hexdigest(token)
end
query(command) click to toggle source
# File lib/checkson/apiclient.rb, line 65
def query(command)
  @ep = "#{@apiaddr}/api/v1"
  route = "#{@ep}/#{command}"
  JSON.parse(Net::HTTP.get(URI.parse(route)))
end
remove_empty_lines(string) click to toggle source
# File lib/checkson/apiclient.rb, line 53
def remove_empty_lines(string)
  regex = /^\s]$\n/
  string.gsub(regex, '')
end