Class: PuppetHerald::App::ApiImplV1

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-herald/app/api.rb

Overview

An implementation of API v1

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ApiImplV1

Constructor

Parameters:

  • app (Sinatra::Base)

    an app module



17
18
19
# File 'lib/puppet-herald/app/api.rb', line 17

def initialize(app)
  @app = app
end

Instance Method Details

#get_node(params) ⇒ Array

Gets a node by its ID

Parameters:

  • params (Hash)

    an requests parsed params

Returns:

  • (Array)

    an response array: [code, body] or [code, headers, body]



49
50
51
52
53
54
55
56
# File 'lib/puppet-herald/app/api.rb', line 49

def get_node(params)
  id = params[:id]
  node = PuppetHerald::Models::Node.get_with_reports(id)
  status = 200
  status = 404 if node.nil?
  body = node.to_json(include: :reports)
  [status, body]
end

#get_report(params) ⇒ Array

Get a report by its ID

Parameters:

  • params (Hash)

    an requests parsed params

Returns:

  • (Array)

    an response array: [code, body] or [code, headers, body]



32
33
34
35
36
37
38
39
# File 'lib/puppet-herald/app/api.rb', line 32

def get_report(params)
  id = params[:id]
  report = PuppetHerald::Models::Report.get_with_log_entries(id)
  status = 200
  status = 404 if report.nil?
  body = report.to_json(include: :log_entries)
  [status, body]
end

#nodesArray

Gets all nodes

Returns:

  • (Array)

    an response array: [code, body] or [code, headers, body]



42
43
44
45
# File 'lib/puppet-herald/app/api.rb', line 42

def nodes
  nodes = PuppetHerald::Models::Node.all
  [200, nodes.to_json]
end

#post_reports(request) ⇒ Array

Creates a new report

Parameters:

  • request (Sinatra::Request)

    an request object

Returns:

  • (Array)

    an response array: [code, body] or [code, headers, body]



23
24
25
26
27
28
# File 'lib/puppet-herald/app/api.rb', line 23

def post_reports(request)
  yaml = request.body.read
  report = PuppetHerald::Models::Report.create_from_yaml yaml
  body = { id: report.id }.to_json
  [201, body]
end

#version_jsonArray

Get a app’s artifact information

Returns:

  • (Array)

    an response array: [code, body] or [code, headers, body]



59
60
61
62
63
64
65
# File 'lib/puppet-herald/app/api.rb', line 59

def version_json
  ver = {}
  [:VERSION, :LICENSE, :NAME, :PACKAGE, :SUMMARY, :DESCRIPTION, :HOMEPAGE].each do |const|
    ver[const.downcase] = PuppetHerald.const_get const
  end
  [200, ver.to_json]
end