Module: Kelbim::PolicyTypes

Defined in:
lib/kelbim/policy-types.rb

Constant Summary collapse

POLICIES =
{
  :ssl_negotiation               => 'SSLNegotiationPolicyType',
  :app_cookie_stickiness         => 'AppCookieStickinessPolicyType',
  :lb_cookie_stickiness          => 'LBCookieStickinessPolicyType',
  #:proxy_protocol                => 'ProxyProtocolPolicyType',
  #:backend_server_authentication => 'BackendServerAuthenticationPolicyType',
  #:public_key                    => 'PublicKeyPolicyType',
}
EXPANDERS =
{
  :ssl_negotiation => proc {|attrs|
    attrs.select {|name, value|
      value[0] =~ /\Atrue\Z/i
    }.map {|n, v| n }
  },
  :app_cookie_stickiness => proc {|attrs| h = {}; attrs.map {|k, v| h[k] = [v].flatten[0] }; h },
  :lb_cookie_stickiness => proc {|attrs| h = {}; attrs.map {|k, v| h[k] = [v].flatten[0] }; h },
}
UNEXPANDERS =
{
  :ssl_negotiation => proc {|attrs|
    unexpanded = {}

    attrs.each do |name|
      unexpanded[name] = ['true']
    end

    unexpanded
  },
  :app_cookie_stickiness => proc {|attrs| h = {}; attrs.map {|k, v| h[k] = [v].flatten }; h },
  :lb_cookie_stickiness => proc {|attrs| h = {}; attrs.map {|k, v| h[k] = [v].flatten }; h },
}

Class Method Summary collapse

Class Method Details

.convert_to_dsl(policy) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/kelbim/policy-types.rb', line 49

def convert_to_dsl(policy)
  policy_name = policy[:name]
  policy_type = policy[:type]
  policy_attrs = policy[:attributes]
  sym = string_to_symbol(policy_type)

  if (expander = EXPANDERS[sym])
    policy_attrs = expander.call(policy_attrs)

    if policy_attrs.kind_of?(Hash)
      new_policy_attrs = {}

      policy_attrs.each do |name, value|
        value = value[0] if value.length < 2
        new_policy_attrs[name] = value
      end

      args = new_policy_attrs.inspect.gsub(/\A\s*\{/, '').gsub(/\}\s*\Z/, '')
    else
      args = policy_attrs.inspect
    end
  else
    args = policy_name.inspect
  end

  "#{sym} #{args}"
end

.expand(sym_or_str, policy_attrs) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/kelbim/policy-types.rb', line 81

def expand(sym_or_str, policy_attrs)
  if sym_or_str.kind_of?(String)
    sym_or_str = string_to_symbol(sym_or_str)
  end

  expander = EXPANDERS[sym_or_str]
  expander ? expander.call(policy_attrs) : policy_attrs
end

.name?(name_or_attrs) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/kelbim/policy-types.rb', line 77

def name?(name_or_attrs)
  name_or_attrs.kind_of?(String)
end

.string_to_symbol(str) ⇒ Object



43
44
45
46
47
# File 'lib/kelbim/policy-types.rb', line 43

def string_to_symbol(str)
  sym = POLICIES.key(str)
  raise "PolicyTypes `#{str}` is not supported" unless sym
  return sym
end

.symbol_to_string(sym) ⇒ Object



37
38
39
40
41
# File 'lib/kelbim/policy-types.rb', line 37

def symbol_to_string(sym)
  str = POLICIES[sym]
  raise "PolicyTypes `#{sym}` is not supported" unless str
  return str
end

.unexpand(sym_or_str, expanded_attrs) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/kelbim/policy-types.rb', line 90

def unexpand(sym_or_str, expanded_attrs)
  if sym_or_str.kind_of?(String)
    sym_or_str = string_to_symbol(sym_or_str)
  end

  unexpander = UNEXPANDERS[sym_or_str]
  unexpander ? unexpander.call(expanded_attrs) : expanded_attrs
end