Module: Ai2Web::Util
- Defined in:
- lib/ai2web/util.rb
Overview
Small shared helpers. AI2Web manifests are JSON documents, so the SDK works internally with
string-keyed hashes; deep_stringify lets callers pass symbol keys and still get parity.
Class Method Summary collapse
-
.deep_stringify(obj) ⇒ Object
Recursively convert every Hash key to a String (values untouched).
-
.enabled?(value) ⇒ Boolean
Mirror of @ai2web/core
_has: a capability/transport is on when it istrueor an object withenabled: true. -
.trim_url(url) ⇒ Object
Strip trailing slashes from a URL/base.
-
.truthy?(value) ⇒ Boolean
Python-style truthiness, used only where the reference validator relies on
bool(...)(empty hash/array/string are falsy).
Class Method Details
.deep_stringify(obj) ⇒ Object
Recursively convert every Hash key to a String (values untouched). Idempotent.
10 11 12 13 14 15 16 |
# File 'lib/ai2web/util.rb', line 10 def deep_stringify(obj) case obj when Hash then obj.each_with_object({}) { |(k, v), h| h[k.to_s] = deep_stringify(v) } when Array then obj.map { |e| deep_stringify(e) } else obj end end |
.enabled?(value) ⇒ Boolean
Mirror of @ai2web/core _has: a capability/transport is on when it is true or an
object with enabled: true.
20 21 22 |
# File 'lib/ai2web/util.rb', line 20 def enabled?(value) value == true || (value.is_a?(Hash) && value["enabled"] == true) end |
.trim_url(url) ⇒ Object
Strip trailing slashes from a URL/base.
36 37 38 |
# File 'lib/ai2web/util.rb', line 36 def trim_url(url) url.to_s.sub(%r{/+\z}, "") end |
.truthy?(value) ⇒ Boolean
Python-style truthiness, used only where the reference validator relies on bool(...)
(empty hash/array/string are falsy). Keeps exact scoring/tier parity with the other SDKs.
26 27 28 29 30 31 32 33 |
# File 'lib/ai2web/util.rb', line 26 def truthy?(value) case value when nil, false then false when Hash, Array, String then !value.empty? when Numeric then value != 0 else true end end |