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



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

def initialize(app)
  @app = app
end

Instance Method Details

#get_node(params, request) ⇒ Array

Gets a node by its ID, with pagination

Parameters:

  • params (Hash)

    an requests parsed params

  • request (Sinatra::Request)

    an request object

Returns:

  • (Array)

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



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

def get_node(params, request)
  id = params[:id]
  pag = paginate request
  node = PuppetHerald::Models::Node.with_reports(id, pag)
  status = 200
  status = 404 if node.nil?
  body = node.to_json(include: :reports, methods: :no_of_reports)
  [status, headers(pag), body]
rescue ArgumentError => ex
  clienterror ex
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]



35
36
37
38
39
40
41
42
# File 'lib/puppet-herald/app/api.rb', line 35

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

#nodes(request) ⇒ Array

Gets all nodes with pagination

Parameters:

  • request (Sinatra::Request)

    an request object

Returns:

  • (Array)

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



47
48
49
50
51
52
53
# File 'lib/puppet-herald/app/api.rb', line 47

def nodes(request)
  pag = paginate request
  nodes = PuppetHerald::Models::Node.paginate(pag)
  [200, headers(pag), nodes.to_json(methods: :no_of_reports)]
rescue ArgumentError => ex
  clienterror ex
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]



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

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]



73
74
75
76
77
78
79
# File 'lib/puppet-herald/app/api.rb', line 73

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