Class: Ai2Web::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/ai2web/manifest.rb

Overview

Fluent AI2Web (ai2w) manifest builder - "describe your website once".

Every setter returns self, so calls chain. Inputs may use symbol or string keys; the builder stores string keys internally so the result round-trips cleanly as JSON.

manifest = Ai2Web.ai2web(name: "Store", url: "https://store.example", type: "ecommerce")
.capability("content")
.capability("commerce", endpoint: "/ai2w/products", checkout: true)
.contact(support: "help@store.example")
.build

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Manifest

Returns a new instance of Manifest.



17
18
19
# File 'lib/ai2web/manifest.rb', line 17

def initialize(site)
  @m = { "protocol" => "ai2w", "version" => "0.2", "site" => Util.deep_stringify(site), "capabilities" => {} }
end

Class Method Details

.for_site(name:, url:, type:, **extra) ⇒ Object



21
22
23
# File 'lib/ai2web/manifest.rb', line 21

def self.for_site(name:, url:, type:, **extra)
  new({ "name" => name, "url" => url, "type" => type }.merge(Util.deep_stringify(extra)))
end

Instance Method Details

#action(a) ⇒ Object



46
47
48
49
50
# File 'lib/ai2web/manifest.rb', line 46

def action(a)
  (@m["actions"] ||= []) << Util.deep_stringify(a)
  capability("actions", "endpoint" => "/ai2w/actions")
  self
end

#agent_identity(a) ⇒ Object



90
91
92
93
94
# File 'lib/ai2web/manifest.rb', line 90

def agent_identity(a)
  base = @m["identity"].is_a?(Hash) ? @m["identity"] : {}
  @m["identity"] = base.merge("agent" => Util.deep_stringify(a))
  self
end

#agent_service(s) ⇒ Object



59
60
61
62
# File 'lib/ai2web/manifest.rb', line 59

def agent_service(s)
  @m["agent_service"] = Util.deep_stringify(s)
  self
end

#auth(a) ⇒ Object



36
37
38
39
# File 'lib/ai2web/manifest.rb', line 36

def auth(a)
  @m["auth"] = Util.deep_stringify(a)
  self
end

#buildObject



109
# File 'lib/ai2web/manifest.rb', line 109

def build = @m

#capability(name, value = true) ⇒ Object



25
26
27
28
29
# File 'lib/ai2web/manifest.rb', line 25

def capability(name, value = true)
  value = { "enabled" => true }.merge(Util.deep_stringify(value)) if value.is_a?(Hash)
  @m["capabilities"][name.to_s] = value
  self
end


41
42
43
44
# File 'lib/ai2web/manifest.rb', line 41

def consent(c)
  @m["consent"] = Util.deep_stringify(c)
  self
end

#contact(c) ⇒ Object



69
70
71
72
# File 'lib/ai2web/manifest.rb', line 69

def contact(c)
  @m["contact"] = Util.deep_stringify(c)
  self
end

#events(e) ⇒ Object



52
53
54
55
56
57
# File 'lib/ai2web/manifest.rb', line 52

def events(e)
  e = Util.deep_stringify(e)
  @m["events"] = e
  capability("events", "endpoint" => e["endpoint"] || "/ai2w/events")
  self
end

#extension(key, value) ⇒ Object

Attach a vendor extension. The key is namespaced with x- if not already.



102
103
104
105
106
107
# File 'lib/ai2web/manifest.rb', line 102

def extension(key, value)
  key = key.to_s
  key = "x-#{key}" unless key.start_with?("x-")
  @m[key] = Util.deep_stringify(value)
  self
end

#governance(g) ⇒ Object

--- v0.2 optional modules (all additive; a minimal manifest stays valid without them). ---



75
76
77
78
# File 'lib/ai2web/manifest.rb', line 75

def governance(g)
  @m["governance"] = Util.deep_stringify(g)
  self
end

#identity(i) ⇒ Object



64
65
66
67
# File 'lib/ai2web/manifest.rb', line 64

def identity(i)
  @m["identity"] = Util.deep_stringify(i)
  self
end

#knowledge(k) ⇒ Object



96
97
98
99
# File 'lib/ai2web/manifest.rb', line 96

def knowledge(k)
  @m["knowledge"] = Util.deep_stringify(k)
  self
end


85
86
87
88
# File 'lib/ai2web/manifest.rb', line 85

def legal(l)
  @m["legal"] = Util.deep_stringify(l)
  self
end

#to_hObject



110
# File 'lib/ai2web/manifest.rb', line 110

def to_h = @m

#to_json(*_args, indent: 2) ⇒ Object

Serialize to a JSON string. Pretty-printed by default; pass indent: 0 for compact output. Accepts (and ignores) a JSON generator state so the object still works when nested inside another JSON.generate call.



115
116
117
# File 'lib/ai2web/manifest.rb', line 115

def to_json(*_args, indent: 2)
  indent.to_i.positive? ? JSON.pretty_generate(@m) : JSON.generate(@m)
end

#transports(t) ⇒ Object



31
32
33
34
# File 'lib/ai2web/manifest.rb', line 31

def transports(t)
  (@m["transports"] ||= {}).merge!(Util.deep_stringify(t))
  self
end

#usage_policy(u) ⇒ Object



80
81
82
83
# File 'lib/ai2web/manifest.rb', line 80

def usage_policy(u)
  @m["usage_policy"] = Util.deep_stringify(u)
  self
end