Class: Capsium::Reactor::Introspection

Inherits:
Object
  • Object
show all
Defined in:
lib/capsium/reactor/introspection.rb,
sig/capsium/reactor/introspection.rbs

Overview

Monitoring HTTP API reports for the package this reactor serves (ARCHITECTURE.md section 7).

Constant Summary collapse

METADATA_PATH =

Returns:

  • (String)
"/api/v1/introspect/metadata"
ROUTES_PATH =

Returns:

  • (String)
"/api/v1/introspect/routes"
CONTENT_HASHES_PATH =

Returns:

  • (String)
"/api/v1/introspect/content-hashes"
CONTENT_VALIDITY_PATH =

Returns:

  • (String)
"/api/v1/introspect/content-validity"
PATHS =

Returns:

  • (Array[String])
[METADATA_PATH, ROUTES_PATH, CONTENT_HASHES_PATH,
CONTENT_VALIDITY_PATH].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package) ⇒ Introspection

Returns a new instance of Introspection.

Parameters:



22
23
24
# File 'lib/capsium/reactor/introspection.rb', line 22

def initialize(package)
  @package = package
end

Instance Attribute Details

#packagePackage (readonly)

Returns the value of attribute package.

Returns:



20
21
22
# File 'lib/capsium/reactor/introspection.rb', line 20

def package
  @package
end

Instance Method Details

#content_hashes_reportHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


58
59
60
# File 'lib/capsium/reactor/introspection.rb', line 58

def content_hashes_report
  { contentHashes: [{ package: package.name, hash: content_hash }] }
end

#content_validity_reportHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/capsium/reactor/introspection.rb', line 62

def content_validity_report
  errors = package.verify_integrity
  entry = {
    package: package.name,
    valid: errors.empty?,
    lastChecked: Time.now.utc.iso8601,
    signed: package.signed?,
    encrypted: package.encrypted?
  }
  entry[:signatureValid] = package.verify_signature if package.signed?
  entry[:reason] = errors.map(&:message).join("; ") unless errors.empty?
  { contentValidity: [entry] }
end

#endpoint?(path) ⇒ Boolean

Parameters:

  • path (String)

Returns:

  • (Boolean)


26
27
28
# File 'lib/capsium/reactor/introspection.rb', line 26

def endpoint?(path)
  PATHS.include?(path)
end

#metadata_reportHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


41
42
43
44
45
46
47
48
49
# File 'lib/capsium/reactor/introspection.rb', line 41

def 
   = package.
  { packages: [{
    name: .name,
    version: .version,
    author: .author,
    description: .description
  }] }
end

#report_for(path) ⇒ Hash[Symbol, untyped]?

The report body for an introspection endpoint, or nil when the path is not an introspection endpoint.

Parameters:

  • path (String)

Returns:

  • (Hash[Symbol, untyped], nil)


32
33
34
35
36
37
38
39
# File 'lib/capsium/reactor/introspection.rb', line 32

def report_for(path)
  case path
  when METADATA_PATH then 
  when ROUTES_PATH then routes_report
  when CONTENT_HASHES_PATH then content_hashes_report
  when CONTENT_VALIDITY_PATH then content_validity_report
  end
end

#routes_reportHash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


51
52
53
54
55
56
# File 'lib/capsium/reactor/introspection.rb', line 51

def routes_report
  entries = package.routes.config.routes.map do |route|
    { method: route.http_method || "GET", path: route.path }
  end
  { routes: [{ package: package.name, routes: entries }] }
end