Module: Smplkit::Flags::Helpers
- Defined in:
- lib/smplkit/flags/helpers.rb
Overview
Helpers that translate between server JSON and the SDK’s wrapper model objects.
Class Method Summary collapse
-
.build_flag_request_body(flag) ⇒ Object
Build the JSON body for a Flag create/update request.
-
.flag_dict_from_json(resource) ⇒ Object
Translate a JSON:API resource Hash into a flat dict the runtime cache can consume.
- .parse_env(env_data) ⇒ Object
- .parse_values(raw) ⇒ Object
Class Method Details
.build_flag_request_body(flag) ⇒ Object
Build the JSON body for a Flag create/update request.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/smplkit/flags/helpers.rb', line 52 def build_flag_request_body(flag) environments = flag.environments.each_with_object({}) do |(env_key, env), out| out[env_key] = { "enabled" => env.enabled, "default" => env.default, "rules" => env.rules.map { |r| { "logic" => r.logic, "value" => r.value, "description" => r.description } } } end attributes = { "id" => flag.id, "name" => flag.name, "type" => flag.type, "default" => flag.default, "description" => flag.description, "environments" => environments } values = flag.values attributes["values"] = values.map { |v| { "name" => v.name, "value" => v.value } } if values { "data" => { "type" => "flag", "id" => flag.id, "attributes" => attributes.compact } } end |
.flag_dict_from_json(resource) ⇒ Object
Translate a JSON:API resource Hash into a flat dict the runtime cache can consume. The shape mirrors smplkit.flags.helpers._flag_dict_from_json in the Python SDK.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/smplkit/flags/helpers.rb', line 13 def flag_dict_from_json(resource) attrs = resource["attributes"] || {} environments = (attrs["environments"] || {}).each_with_object({}) do |(env_key, env_data), out| out[env_key] = parse_env(env_data || {}) end { "id" => resource["id"] || attrs["id"], "name" => attrs["name"], "type" => attrs["type"], "default" => attrs["default"], "values" => parse_values(attrs["values"]), "description" => attrs["description"], "environments" => environments } end |
.parse_env(env_data) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/smplkit/flags/helpers.rb', line 36 def parse_env(env_data) rules = (env_data["rules"] || []).map do |r| FlagRule.new( logic: r["logic"] || {}, value: r["value"], description: r["description"] ) end FlagEnvironment.new( enabled: env_data.fetch("enabled", true) ? true : false, default: env_data["default"], rules: rules ) end |
.parse_values(raw) ⇒ Object
30 31 32 33 34 |
# File 'lib/smplkit/flags/helpers.rb', line 30 def parse_values(raw) return nil if raw.nil? raw.map { |v| FlagValue.new(name: v["name"], value: v["value"]) } end |